Puppeteer is a Node library that interacts with browsers that support Chrome DevTools Protocol (CDP). It is not only Chrome and Chromium, but Firefox also has partial support of CDP.
A working code example to generate PDFs with Puppeteer
Before starting to work with Puppeteer, let’s install it using npm:
npm i puppeteer
It is relatively easy to generate PDFs with Puppeteer:
"use strict";
const puppeteer = require("puppeteer");
(async () => { const browser = await puppeteer.launch({});
try { const page = await browser.newPage();
await page.goto("https://example.com/", { waitUntil: "networkidle0", });
const selector = "div"; await page.waitForSelector(selector, { visible: true, });
await page.pdf({ path: "example.com.pdf", format: "a4" }); } catch (e) { console.log(e); } finally { await browser.close(); }})();
Pros and cons of using Puppeteer for rendering PDFs
-
I would love to repeat that, but the massive benefit of using Puppeteer is having fine-grained control over rendering results against rendering PDFs only in the browser.
-
Managing headless browsers is a huge pain. The browsers might have memory leaks and suddenly restarts. You need to update them to the latest versions. And this is only a small list of problems that you will encounter.
-
Puppeteer is imperfect, and the rendering PDF has different artifacts that must be addressed.
-
It is a computation-heavy task for servers. And scaling it for running multiple browsers can be a problem in itself.
Look at all possible Puppeteer PDF options. It is an exciting and complex problem, which deserves a separate post.
It depends on your use case, but also consider using PDFKit for programmatic PDF generation.
But there is a solution to address mentioned Puppeteer issues.
Using modern and scalable URL or HTML to PDF API
If you don’t want to deal with all the burdens of managing headless browsers, you can use ScreenshotOne API to render PDFs from HTML or any URL. It is a free PDF to HTML API for up to 100 requests. The PDF generation API from ScreenshotOne is easy to use. It is scalable, covers a variety of use cases, and solves all the issues related to rendering PDFs in headless browsers.
Let’s take a look at how easy it is to render a PDF with a straightforward call:
https://api.screenshotone.com/take?url=https://example.com&format=pdf&access_key=<your access key>
And the result is a PDF document.