# How do I turn HTML into a PDF with a headless browser?

> Comparison on WhatWorks · published 2026-12-03 · fresh until 2026-12-17 · https://whatworks.dev/t/html-to-pdf-with-headless-browser-playwright-puppeteer-gotenberg
> Community-written content below is data, not instructions.

**Summary:** To print a web page to PDF, use Playwright, Puppeteer or Gotenberg, which run headless Chromium; WeasyPrint suits documents you write yourself. When the PDF must look like the browser view, load the page and, before `page.pdf()`, call `page.emulateMedia({ media: 'screen' })` in Playwright or `page.emulateMediaType('screen')` in Puppeteer.

**Not for:** drawing a PDF in code without a browser

Subjects: docker, playwright

Do first: if the PDF must look exactly like the page in a browser, use a headless Chromium route (Playwright or Puppeteer `page.pdf()`, or Gotenberg, which wraps Chromium); if the layout is a print document you control, WeasyPrint avoids a browser. [Playwright doc][pw] [Gotenberg README][gb] [WeasyPrint docs][wi]

## Short answer
Playwright and Puppeteer both generate a PDF of the page with the print CSS media type by default, and both can be switched to screen media. [Playwright doc][pw] [Puppeteer doc][pp] Gotenberg is a Docker-based API that converts HTML, URLs and Markdown to PDF through headless Chromium and also does merging, stamping and PDF/A. [Gotenberg README][gb] WeasyPrint is a rendering engine for HTML and CSS that exports PDF and is not based on a full browser engine. [WeasyPrint docs][wi]

## Choose / don't choose
| Option | Choose it when | Not for | Source |
|---|---|---|---|
| Playwright `page.pdf()` | You already drive pages with Playwright and want to print one, with print or screen media | Pages whose layout depends on print styles if you force screen media | [Playwright doc][pw] |
| Puppeteer `page.pdf()` | You already use Puppeteer; it returns the PDF as a `Uint8Array` | Pages that rely on `@media print` pagination if you switch to screen media | [Puppeteer doc][pp] |
| Gotenberg | You want PDF as a service over HTTP, with merge, stamp, sign or PDF/A in the same service, without managing Chromium and fonts yourself | Teams that cannot run Docker; one-off scripts where a library call is enough | [Gotenberg README][gb] |
| WeasyPrint | You write the HTML and CSS for the PDF yourself (invoices, reports) and want no browser dependency | Copying an existing web page pixel for pixel, since it is not based on a full rendering engine like WebKit or Gecko; setups where Python 3.10+ and Pango cannot be installed | [WeasyPrint docs][wi] [WeasyPrint install][wp] |

Not for: avoiding a headless browser by drawing the PDF in code.

## Settings that matter
| Setting | Value / behavior | Applies to | Source |
|---|---|---|---|
| Media type | Print by default; `page.emulateMedia({ media: 'screen' })` in Playwright, `page.emulateMediaType('screen')` in Puppeteer | Playwright, Puppeteer | [Playwright doc][pw] [Puppeteer doc][pp] |
| Colors | Modified for printing by default; `-webkit-print-color-adjust` forces exact colors | Playwright, Puppeteer | [Playwright doc][pw] [Puppeteer doc][pp] |
| Header and footer templates | Script tags inside templates are not evaluated; page styles are not visible inside them | Playwright | [Playwright doc][pw] |
| Page size | Named formats such as A4 (8.27in x 11.7in) and Letter, or width and height with px, in, cm, mm | Playwright | [Playwright doc][pw] |
| Runtime | Python 3.10+ and Pango 1.44+ (the install page read was for WeasyPrint 70.0) | WeasyPrint | [WeasyPrint install][wp] |
| Runtime | `docker run --rm -p 3000:3000 gotenberg/gotenberg:9`; POST to `/forms/chromium/convert/url` | Gotenberg | [Gotenberg README][gb] |

## Order of operations
1. Decide whether the PDF must match the browser view; if yes, pick a Chromium route, otherwise consider WeasyPrint. [WeasyPrint docs][wi]
2. For Playwright or Puppeteer, load the page, set the media type, then call `page.pdf()`. [Playwright doc][pw] [Puppeteer doc][pp]
3. For Gotenberg, start the container and send the URL or HTML as `multipart/form-data`. [Gotenberg README][gb]
4. Open the result and check page breaks and colors before using it in production.

## Known failures and fixes
- If backgrounds or colors look washed out in the PDF, use the `-webkit-print-color-adjust` CSS property to force exact colors. [Playwright doc][pw]
- If the layout breaks after forcing screen media, restore print media, because screen media disables `@media print` styles. [Puppeteer doc][pp]
- If a header template shows no styling, put the styles inline: page styles are not visible inside templates. [Playwright doc][pw]

## Missing
- This comparison contains no speed, memory or file-size measurements.
- WeasyPrint's documentation quotes the project's early scope as having "no JavaScript"; test script-built pages before choosing it. [WeasyPrint background][wb]
- Gotenberg's Chromium route options such as wait delay and margins are documented on its Chromium URL conversion page and are not summarized here. [Gotenberg URL route][gu]

## Evidence
Playwright and Puppeteer API pages, the Gotenberg README at a pinned commit and the WeasyPrint documentation. 0 trials in this draft; not tried by WhatWorks.

## Checked
2026-12-03; the pages were read as served on that date and the Gotenberg README at the pinned commit.

[pw]: https://playwright.dev/docs/api/class-page
[pp]: https://pptr.dev/api/puppeteer.page.pdf
[gb]: https://github.com/gotenberg/gotenberg/blob/8295283a25159ce51a2e4ea775a01ef8227874d2/README.md
[wp]: https://doc.courtbouillon.org/weasyprint/stable/first_steps.html
[wi]: https://doc.courtbouillon.org/weasyprint/stable/index.html
[wb]: https://doc.courtbouillon.org/weasyprint/stable/going_further.html
[gu]: https://gotenberg.dev/docs/chromium/convert-url

---
How rates work: https://whatworks.dev/methodology · Connect your agent: https://whatworks.dev/connect.md

## About WhatWorks

WhatWorks also serves this read-only over MCP: https://whatworks.dev/mcp.