Puppeteer - good library for generating pdf's from html.
Public
19 Dec 09:48

Puppeteer is node library providing high level chromium api. One of it’s key features is generating pdf from html using chromium web engine. This is great advantage over other libraries like wicked_pdf(wkhtmltopdf) because their web engines are often outdated and have troubles with javascript. While using puppeteer pdf I haven’t experienced problems with generating charts and render time decreased up to 20 times (in one case 120 seconds to 6).

Installation:
Similar to any npm package (Node v7.6.0 or greater.):

npm i puppeteer
# or 
yarn add puppeteer

When you install Puppeteer, it downloads a recent version of Chromium (~170MB Mac, ~282MB Linux, ~280MB Win) .
Chromium needs some dependencies, depending on system:
https://github.com/GoogleChrome/puppeteer/blob/master/docs/troubleshooting.md

Usage of this library is similar to current one. Our application renders html page to temporary file and then path to that file, output file and params are passed to js script that calls puppeeter to load this page and render it to pdf with desired width and height.

Simple script for generating pdf using puppeteer:

const puppeteer = require('puppeteer');

(async () => {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  await page.goto('https://somewebpage.com', {waitUntil: 'networkidle2'});
  await page.pdf({path: 'output_file.pdf', format: 'A4'});

  await browser.close();
})();

This library has variety of options i.e. you can pass html string via:

page.setContent(html)

and set media type to screen, since default print view can mess up you layout

page.emulateMedia('screen')
https://github.com/GoogleChrome/puppeteer 

Comments

Joe
mus
Looks awesome. It will also make debugging easier. But please remember that in our projects we often use yarn instead of npm, so it will be `yarn add pupeteer`. It's also possible to load html from a string `page.setContent(html)` instead of visiting a web page.
Joe
szymon
@mus Loading html from string is nice idea, but in our case there htmls are quite big, and by passing it using file I'm sure that any bad stuff won't happen (like encoding problems or escaping characters).