Help. Rendering a Template as a PDF


#1

I’m a newbie with Meteor. I need help with how to render a template as a PDF. I found a couple packages: pascoual:pdfjs & pascoual:pdfkit. I have gotten it to work so that my route automatically downloads a PDF. The route looks like this:

Router.route('/orders/dl_pdf/:_id', {
   name: 'order.pdf',
   action: function () {
      var order = Orders.findOne({ _id: this.params._id });
      var doc = new PDFDocument({size: 'A4', margin: 50});
      doc.fontSize(12);
      doc.text("Invoice #" + order.orderNumber, 10, 30, {align: 'center', width: 200});
      this.response.writeHead(200, {
         'Content-type': 'application/pdf',
         'Content-Disposition': "attachment; filename=order-" + this.params._id + ".pdf"
      });
      this.response.end( doc.outputSync() );
   }, where: 'server'
});

But I have a couple issues with this. One thing is, I would have to build the entire PDF in doc.text(); I want to learn how to pull my text from a template (if possible). And I want to show the PDF in the browser instead of downloading it.

Am I using the right packages? Any advice or even a pointer to an outside tutorial so I can figure it out. I tried doing my own searching but am having trouble finding a decent tutorial that actually helps me.


#2

To render the PDF, I would create a Blaze/React view and grab the output with PhantomJS (which has PDF export). Creating a PDF in code can be hard.


#3

Thank you! I looked into PhantomJS a little, seems to be what I need.


#4

can you share your sample code here =)


#5

Are you talking about mine? Because as of so far, I only have what I posted in the OP. I have moved on to other sections of my project before I return to PDFs


#6

Yes I was talking about your pdf code in your project which is already functional


#7

Yes, the code I have that is functional is exactly what I’ve posted in the OP lol. When I link to that route, it downloads the PDF. As of right now it’s not really usable…because all it does is print out an order number at the top of the PDF. It doesn’t actually build a whole order into a PDF yet. That’s why I was looking for help on how to capture a template so it can render as a PDF so I don’t have to build the whole PDF like that.

But to be honest, I am not sure I’m even going to bother with this for the time being because if someone wants to save a PDF they can do CTRL + P and Chrome has built-in capability to render the page as a pdf and you can save/print from there. And for now, that is good enough for my use case.