Puppeteer - How to export/print HTML to a PDF file in NodeJS?

Puppeteer has become pretty popular for doing any kind of automation related to Chrome web. It is a NodeJS library to give a higher level of control over Headless Chrome.

Chrome has done a huge favor by allowing it to run in headless mode else people used to do that using PhantomJS etc which used to break a lot of time. So, I have personally used Phantom and Puppeteer and found that Puppeteer is way better than Phantom for writing any application which requires Chrome to run in headless mode. A few examples where you need your browser to run in headless mode:

  • For scraping websites
  • For taking snapshots and printing PDF's from webpage
  • For automated UI/end to end testing
  • For finding performance issues of a website and many more

In this blog, we will discuss on printing a webpage or HTML to pdf using Puppeteer.

With Puppeteer, it has become extremely easy to save/export a webpage as PDF at runtime in your node applications. You just need to run the snippet below and it will create a pdf for any given URL in the input field of function html2pdf like https://browsee.io.

const puppeteer = require('puppeteer');

async function startBrowser() {
    const browser = await puppeteer.launch();
    const page = await browser.newPage();
    return {browser, page};
}

async function closeBrowser(browser) {
    return browser.close();
}

async function html2pdf(url) {
    const {browser, page} = await startBrowser();
    await page.goto(url);
    await page.emulateMedia('screen');
    await page.pdf({path: 'page.pdf'});
}

(async () => {
    await html2pdf("https://browsee.io/");
    process.exit(1);
})();

Using this function, you can export PDF for invoices, invites, emails etc in your node application directly considering they are either accessible to you at a certain URL or you have the complete HTML.

Happy Puppeteering!