Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert HTML + CSS to PDF with nodeJS or Javascript?

I have a problem. I've tried some libraries that convert html to PDF but they don't import CSS, so my PDF is invalid.

I's tried with "html2pdf" , "pdfmake", "jspdf"..

PDFMake does not help me because it need to generate a JSON with HTML data...

The structure of file that I would like to convert to PDF is:

  • html: www/templates/phase_one_report.html
  • css: www/css/phase_one_report.css

Some ideas? I am using nodeJS with sailsJS in backend and javascript with ionic in frontend.

Sorry about my english.

like image 952
oihi08 Avatar asked Oct 29 '25 18:10

oihi08


1 Answers

There are a number of options available right now:

Edit 09/2018: Use puppeteer, the JS Headless Chrome driver. Firefox now also has headless mode but I'm not sure which library corresponds to puppeteer.

wkhtmltopdf as mentioned before does the job but is slightly outdated.

You will have to watch the latest chrome releases which will have a --headless option to enable html+css+js to pdf conversion.

Then there is PhantomJS and Slimer.js. Both are possible to use with node and Javascript. Nightmare.js is also an option but sits on top of it.

However, Phantom.js is currently the only solution that is truly headless and javascript based. Slimer.JS works with Firefox but requires you to have a window manager, at least xvfb, a virtual frame buffer.

If you want the latest browser features you will have to go with slimer.js or, another option, go with one of the Electron based solutions that keep popping up. Electron is based on Chrome and is scriptable too. A fine solution that also ships with Docker containers is currently https://github.com/msokk/electron-render-service

This list is possibly incomplete and will change a lot in the near future.

like image 156
three Avatar answered Nov 01 '25 07:11

three