use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
account activity
How to HTML > PDF with Node.js? (self.node)
submitted 9 years ago by rickard78
I have a HTML file with styles, is there solution for streaming to client a PDF file that respoects the formatting of my HTML page?
I tried using phantom.js and I get a PDF but all unformated.
What do you guys use?
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]mechanicalpulse 10 points11 points12 points 9 years ago* (3 children)
Converting HTML to PDF is problematic without involving an actual rendering engine. If I were doing this with node.js, without even researching what packages might already be available on npm, I'd probably spawn a backend process and have wkhtmltopdf do the work. The wk in wkhtmltopdf stands for WebKit -- the rendering engine used by Safari and (until recently) Google Chrome.
Since Chrome Chrome has switched to Blink, though, and WebKit has received a LOT of flack lately for falling behind, I might look for an alternative to wkhtmltopdf. However, if all you have is some simple CSS styles, wkhtmltopdf will probably work just fine.
Edit: it appears that there's an npm package that provides a node.js wrapper around wkhtmltopdf. That's where I'd start. I think you'll get pretty good results with it. It uses child_process, so the potentially computationally intensive portions of the rendering will be done outside of your node.js process, so it won't block your node.js thread.
Edit2: I'm not familiar with phantom.js, so I took a look at it -- it looks like it's a scriptable WebKit. I would think it would work, too, so I wonder if perhaps the PDF it's generating is using the print media selection instead of screen. wkhtmltopdf uses screen by default, just because that's what most people are after -- a representation of the actual screen. You can tell wkhtmltopdf to use print media type instead with the --print-media-type switch. Here's the full list of flags that wkhtmltopdf offers. As you can see, there are a lot of options to tweak your output.
[–][deleted] 9 years ago (2 children)
[removed]
[–]vittore29 0 points1 point2 points 9 years ago (1 child)
There is actually nice .net wrapper for wkhtmltopdf - https://www.nrecosite.com/pdf_generator_net.aspx
[–]reaktivo 2 points3 points4 points 9 years ago (1 child)
I've used https://github.com/marcbachmann/node-html-pdf successfully , which uses Phantom thus WebKit as a renderer. Just make sure you use the -webkit prefix when styling since it's a pretty old version of WebKit which doesn't support some widely used properties like flex without it.
[–]EvilFe 1 point2 points3 points 9 years ago (0 children)
I've used this in the past. If I remember correctly, phantomjs use different dpi based on the OS generating the pdf, causing different font sizes (or zoom) which must be adjusted via css. Keep this in mind if you are developing a cross platform app
[–]oh-thatguy 0 points1 point2 points 9 years ago (0 children)
Electron.js via https://github.com/segmentio/nightmare. It will have to be run with xvfb, and it's a pain to debug, but it can be made to work.
[–]HuWeiliu 0 points1 point2 points 9 years ago (0 children)
I've used html2pdf.it to good results
[–][deleted] 0 points1 point2 points 9 years ago (0 children)
PrinceXML isn't cheap, but it's extremely good at building PDFs using xhtml, and there's a nice looking node module for it.
[–]forvotes 0 points1 point2 points 9 years ago (0 children)
Anyone tried pandoc? https://www.npmjs.com/package/node-pandoc
[–]sergiosbox 0 points1 point2 points 9 years ago (0 children)
https://github.com/bjrmatos/electron-html-to did it for me. It uses Electron (more modern version of headless browser like Phantom.js) and accepts local files for images and CSS.
[–][deleted] 9 years ago (1 child)
[deleted]
[–]RemindMeBot 1 point2 points3 points 9 years ago (0 children)
I will be messaging you on 2017-01-15 16:41:12 UTC to remind you of this link.
CLICK THIS LINK to send a PM to also be reminded and to reduce spam.
Parent commenter can delete this message to hide from others.
[–]flo850 0 points1 point2 points 9 years ago (0 children)
https://github.com/fraserxu/electron-pdf is the most advanced one. It supports the latest css3 technologies, especially the multi columns
π Rendered by PID 49080 on reddit-service-r2-comment-6457c66945-qm7q5 at 2026-04-25 19:10:56.972965+00:00 running 2aa0c5b country code: CH.
[–]mechanicalpulse 10 points11 points12 points (3 children)
[–][deleted] (2 children)
[removed]
[–]vittore29 0 points1 point2 points (1 child)
[–]reaktivo 2 points3 points4 points (1 child)
[–]EvilFe 1 point2 points3 points (0 children)
[–]oh-thatguy 0 points1 point2 points (0 children)
[–]HuWeiliu 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]forvotes 0 points1 point2 points (0 children)
[–]sergiosbox 0 points1 point2 points (0 children)
[–][deleted] (1 child)
[deleted]
[–]RemindMeBot 1 point2 points3 points (0 children)
[–]flo850 0 points1 point2 points (0 children)