Use Playwright’s page.pdf() after navigating to the page. It creates a PDF using print CSS by default; choose a paper format and margins, enable backgrounds if needed, and wait for your site’s content to be ready before capturing.
Generate a website PDF with Playwright
This JavaScript example opens a Chromium page, navigates to a URL, and saves an A4 PDF. Playwright’s Pages guide documents page navigation, and its Page API documents PDF generation: Pages and Page API.
const { chromium } = require('playwright');
(async () => {
const browser = await chromium.launch();
try {
const page = await browser.newPage();
await page.goto('https://example.com');
await page.pdf({ path: 'page.pdf', format: 'A4' });
} finally {
await browser.close();
}
})();
Install Playwright and its Chromium browser binary before running the script. From a project directory, a basic setup is:
npm install playwright
npx playwright install chromium
The browser binary must correspond to the Playwright version in your project. The official browser installation guide explains installation and recommends updating Playwright and reinstalling browsers when versions change.
Do these 3 things before closing this tab:
1Scan for outdated or missing drivers - takes under a minute2Clear out junk files and repair common Windows errors3Fix the driver behind crashes, sound loss and screen glitches#1 Best Overall
- Scanner type: Document
- Connectivity technology: USB
- With Auto Scan Mode, the scanner automatically detects what you're scanning
- Digitize documents and images
Wait for the right content before printing
A successful page.goto() does not guarantee that a modern application has finished rendering the content you want in the PDF. Choose a readiness condition that matches the site: for example, wait for a particular element or for an application-specific signal after client-side data has loaded. Do not assume that a fixed delay works for every page.
await page.goto('https://example.com', { waitUntil: 'networkidle' });
await page.locator('main').waitFor();
await page.pdf({ path: 'page.pdf', format: 'A4' });
Use a selector that actually signals print readiness on the target site. Network activity can persist or settle before the relevant content appears, so a site-specific condition may be more dependable than relying on navigation alone.
Choose print or screen CSS
page.pdf() uses print CSS media. Playwright’s official Page API documentation states, “page.pdf() generates a pdf of the page with print css media.” Print styles can hide navigation, rearrange columns, or replace colors and images, so inspect the output if it differs from the screen version.
Rank #2
- FAST DOCUMENT SCANNING — Document scanner with feeder allows you to speed through stacks with a 50-sheet Auto Document Feeder (ADF); Efficient office scanner to help you scan more productively
- INTUITIVE, HIGH-SPEED SOFTWARE — Quickly scan with this desktop document scanner; Epson ScanSmart Software lets you easily preview scans, email files, upload to the cloud, and more; Plus, automatic file naming saves even more time
- SEAMLESS INTEGRATION — Easily incorporate your data into most document management software with the included TWAIN driver; Office document scanner integrates seamlessly with business workflows
- EASY SHARING — Duplex scanner allows you to scan straight to email or popular cloud storage2 services like Dropbox, Evernote, Google Drive, and OneDrive for simple storage and sharing
- SIMPLE FILE MANAGEMENT — Scanner allows the creation of searchable PDFs with Optical Character Recognition (OCR) and convert scans to editable Word or Excel files effortlessly; Designed for home and office document scanning
To render using screen CSS instead, emulate screen media before calling page.pdf():
The Tool Desk
Outbyte Driver Updater FREEScan for outdated or missing drivers - takes under a minuteDriver Scan →Outbyte PC Repair FREERepair Windows errors before they cause bigger problemsFix Now →await page.emulateMedia({ media: 'screen' });
await page.pdf({ path: 'page.pdf', format: 'A4' });
Use print media for a document intended to print or for a site with deliberate print styles. Use screen media when the PDF should resemble the on-screen layout; the choice changes which CSS rules apply.
Set paper size, margins, and backgrounds
The PDF options determine how page content maps onto paper. The API defaults to Letter paper with no paper margins. format takes precedence over width and height; dimensions and margins accept units such as px, in, cm, and mm. An unlabeled numeric value is interpreted as pixels. See the Page API options for the full current API reference.
Rank #3
- SET IT UP ONCE AND PRINT WITH CONFIDENCE. No complicated maintenance. Just easy, reliable printing you can count on.
- INK FOR YEARS. NOT MONTHS. Up to 2 years of ink included. Get thousands of pages of cartridge-free printing. More pages, less hassle
- KEEPS PRINTING WELL AFTER COMPETITORS HAVE QUIT. No complex maintenance. Sharper text, richer colors.[2] Only with HP Smart Tank
- INNOVATION YOU CAN TRUST. Cartridge-free printing that keeps you printing longer with fewer interruptions, powered by the trusted HP innovation you rely on today
- PREMIUM SUPPORT - Strong technical expertise to solve issues faster.
await page.pdf({
path: 'page.pdf',
format: 'A4',
margin: { top: '15mm', right: '12mm', bottom: '15mm', left: '12mm' },
printBackground: true,
preferCSSPageSize: true
});
formatselects a paper preset such asA4orLetter.marginsets paper margins; margins are zero by default.printBackground: trueincludes background graphics, which are off by default.preferCSSPageSize: truegives a page’s CSS@pagesize priority. Without it, content is scaled to fit the selected paper size.
Playwright notes that PDF generation adjusts colors for printing by default. Its API documentation identifies the CSS property -webkit-print-color-adjust as a way to force exact colors. Check the PDF for the target site, since print styles and color adjustment affect the result.
Choose an output path or return a PDF buffer
Pass path to save the PDF to disk. A relative path is resolved from the process’s current working directory. If you omit path, page.pdf() returns a PDF buffer, which is useful when another part of your application will upload, store, or send the result.
const pdfBuffer = await page.pdf({ format: 'A4' });
// Use pdfBuffer with your application's storage or response code.
Useful PDF options
| Option | What it controls |
|---|---|
format |
Paper preset, such as A4 or Letter; takes precedence over width and height. |
width, height |
Paper dimensions when not superseded by format; values accept CSS-style units. |
margin |
Paper margins; defaults to none. |
preferCSSPageSize |
Whether CSS @page sizing takes priority over the selected paper size. |
printBackground |
Whether to include background graphics; defaults to false. |
pageRanges |
Pages to include, for example 1-5, 8. |
scale |
Output scale; defaults to 1 and accepts values from 0.1 to 2. |
displayHeaderFooter, headerTemplate, footerTemplate |
Whether to show header and footer templates. Template scripts are not evaluated, and page styles are not visible inside the templates. |
tagged, outline |
PDF tagging and outline options, documented as added in Playwright v1.42; both default to false. |
path |
File path for saving the PDF; omit it to receive a PDF buffer. |
Browser support and version setup
Playwright supports Chromium, WebKit, and Firefox for browser automation, with browser binaries tied to Playwright versions. Install the browser you intend to use with the Playwright CLI, and reinstall after updating Playwright if needed; consult the official browser guide.
Rank #4
- FAST SPEEDS - Scans color and black and white documents a blazing speed up to 16ppm (1). Color scanning won’t slow you down as the color scan speed is the same as the black and white scan speed.
- ULTRA COMPACT – At less than 1 foot in length and only about 1. 5lbs in weight you can fit this device virtually anywhere (a bag, a purse, even a pocket).
- READY WHENEVER YOU ARE – The DS-640 mobile scanner is powered via an included micro USB 3. 0 cable allowing you to use it even where there is no outlet available. Plug it into you PC or laptop and you are ready to scan.
- WORKS YOUR WAY – Use the Brother free iPrint&Scan desktop app for scanning to multiple “Scan-to” destinations like PC, Network, cloud services, Email and OCR. (2) Supports Windows, Mac and Linux and TWAIN/WIA for PC/ICA for Mac/SANE drivers. (3)
- OPTIMIZE IMAGES AND TEXT – Automatic color detection/adjustment, image rotation (PC only), bleed through prevention/background removal, text enhancement, color drop to enhance scans. Software suite includes document management and OCR software. (4)
For Chromium automation through Chrome or Edge, Playwright says it works best with the Chromium version bundled with Playwright and does not guarantee compatibility with other browser versions. That guidance is in the BrowserType API documentation. Separately, Playwright’s MCP PDF export page says that PDF generation in that MCP tool is Chromium-only; that limitation applies to the MCP export feature, not to every Playwright API or language binding.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Troubleshoot common PDF problems
- The PDF uses the wrong layout:
page.pdf()applies print media by default. Check the site’s print CSS or callpage.emulateMedia({ media: 'screen' })before generating the PDF. - Background colors or images are missing: Set
printBackground: true. For color differences, review print color adjustment and the target page’s-webkit-print-color-adjuststyling. - Content is blank or incomplete: Navigation completion may precede application rendering. Wait for a meaningful selector or the site’s own readiness signal before calling
page.pdf(). - The browser executable is missing or incompatible: Install the browser binary for your Playwright version with
npx playwright install(ornpx playwright install chromiumfor this example). After upgrading Playwright, reinstall the browsers as described in the browser guide. - The PDF has unexpected page dimensions or scaling: Check whether
formatoverrideswidthandheight, and whetherpreferCSSPageSizeshould let the page’s@pagerule control sizing. - The saved file is not where expected: A relative
pathresolves from the current working directory. Use an explicit path or omit it and handle the returned buffer.
Or skip the browser setup
ScreenshotNeo can return a PDF with one GET request, without setting up Playwright locally. Its capture process accepts cookie and consent banners like a visitor and removes more than 60 known consent platforms, newsletter popups, and chat widgets; each step can be turned off. Only clean shots are billed: bot checks or CAPTCHAs, blank pages, timeouts, failed loads, and cache hits cost nothing, and responses identify the page verdict and billing status in headers. It also has an MCP server with take_screenshot, get_page_info, and capture_pdf tools for AI agents.
curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.pdf
See the ScreenshotNeo API documentation for PDF and capture options. The free plan includes 1,000 shots a month with no card; paid plans start at $5 for 3,000 shots. Sign up for ScreenshotNeo and get 1,000 free screenshots a month with no card.
Frequently Asked Questions
Can Playwright save a webpage as a PDF without writing a file first?
Yes. Omit the path option; page.pdf() returns a PDF buffer.
Does Playwright’s MCP PDF export support every browser?
No. The official MCP PDF export page specifies Chromium-only support for that tool.
Quick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

