Halloween 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 NowHalloween DealsAmazon USOctober picks are moving - compare before checkoutAmazon US: current deals, useful picks and tech finds.See Picks×
FREEDOM251

Freedom251 › Blog › Node.js

How to Save a Webpage as PDF with Puppeteer and Wait for Fonts to Load

By Freedom251 Team · · 5 min read

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

Use Puppeteer’s page.pdf() after navigating to the page. Its waitForFonts option waits for document.fonts.ready and defaults to true in the documented API, so you can set it explicitly for clarity. Network-idle waits are separate: they do not replace the font-readiness check.

Generate a PDF after navigating to the page

Install Puppeteer in your Node.js project, then run this ES-module example. It checks the main navigation response, writes the PDF to page.pdf, and closes the browser even if an error occurs.

As an Amazon Associate I earn from qualifying purchases.

import puppeteer from 'puppeteer';

const browser = await puppeteer.launch();
try {
  const page = await browser.newPage();
  const response = await page.goto('https://example.com', {
    waitUntil: 'networkidle2',
  });

  if (response && !response.ok()) {
    throw new Error(`Navigation failed: ${response.status()}`);
  }

  await page.pdf({
    path: 'page.pdf',
    waitForFonts: true,
    format: 'A4',
    printBackground: true,
  });
} finally {
  await browser.close();
}

Puppeteer’s PDF generation guide demonstrates navigation followed by page.pdf(). The PDFOptions reference documents waitForFonts as true by default; writing it explicitly makes the intent visible and allows you to change it if necessary.

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

What the font wait does—and what it does not do

When waitForFonts is enabled, Puppeteer waits for the page’s document.fonts.ready promise before generating the PDF. This is the font-specific readiness condition. It does not guarantee that every element on a page has finished application-specific rendering or that a font will load successfully if the page itself cannot obtain it.

waitUntil: 'networkidle2' is a navigation condition used in the example; it is not the font wait. A page may have network activity unrelated to fonts, and a quiet network alone is not proof that the intended fonts are ready. Puppeteer documents an explicit Page.waitForNetworkIdle() method as well, for cases where you need to wait for network idleness.

Choose PDF media, paper, margins, and color behavior

Print CSS or screen CSS

page.pdf() uses print media by default, so print-specific CSS may affect layout and visibility. If the PDF should reflect screen-media styles instead, call page.emulateMediaType('screen') before page.pdf(). See the Page API reference.

Rank #2
Sale
HTML and CSS: Design and Build Websites
  • HTML CSS Design and Build Web Sites
  • Comes with secure packaging
  • It can be a gift option

Paper and layout

The PDF options support paper formats, custom dimensions, margins, orientation, page ranges, scale, output path, and timeout. The documented default paper format is Letter; the example selects A4 to avoid relying on that default. Consult the PDFOptions reference for the exact option names and accepted values for your installed Puppeteer version.

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

Backgrounds and print colors

printBackground defaults to false, so set it to true when the output needs page backgrounds. PDF output can also have colors adjusted for printing; the Page reference identifies -webkit-print-color-adjust as the CSS mechanism to request exact colors. Review the page’s print styles if its colors differ from the browser view.

Handle pages that need additional readiness conditions

Background tabs

Puppeteer notes that waiting for fonts may require activating a page with page.bringToFront() when it is in the background. If font waiting appears not to complete as expected in a multi-page workflow, bring the target page to the foreground before producing its PDF.

Application-specific rendering

If the page displays content only after a client-side state change, use page.waitForFunction() with an expression that reflects the page’s own ready condition. This is separate from waitForFonts; keep the font wait enabled when the PDF also depends on web fonts.

Rank #4
Sale
Web Design with HTML, CSS, JavaScript and jQuery Set
  • Brand: Wiley
  • Set of 2 Volumes
  • A handy two-book set that uniquely combines related technologies Highly visual format and accessible language makes these books highly effective learning tools Perfect for beginning web designers and front-end developers
await page.waitForFunction(() => {
  return document.querySelector('[data-report-ready="true"]') !== null;
});

await page.pdf({ path: 'page.pdf', waitForFonts: true });

The selector in this example is illustrative: replace it with a condition your application actually exposes. Puppeteer’s waitForFunction API supports page-context expressions, including asynchronous functions.

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.

Check navigation failures and PDF timeouts

page.goto() returns the main-resource response, or null in certain special cases. A valid HTTP error status such as 404 or 500 may not itself throw, so check the returned response when a successful page load is required. The Page.goto reference also notes that headless shell does not support PDF navigation; do not treat that runtime as equivalent to generating a PDF from a page.

The documented PDF API timeout default is 30,000 ms, and it can be configured through page default timeout settings. If font loading or PDF generation exceeds the timeout, first determine whether the page is still loading resources or whether its own rendering condition has not occurred; increase the timeout only when a longer wait is appropriate.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Troubleshoot common output problems

  • Fonts look like fallbacks: Keep waitForFonts: true. Verify that the page can load the font and, if the page is in the background, try page.bringToFront().
  • The page never reaches network idle: A site may continue making requests. Network idleness and font readiness are distinct; choose a suitable navigation condition and rely on the font-specific option for fonts.
  • The PDF omits backgrounds: Set printBackground: true.
  • PDF layout differs from the browser: Check print-media CSS. Call page.emulateMediaType('screen') first only if screen styling is the intended output.
  • The script continues despite an HTTP error: Inspect the navigation response and handle non-success status codes explicitly.
  • PDF generation times out: Check for delayed resources or application readiness conditions, then adjust the page timeout if the longer operation is expected.

Or skip the browser setup

If you need a screenshot or PDF without running Puppeteer yourself, ScreenshotNeo is a website screenshot API and MCP server for developers. Its one-call API can return a PDF:

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

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

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

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

Frequently Asked Questions

Does Puppeteer wait for web fonts before creating a PDF by default?

Yes. The documented PDFOptions API sets waitForFonts to true by default; set it explicitly if you want that behavior to be clear in your code.

Does network idle mean the fonts have loaded?

No. Network idleness and document.fonts.ready are separate readiness conditions.

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.

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

Node.jsPDFPuppeteerweb automationWeb Fonts

Leave a Reply

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

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

Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
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.