# Playwright PDF: save a web page as PDF

> Runnable page.pdf() code for Playwright: paper size, backgrounds and page breaks, why the PDF differs from the screen, and the one-request alternative.

By [Mayur Gadakh](https://urlbooth.com/about) · Published 2026-11-23 · Last updated 2026-11-23

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:

```js
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

```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

```bash
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](https://urlbooth.com/docs#options), or use the free [save a webpage as PDF tool](https://urlbooth.com/tools/save-webpage-as-pdf) without code.

## Playwright or the API for PDFs?

|  | Playwright yourself | URLBooth API |
| --- | --- | --- |
| Cost | The 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. |
| Speed | About 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. |
| Maintenance | You 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. |
| Control | Full: log in, click, run scripts, set headers, use any browser option. | Public URLs only. Options: device, paper, printBackground, cleanup. |
| Limits | Whatever 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

- [Puppeteer PDF](https://urlbooth.com/guides/puppeteer-pdf)
- [HTML to PDF API: convert a URL to PDF](https://urlbooth.com/guides/html-to-pdf-api)
- [Playwright screenshot](https://urlbooth.com/guides/playwright-screenshot)

## Related guides

- [wkhtmltopdf alternatives](https://urlbooth.com/guides/wkhtmltopdf-alternatives)

  Why wkhtmltopdf is a dead end, how headless Chrome, Puppeteer, Gotenberg, WeasyPrint and Prince compare, and a flag-by-flag port.
- [Puppeteer PDF: generate a PDF from a web page](https://urlbooth.com/guides/puppeteer-pdf)

  page.pdf() code for Node.js, paper sizes, backgrounds, common problems and fixes, and the same PDF from one API request.
- [HTML to PDF API: convert a URL to PDF](https://urlbooth.com/guides/html-to-pdf-api)

  curl and Node.js examples, paper sizes or one continuous page, errors and limits. It takes a public URL, not raw HTML.
- [Playwright screenshot: full page and viewport](https://urlbooth.com/guides/playwright-screenshot)

  Runnable Playwright code, the screenshot options that matter, common gotchas, and the same image from one API call.

---

Source: https://urlbooth.com/guides/playwright-pdf
