james
james

Reputation: 169

How to use pdfkit on meteor

I've installed meteor add jaredmartin:pdfkit but the documentation doesn't seem correct. I receive an error with the first line PDFDocument = require 'pdfkit' and can't get past this? Is there a meteor pdfkit manual or something I'm doing wrong on meteor 0.9?

Upvotes: 0

Views: 886

Answers (3)

EdU
EdU

Reputation: 71

In Meteor 1.1.0.2, im using a Route page to build a PDF, and download it.

(Consider: packages multiply:iron-router-progress and pascoual:pdfkit)

Router.route('/buildPDF/:_param1', function () {

  var param1 = this.params._param1;
  if (!param1) return;

  var doc = new PDFDocument({
    size: 'A4',
    margins: {
      top: 50,
      bottom: 0,
      left: 50,
      right: 50,
    }
  });

  doc.image(process.env.PWD + '/public/img/sample1.jpg', 0, 30, {
    width: 598
  });

  doc.rect(0, 370, 598, 210)
    .fill('#22829f', 'even-odd');

  doc.fontSize(25).fillColor('white');
  doc.text('some text', 50, 390, {
    align: 'center',
    width: 500
  });

...

  this.response.writeHead(200, {
    'Content-type': 'application/pdf',
    'Content-Disposition': 'attachment; filename=sometitle.pdf'
  });
  this.response.end(doc.outputSync());
}, {
  where: 'server'
});

All works prefecly, in chrome the PDF is automatically downloaded, except when I install on test Meteor servers, I just receive a message error in a blank page

Server Error.

Using meteor logs myapp.meteor.com I got this:

W20150615-16:52:09.513(-3)? (STDERR) Error: ENOENT, no such file or directory '/Users/EdU/Documents/Develope/git-repos/TyTimg/layers/sample1.jpg'
W20150615-16:52:09.515(-3)? (STDERR)     at Object.fs.openSync (fs.js:439:18)
W20150615-16:52:09.515(-3)? (STDERR)     at Object.fs.readFileSync (fs.js:290:15)
W20150615-16:52:09.515(-3)? (STDERR)     at Function.PDFImage.open (/Users/EdU/.meteor/packages/pascoual_pdfkit/.1.0.5.7067nv++os+web.browser+web.cordova/npm/node_modules/pdfkit/js/image.js:27:28)
W20150615-16:52:09.515(-3)? (STDERR)     at PDFDocument.module.exports.image (/Users/EdU/.meteor/packages/pascoual_pdfkit/.1.0.5.7067nv++os+web.browser+web.cordova/npm/node_modules/pdfkit/js/mixins/images.js:30:26)

The variable process.env.PWD loose the context into Meteor test server. Im trying to solve this. I hope to help you.

Upvotes: 0

Jared Martin
Jared Martin

Reputation: 653

Wow, I just saw this here, and I'm sorry it's been so long and you were having trouble using my package. The truth is, I just made it cause at the time, pascual had not updated his original package to work with the new meteor packaging system so I made mine as a stopgap. Since then he has updated his package, so I recommend you use his package pascoual:pdfkit

Upvotes: 1

Travis Ashworth
Travis Ashworth

Reputation: 1

I'm also having trouble. I found the following for an older meteor package and it doesn't have that line but I've yet to make it work.

      var doc = new PDFDocument({size: 'A4', margin: 50});
      var imageBase64 = Meteor.users.findOne(this.userId).profile.picture;
      var imageBuffer2 = new Buffer(imageBase64.replace('data:image/png;base64,','') || '', 'base64');
      doc.image(imageBuffer2, 10, 10, {height: 75});
      doc.fontSize(12);
      doc.text('PDFKit is simple', 10, 30, {align: 'center', width: 200});
      // Save it on myApp/public/pdf folder (or any place) with the Fibered sync methode:
      doc.writeSync(process.env.PWD + '/public/pdf/PDFKitExample.pdf');

Upvotes: 0

Related Questions