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')
None
Comments