Puppeteer PDF: generate a PDF from a web page

By Mayur Gadakh · Published · Last updated

To generate a PDF with Puppeteer, launch the browser, open the URL and call page.pdf() with a format and printBackground: true. The code below runs as written. One URLBooth API request gives the same PDF without running a browser.

How do I create a PDF with Puppeteer?

Install with npm i puppeteer (it downloads a matching Chrome), then run it. If your npm version skips install scripts and no browser was downloaded, run npx puppeteer browsers install chrome once.

import puppeteer from "puppeteer";
​
const browser = await puppeteer.launch();
try {
const page = await browser.newPage();
await page.goto("https://example.com", { waitUntil: "networkidle2" });
await page.pdf({ path: "page.pdf", format: "A4", printBackground: true });
} finally {
await browser.close();
}

networkidle2 waits until at most two network connections are open for 500 ms. Pages that keep polling may need a shorter wait or a specific element instead.

Common Puppeteer PDF problems

  • Missing backgrounds: printBackground defaults to false.
  • Different layout from the screen: page.pdf() uses print CSS and the paper width. Call page.emulateMediaType("screen") first to use the screen styles.
  • Lazy images are blank: images that load only when scrolled into view can stay blank in a PDF unless you scroll the page first.
  • Fonts and sandbox errors in containers: on a server you need the browser's system libraries and fonts, and often extra launch flags.

How do I do the same with one API call?

URLBooth PDF in Node.js
import { writeFile } from "node:fs/promises";
​
const response = await fetch("https://urlbooth.com/v1/screenshots", {
method: "POST",
headers: {
Authorization: `Bearer ${process.env.URLBOOTH_API_KEY}`,
"Content-Type": "application/json",
},
body: JSON.stringify({ url: "https://example.com", format: "pdf", paper: "a4" }),
signal: AbortSignal.timeout(60_000),
});
​
if (!response.ok) {
const { error } = await response.json();
throw new Error(`${error.code}: ${error.message}`);
}
​
await writeFile("page.pdf", Buffer.from(await response.arrayBuffer()));

Add paper (letter, a4, legal or a3) for printed pages, or leave it out for one continuous page at the device width. The page is loaded by URLBooth's browser, lazy images are scrolled into view for the continuous mode, and cookie banners are removed on a best-effort basis. See the options reference.

Puppeteer or the API?

Puppeteer yourselfURLBooth API
CostThe library is free. You pay for the servers or serverless time that run Chromium, and for the engineering time to run it.$20 per month (Pro) for up to 10,000 captures a day. There is no per-capture charge.
SpeedAbout 2 to 9 seconds per page in our tests, including starting the browser (3 sites, on a laptop).About 5 to 25 seconds in our tests (A4 pages: GitHub 10 s, Linear 25 s, Tailwind CSS 13 s; one continuous page: 5 to 26 s on 25 popular sites). The request travels over the internet and the browser starts for each capture.
MaintenanceYou keep the browser, its system libraries and fonts current, handle crashes, and limit concurrency. Cookie banners and ads are yours to remove.None to run. Cookie banners and ads are removed on a best-effort basis.
ControlFull: log in, click, run scripts, set headers, use any browser option.Public URLs only. Options: device, paper, printBackground, cleanup.
LimitsWhatever your machine can run.PDF up to 25 MB, 45 seconds per request, 60 requests a minute and 10 at once.

For the broader decision, read screenshot API vs Puppeteer and Playwright.

Where to go next