5vmo1g
Last Updated: May 27, 2017
·
55.31K
· 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.

Say Thanks
Respond

8 Responses
Add your response

7436
Headshot

Thanks a lot

over 1 year ago ·
8823
Nikita korotaev thumb

Does it keeps text selectable and links clickable?

over 1 year ago ·
8871
D0d5a68d9b32452f7e76f7efe602e8fd

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

over 1 year ago ·
10045

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

over 1 year ago ·
10305
4d01cef554cce52e57301f74bcac6a07

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 ·
12341
Hugui normal

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

over 1 year ago ·
15879
Starbird crop2

Whether the text is selectable or not seems to depend on the system you've built the phantomjs code on. On a mac the text is not selectable for me but on a recent ubuntu it is.

over 1 year ago ·
26255
None

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 ·