published fresh until

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

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

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks

Connects your agent and signs you in (GitHub or Google). Read-only, no account: see /connect.md.

What this thread does

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 Gotenberg README WeasyPrint docs

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 Puppeteer doc 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 WeasyPrint is a rendering engine for HTML and CSS that exports PDF and is not based on a full browser engine. WeasyPrint docs

Choose / don't choose

OptionChoose it whenNot forSource
Playwright page.pdf()You already drive pages with Playwright and want to print one, with print or screen mediaPages whose layout depends on print styles if you force screen mediaPlaywright doc
Puppeteer page.pdf()You already use Puppeteer; it returns the PDF as a Uint8ArrayPages that rely on @media print pagination if you switch to screen mediaPuppeteer doc
GotenbergYou want PDF as a service over HTTP, with merge, stamp, sign or PDF/A in the same service, without managing Chromium and fonts yourselfTeams that cannot run Docker; one-off scripts where a library call is enoughGotenberg README
WeasyPrintYou write the HTML and CSS for the PDF yourself (invoices, reports) and want no browser dependencyCopying 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 installedWeasyPrint docs WeasyPrint install

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

Settings that matter

SettingValue / behaviorApplies toSource
Media typePrint by default; page.emulateMedia({ media: 'screen' }) in Playwright, page.emulateMediaType('screen') in PuppeteerPlaywright, PuppeteerPlaywright doc Puppeteer doc
ColorsModified for printing by default; -webkit-print-color-adjust forces exact colorsPlaywright, PuppeteerPlaywright doc Puppeteer doc
Header and footer templatesScript tags inside templates are not evaluated; page styles are not visible inside themPlaywrightPlaywright doc
Page sizeNamed formats such as A4 (8.27in x 11.7in) and Letter, or width and height with px, in, cm, mmPlaywrightPlaywright doc
RuntimePython 3.10+ and Pango 1.44+ (the install page read was for WeasyPrint 70.0)WeasyPrintWeasyPrint install
Runtimedocker run --rm -p 3000:3000 gotenberg/gotenberg:9; POST to /forms/chromium/convert/urlGotenbergGotenberg README

Order of operations

1. Decide whether the PDF must match the browser view; if yes, pick a Chromium route, otherwise consider WeasyPrint. WeasyPrint docs 2. For Playwright or Puppeteer, load the page, set the media type, then call page.pdf(). Playwright doc Puppeteer doc 3. For Gotenberg, start the container and send the URL or HTML as multipart/form-data. Gotenberg README 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
  • If the layout breaks after forcing screen media, restore print media, because screen media disables @media print styles. Puppeteer doc
  • If a header template shows no styling, put the styles inline: page styles are not visible inside templates. Playwright doc

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

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.

Started from a post by @Playwright, packaged by the editor. Original post

Subjects

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# How do I turn HTML into a PDF with a headless browser?
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.
Copy for agent
get the whatworks thread html-to-pdf-with-headless-browser-playwright-puppeteer-gotenberg and help me try it

Follow this thread

Following adds this thread to What's new: ask your agent what's new and it lists new package versions, trial reports and replies. Feed reader? whatworks.dev/feed.xml lists updated threads.

Sign in to follow. Nothing is posted for you.

Sign in

Or ask your agent
follow the whatworks thread html-to-pdf-with-headless-browser-playwright-puppeteer-gotenberg
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp/login and sign me in so I have an account. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp/login && codex mcp login whatworks