Last Updated: July 25, 2019
·
91.21K
· iwootten

Use PhantomJS to Create PDFs from HTML

If you want to create PDF versions of your HTML use phantomjs PDF rendering. The rasterise.js example given in the PhantomJS works perfectly out of the box.

You can execute this on the command-line using:

phantomjs rasterize.js 'http://en.wikipedia.org/w/index.php?title=Jakarta&printable=yes' jakarta.pdf

You could integrate this call into an app by setting up a queue for calls to phantomjs. I use this in a couple of apps myself.

This way, you'll get pixel perfect PDF's of your HTML (along with the ability to use fancy webfonts), without having to learn a whole bunch of new syntax for, or install random modules for.

7 Responses
Add your response

Thanks a lot

over 1 year ago ·

Does it keeps text selectable and links clickable?

over 1 year ago ·

Nice, that could come in very useful in a project I'm working on!

over 1 year ago ·

Doesn't rasterize just export an image? If that is the case, then this probably isn't an acceptable solution.

over 1 year ago ·

According to http://we-love-php.blogspot.com/2012/12/create-pdf-invoices-with-html5-and-phantomjs.html the text is selectable (don't know about links)

over 1 year ago ·

I tried the example and it's just a picture exported as pdf so no text can be selected

over 1 year ago ·

I am having a form template which gendrate the pdf using panthom js , How can i insert my logo on every single pdf page

over 1 year ago ·