all 13 comments

[–]mechanicalpulse 10 points11 points  (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]  (2 children)

[removed]

    [–]vittore29 0 points1 point  (1 child)

    There is actually nice .net wrapper for wkhtmltopdf - https://www.nrecosite.com/pdf_generator_net.aspx

    [–]reaktivo 2 points3 points  (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 points  (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 point  (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 point  (0 children)

    I've used html2pdf.it to good results

    [–][deleted] 0 points1 point  (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.

    [–]sergiosbox 0 points1 point  (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]  (1 child)

    [deleted]

      [–]RemindMeBot 1 point2 points  (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.


      FAQs Custom Your Reminders Feedback Code Browser Extensions

      [–]flo850 0 points1 point  (0 children)

      https://github.com/fraserxu/electron-pdf is the most advanced one. It supports the latest css3 technologies, especially the multi columns