#!/node.js HTML to PDF using Puppeteer

I don’t like using third party libraries if I can help it. They’re useful, but often unnecessary in production code.

There are a lot of HTML to PDF libraries out there, most of which use Headless Chrome or PhantomJS. Most (all?) of them require you to run PhantomJS or Headless Chrome on the side as a server, while they do their work. I don’t like the sound of that; that sounds really hacky.

Google actually publishes a fantastic library called Puppeteer that doesn’t require you to manually run Headless Chrome on the side (although you can, if you’d like). What’s more, you can use Puppeteer to transform your HTML files to PDF; it’s really painless and easy to use…

Check out the docs when you can. Here are a couple of links for you, followed by a snippet (untested):
Official Documentation
Official PDF example

const puppeteer = require('puppeteer')
async (err, data) => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
//visit the page and wait till all asset & XHR calls are done.
wait page.goto(`file://${HTML}`, {waitUntil: 'networkidle'});
    await page.pdf({
path: PDF_OUT,
printBackground: true,
margin: {
top: 0,
bottom: 0,
left: 0,
right: 0

await browser.close();

Monarch Wadia is Principal Developer at Zero Projects, where he does Javascript consulting for fun and profit. Feel free to reach out if you need a hand with your code!