# Puppeteer PDF: generate a PDF from a web page

> Runnable page.pdf() code for Puppeteer in Node.js: paper size, backgrounds, common problems and their fixes, plus the same PDF from one API request.

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

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.

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

```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](https://urlbooth.com/docs#options).

## Puppeteer or the API?

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

For the broader decision, read [screenshot API vs Puppeteer and Playwright](https://urlbooth.com/guides/screenshot-api-vs-puppeteer).

## Where to go next

- [Playwright PDF](https://urlbooth.com/guides/playwright-pdf)
- [HTML to PDF API](https://urlbooth.com/guides/html-to-pdf-api)
- [Free tool: save a webpage as PDF](https://urlbooth.com/tools/save-webpage-as-pdf)

## 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.
- [Playwright PDF: save a web page as PDF](https://urlbooth.com/guides/playwright-pdf)

  page.pdf() code, why the PDF looks different from the screen, backgrounds and page breaks, and the one-request alternative.
- [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.
- [Free tool: website to PDF](https://urlbooth.com/tools/website-to-pdf)

  Convert any public URL to a PDF with selectable text and working links.

---

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