Playwright PDF: save a web page as PDF

By Mayur Gadakh · Published · Last updated

To save a page as a PDF with Playwright, open it in Chromium and call page.pdf() with a paper format and printBackground: true. The code below runs as written. The URLBooth API does the same with one request, and can also return the whole page as one continuous sheet.

How do I generate a PDF with Playwright?

Install with npm i playwright and npx playwright install chromium, then run:

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

The result is an A4 PDF. Playwright can create PDFs only in headless Chromium; page.pdf() is not available in Firefox or WebKit.

Why does my Playwright PDF look different from the page?

  • Backgrounds are missing. printBackground defaults to false, so background colours and images are left out. Set it to true.
  • It uses print styles. The PDF is laid out with the page's print CSS, which often hides menus. Call page.emulateMedia({ media: "screen" }) before page.pdf() to use the screen styles instead.
  • The layout reflows. The page is laid out at the paper width (about 794 pixels for A4), so a desktop site often switches to its tablet layout.
  • Elements split across pages. Paper pages break wherever the content runs out. CSS such as break-inside: avoid helps, but it is per-site work.

How do I get the same PDF with one API call?

Send format: "pdf" and a paper size. This Node.js script (Node 18 or later) saves an A4 PDF:

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()));

Leave out paper to get one continuous page as wide as the device and as tall as the page, which keeps the desktop layout with no page breaks. The same request with curl:

URLBooth PDF with curl
curl --fail-with-body "https://urlbooth.com/v1/screenshots" \
-H "Authorization: Bearer $URLBOOTH_API_KEY" \
-H "Content-Type: application/json" \
-d '{"url":"https://example.com","format":"pdf"}' \
-o page.pdf

Pages taller than 40,000 pixels are refused in continuous mode; choose a paper size for those. A PDF is limited to 25 MB. See the options reference, or use the free save a webpage as PDF tool without code.

Playwright or the API for PDFs?

Playwright 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.

Playwright is the better fit for pages behind a login, for documents you generate from your own HTML, and for custom headers and footers. The API only opens public URLs.

Where to go next