October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk4 min

Convert HTML to PDF with Headers and Footers in Playwright

Create PDFs from HTML in Playwright with repeating headers and footers, page numbers, print margins, and the right Chromium and CSS settings.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use Playwright’s page.pdf() on a page loaded with your HTML, set displayHeaderFooter: true, and provide a headerTemplate, a footerTemplate, or both. The method returns a PDF buffer; add path to save a file. Playwright’s PDF export is Chromium-only, so use a Chromium browser for this workflow. Playwright Page API · PDF Export documentation

Generate the PDF with repeating header and footer content

This Node.js example assumes page is a Playwright Page that has already loaded the HTML you want to print. The templates use Playwright’s injected classes for the document title, date, page number, and page count.

As an Amazon Associate I earn from qualifying purchases.

const pdf = await page.pdf({
  path: 'output.pdf',
  format: 'A4',
  displayHeaderFooter: true,
  headerTemplate: `
    <div style="width:100%; font-size:9px; padding:0 12mm;">
      <span class="title"></span>
      <span style="float:right" class="date"></span>
    </div>`,
  footerTemplate: `
    <div style="width:100%; font-size:9px; text-align:center;">
      <span class="pageNumber"></span> / <span class="totalPages"></span>
    </div>`,
  margin: { top: '18mm', bottom: '18mm', left: '12mm', right: '12mm' },
  printBackground: true,
});

page.pdf() returns a PDF buffer, so you can use pdf directly if you do not need a file. The path option writes the PDF to disk as well. See the Page API reference for the current options in your installed Playwright version.

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

Understand the header and footer templates

Header and footer display is off unless you enable displayHeaderFooter. Templates are HTML strings; the documented injected classes are date, title, url, pageNumber, and totalPages. Use the same kind of markup for a footer as for a header.

#1 Best Overall
Brother DCP-L2640DW Wireless Compact Monochrome Multi-Function Printer, Copy, Scan, Duplex, Mobile Printing
  • BEST FOR SMALL BUSINESSES – Engineered for extraordinary productivity, the Brother DCP-L2640DW Monochrome (Black & White) 3-in-1 combines laser printer, scanner, copier in one compact footprint and delivers high-quality black & white prints
  • FAST PRINTER WITH EFFICIENT SCANNING – Produces documents quickly with print speeds up to 36 ppm(2) and scan speeds up to 23.6/7.9 ipm(3) (black/color). A 50-page auto document feeder(4) allows for convenient, time saving multi-page scanning and copying
  • FLEXIBLE CONNECTION OPTIONS – Easily navigate the changing demands of your business with secure multi-device connectivity via built-in dual-band wireless (2.4GHz / 5GHz) and Ethernet. Or connect locally to a single computer via USB interface
  • BROTHER MOBILE CONNECT APP – Print, scan, and manage your wireless printer anytime, from almost anywhere from your mobile device. Order Brother Genuine Supplies, track toner usage, and complete more work on-the-go(5)
  • CHOOSE BROTHER GENUINE TONER – When it’s time to replace your toner, be sure to choose Brother Genuine TN830 or TN830XL replacement toner. And with Refresh EZ Print Subscription Service, you’ll never worry about running out of toner again and you’ll enjoy savings of up to 50%(6) on Brother Genuine Toner. Get started with Refresh today with a Free Trial(1)
  • Template scripts are not evaluated.
  • Styles from the page do not carry into the templates. Give template elements their own inline styles or other self-contained styling.
  • Reserve room for the running header and footer with top and bottom margins. Paper margins default to zero, and unlabeled numeric margin values are interpreted as pixels.

Choose print media, paper size, and appearance

Print CSS or screen CSS

page.pdf() uses print CSS media by default. If the page should use its screen styles instead, call await page.emulateMedia({ media: 'screen' }) before generating the PDF. Print output can adjust colors; use -webkit-print-color-adjust in your document CSS when preserving exact colors matters.

Paper dimensions and orientation

Set a paper format such as A4 or Letter, or provide width and height. If you set format, it takes precedence over width and height. If the document has a CSS @page size that should control output, set preferCSSPageSize: true; otherwise Playwright scales content to fit the selected paper size. Set landscape: true for landscape orientation.

Rank #2
Brother HL-L2405W Wireless Compact Monochrome Laser Printer with Mobile Printing, Black & White Output | Includes Refresh Subscription Trial(1), Works with Alexa
  • BEST FOR HOMES & HOME OFFICES – Engineered for consistent, premium print quality, the Brother HL-L2405W Monochrome (Black & White) Laser Printer delivers sharp, crisp prints at an affordable price. Prints one-sided documents at speeds up to 30ppm(2)
  • COMPACT, CONNECTED PRINTER – Flexible connection options make this an ideal printer for home use and at-home offices. Securely connect to multiple devices with built-in dual-band wireless (2.4GHz/5GHz) or locally to a single computer via USB interface
  • BROTHER MOBILE CONNECT APP – Manage your printer remotely and print from your mobile device anytime, from almost anywhere. Order Brother Genuine Supplies, track toner usage, and complete more work on-the-go(3)
  • VERSATILE PAPER HANDLING – Enjoy seamless, reliable everyday printing with the 250-sheet paper tray(4) and a manual feed slot that enables printing on envelopes and specialty pape
  • BROTHER IS AT YOUR SIDE – Backed by Brother with a 1-year limited warranty and free online, call, or live chat support for the life of your printer

Backgrounds, scale, and page ranges

Background graphics are omitted by default; enable printBackground: true when they belong in the PDF. The default scale is 1, and the documented range is 0.1 to 2. Use pageRanges to export selected pages rather than the entire document. These options are documented in the Page API reference.

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.

Run PDF export in Chromium

Playwright’s dedicated PDF Export documentation states that PDF generation is Chromium-only. Do not expect this page.pdf() workflow to produce PDFs in Firefox or WebKit. Configure a Chromium browser for the export task and check the documentation for the Playwright version in your project, since API details can change over time.

Rank #3
Sale
Canon imageCLASS LBP6030w - Monochrome Single-Function Wireless Compact Wireless Laser Printer, 1 Year Limited Warranty, 19 PPM, White - Print Only
  • FAST PRINT SPEEDS: Print up to 19 pages per minute.
  • COMPACT DESIGN: Space-saving, compact design fits anywhere in your home, school or small office.
  • WIRELESS CONNECTIVITY: Print from almost anywhere in your workspace using your compatible mobile device.
  • PAPER CAPACITY: Up to 150 sheets.
  • SUSTAINABILITY: Uses less than 2 watts in Energy Saver mode.

Troubleshoot missing or incorrect output

  • No header or footer appears: make sure displayHeaderFooter: true is set and that the relevant template string is not empty.
  • The body overlaps the running content: increase the top or bottom margin to create space for the header or footer.
  • Template styling looks different from page styling: page styles are not visible inside templates. Put the needed styles directly in the template.
  • Template JavaScript has no effect: scripts in header and footer templates are not evaluated; provide the markup and values through supported template content instead.
  • Background graphics are missing: set printBackground: true.
  • The output uses unexpected page styles: PDF export defaults to print media. Emulate screen media before calling page.pdf() only if screen styling is intended.
  • The paper size differs from CSS: format overrides explicit width and height. To let CSS @page sizing take priority, set preferCSSPageSize: true.
  • PDF export is unavailable in your browser project: use Chromium; Playwright documents PDF generation as Chromium-only.

Or skip the browser setup

If your goal is a screenshot or PDF endpoint rather than controlling a Playwright page yourself, ScreenshotNeo is a website screenshot API and MCP server. It can return a PNG, JPEG, WebP, or PDF from one GET request. For a PDF, consult its options in the ScreenshotNeo documentation.

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

The one-call example saves an image; use the documented PDF options when you need a PDF. ScreenshotNeo accepts cookie or consent banners before capture and removes 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 cost nothing, and responses include X-Page-Verdict and X-Billed headers. Its MCP server provides take_screenshot, get_page_info, and capture_pdf for AI agents. The Free plan includes 1,000 shots per month with no card; paid plans start at $5 for 3,000 shots. Sign up for ScreenshotNeo’s free plan.

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

Frequently Asked Questions

Can Playwright put both a header and footer on each PDF page?

Yes. Enable displayHeaderFooter and provide both headerTemplate and footerTemplate.

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.
Rank #4
Brother HL-L2460DW Wireless Compact Monochrome Laser Printer with Duplex, Mobile Printing, Black & White Output | Includes Refresh Subscription Trial(1), Works with Alexa
  • BEST FOR HOME OFFICES & SMALL TEAMS – Engineered for consistent, premium print quality, the Brother HL-L2460DW Monochrome (Black & White) Laser Printer produces documents that are clear, crisp, and easy to review and share, all at an affordable price
  • COMPACT, CONNECTED, EXCEPTIONALLY EFFICIENT– Connect with built-in dual-band wireless (2.4GHz/5GHz), Ethernet, or to a single computer via USB interface. Prints at speeds up to 36ppm(2), plus automatic duplex printing saves time and reduces paper waste
  • BROTHER MOBILE CONNECT APP – Manage your wireless printer remotely and print from your mobile device anytime, from almost anywhere. Order Brother Genuine Supplies, track toner usage, and complete more work on-the-go(3)
  • VERSATILE PAPER HANDLING – Tackle high-volume black & white printing with the 250-sheet capacity paper tray.(4) The manual feed slot enables printing on envelopes and specialty paper
  • BROTHER IS AT YOUR SIDE – Backed by Brother with a 1-year limited warranty and free online, call, or live chat support for the life of your printer

Can I use Playwright’s PDF export in Firefox or WebKit?

No. Playwright’s PDF Export documentation specifies Chromium-only PDF generation.

Best Value
HP LaserJet M110w | Monochrome | Laser Printer | Fast Black-and-White Printing | Mobile Printing | Wireless Printing | Fast Printing
  • FROM AMERICA'S MOST TRUSTED PRINTER BRAND – Perfect for small teams printing professional-quality black & white documents and reports. Perfect for 1-3 people
  • WORLD'S SMALLEST LASER IN ITS CLASS – Precision laser printing that fits anywhere
  • FAST PRINT SPEEDS – Up to 21 black-and-white pages per minute single-sided
  • WIRELESS WITH SELF-RESET – Helps you stay connected
  • PRINT FROM ANY DEVICE – Wireless printing from any mobile device, PC or tablet. Works with Microsoft, Mac, AirPrint, Android, Chromebook and more

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 *

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
Outdated Drivers Are Slowing You DownFree scan - exact matches
PC Slower Than It Used to Be?Free scan - under a minute

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.