Driver FixRecommendedSound, Wi-Fi or graphics acting up? Check drivers firstFind missing or outdated drivers fast.Check DriversHalloween DealsAmazon USHalloween week deals: useful picks before month-endAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan Now×
Skip to content
World desk5 min

How to Save Playwright Web Pages as PDFs or Full-Page Screenshots

Playwright uses separate APIs for PDFs and screenshots. Learn how to save either, control print or screen rendering, and resolve common capture issues.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use page.pdf() to save a web page as a paginated PDF; use page.screenshot() to save it as an image. They are separate Playwright APIs, so a full-page screenshot does not itself create a PDF. Playwright’s PDF output uses print CSS by default; switch to screen media first if you want the page’s screen styles.

Choose PDF or image output

What you need Playwright method What it creates
A printable document page.pdf() A paginated PDF, using print CSS media by default. Playwright Page API
A viewport or full-page capture page.screenshot() An image such as PNG, JPEG, or WebP. Use fullPage: true to capture the full scrollable page. Playwright Screenshots guide
Repeatable visual regression evidence Playwright Test’s expect(page).toHaveScreenshot() A screenshot assertion against a baseline image; it is not a PDF export. Playwright Visual comparisons guide

Set up a runnable Node.js example

The example below uses Playwright’s Node.js API. Install Playwright in your project and install its browser before running the script:

As an Amazon Associate I earn from qualifying purchases.

  1. Run npm init -y if the directory does not already have a package manifest.
  2. Run npm install playwright.
  3. Run npx playwright install chromium to install the browser used below.
  4. Save the script as save-page.js, then run node save-page.js.
const { chromium } = require('playwright');

(async () => {
  const browser = await chromium.launch();
  const page = await browser.newPage();

  try {
    await page.goto('https://example.com', { waitUntil: 'networkidle' });

    // Full scrollable page as a PNG image.
    await page.screenshot({ path: 'page.png', fullPage: true });

    // Paginated PDF, laid out using print CSS by default.
    await page.pdf({
      path: 'page.pdf',
      format: 'A4',
      printBackground: true
    });

    // For a PDF rendered with screen CSS instead, emulate screen media
    // before calling page.pdf().
    await page.emulateMedia({ media: 'screen' });
    await page.pdf({ path: 'page-screen.pdf', printBackground: true });
  } finally {
    await browser.close();
  }
})();

Replace https://example.com with the page you need. The script writes page.png, page.pdf, and page-screen.pdf to the current directory. The navigation wait shown is an example, not a guarantee that every site has finished rendering; pages with delayed or continuously active requests may need a more specific readiness condition.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Save a full-page screenshot as an image

To capture the entire scrollable document in one image, set fullPage: true:

await page.screenshot({ path: 'full-page.png', fullPage: true });

Without that option, the screenshot covers the current viewport. A full-page capture remains an image; it does not paginate the page or produce a PDF.

Choose image type and resolution

Playwright supports PNG, JPEG, and WebP screenshot output. The documented screenshot options also include clipping a region, setting JPEG or WebP quality, and choosing between CSS-pixel and device-pixel scale. With scale: 'css', the output uses one image pixel per CSS pixel; scale: 'device' uses device pixels and may create a larger image on a high-DPI display. Check the installed version’s Page API reference for the accepted options and defaults.

Save a web page as a PDF

Call page.pdf() after navigation. Supplying path writes the PDF to disk; without a path, the method returns a PDF buffer that your application can save or process.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
await page.pdf({ path: 'page.pdf' });

Set page size, margins, and page ranges

Use format for a named paper size such as 'A4', or set width and height directly. Unlabeled width and height values are interpreted as pixels; supported units include px, in, cm, and mm. The PDF options also include margins, page ranges, scale, and preferCSSPageSize.

await page.pdf({
  path: 'selected-pages.pdf',
  format: 'A4',
  margin: { top: '12mm', right: '12mm', bottom: '12mm', left: '12mm' },
  pageRanges: '1-3',
  printBackground: true
});

Set preferCSSPageSize: true when the page’s CSS @page size should take priority over a size set through format, width, or height. Consult the Page API documentation for the complete PDF option list.

Control PDF media and colors

PDF generation uses print CSS media by default. Print styles can change layout, omit elements, or use different colors from the screen version. To render with screen media, call page.emulateMedia({ media: 'screen' }) before page.pdf():

await page.emulateMedia({ media: 'screen' });
await page.pdf({ path: 'screen-style.pdf', printBackground: true });

These are separate controls: printBackground: true enables background graphics, while the CSS property -webkit-print-color-adjust can be used to force exact colors when print color adjustment changes them. Inspect the resulting PDF, especially if the site has an intentional print stylesheet.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Make visual regression screenshots more stable

Screenshot output can vary with the host operating system, browser version, browser settings, hardware, power source, and headless mode. Playwright recommends generating and comparing visual baselines in the same environment. Its toHaveScreenshot() assertion waits for consecutive screenshots to match before comparing with the expectation; it does not turn a page into a PDF. See the Visual comparisons guide for the assertion workflow.

  • Keep the browser version and host environment aligned between baseline generation and comparison.
  • Use the same relevant browser settings and capture mode for both runs.
  • When a test changes unexpectedly, check for environment differences before treating the image change as a product regression.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Troubleshoot common output problems

Symptom Likely cause What to do
The PDF does not look like the screen page. page.pdf() renders with print CSS by default. Call page.emulateMedia({ media: 'screen' }) before generating the PDF, then inspect the output.
PDF backgrounds or colors are missing or changed. Background graphics are not enabled, or print color adjustment and print CSS affect rendering. Set printBackground: true for background graphics. Check print styles and consider -webkit-print-color-adjust when exact colors are needed.
The screenshot contains only the visible portion. The screenshot call did not request a full-page capture. Set fullPage: true in page.screenshot().
A visual screenshot test differs across machines. The rendering environment may differ in OS, browser version, settings, hardware, power source, or headless mode. Align the baseline and comparison environments, then review the changed image.
The PDF uses an unexpected paper size. The page’s CSS @page size may take precedence, or the configured format and dimensions may not match the intended output. Review format, width, and height; use preferCSSPageSize: true only when the CSS page size should take priority.

Or skip the browser setup

If you need a screenshot or PDF without launching Playwright and managing a browser in your own code, ScreenshotNeo provides a website screenshot API and MCP server. One GET request can return a PNG, JPEG, WebP, or PDF; its options include full-page capture and PDF paper settings.

For example, save a PDF of a page with cURL:

curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://example.com -o page.pdf

See the ScreenshotNeo documentation for API parameters and output options. ScreenshotNeo accepts cookie or consent banners and removes more than 60 known consent platforms, newsletter popups, and chat widgets before capture; each of those steps can be turned off. Bot checks or CAPTCHAs, blank pages, timeouts, failed loads, and cache hits are not billed, and responses identify the page verdict and billing status in headers. Its MCP server offers take_screenshot, get_page_info, and capture_pdf for AI agents and other MCP clients. The Free plan includes 1,000 shots per month with no card; paid plans start at $5 for 3,000 shots.

Sign up for 1,000 free screenshots a month, with no card required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Frequently Asked Questions

Does page.screenshot() save a PDF?

No. It creates an image; use page.pdf() for a paginated PDF.

Can I keep a PDF’s output in memory instead of saving a file?

Yes. Omit the path option from page.pdf() to receive a PDF buffer.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.