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

Use Puppeteer’s page.pdf() options: set displayHeaderFooter: true, then place recurring text in headerTemplate or footerTemplate. Reserve space with the matching top or bottom margin. Page numbers, total pages, the document title, URL, and print date are available through documented CSS classes inside those templates.

Working example: repeated header, footer, and page numbers

This complete Node.js example loads a page and writes a PDF with text on every page. The header and footer are HTML strings, not elements added to the page body.

const puppeteer = require('puppeteer');

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

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

  await page.pdf({
    path: 'output.pdf',
    format: 'A4',
    displayHeaderFooter: true,
    headerTemplate: `
      <div style="width:100%; font-size:9px; text-align:center;">
        Acme report — confidential
      </div>
    `,
    footerTemplate: `
      <div style="width:100%; font-size:9px; text-align:center;">
        <span class="pageNumber"></span> / <span class="totalPages"></span>
      </div>
    `,
    margin: {
      top: '60px',
      bottom: '60px',
      left: '20px',
      right: '20px'
    }
  });

  await browser.close();
})();

Install Puppeteer with npm install puppeteer, save the script, and run it with Node.js. The resulting output.pdf has the header and footer repeated on each printed page.

How the PDF header and footer options work

Enable the feature explicitly

displayHeaderFooter defaults to false. Without setting it to true, Puppeteer ignores the header and footer templates and produces a PDF without recurring text.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
#1 Best Overall
Sale
Epson EcoTank ET-2800 Wireless Color All-in-One Supertank Printer - Black
  • INNOVATIVE CARTRIDGE-FREE PRINTING — No more dealing with lots of tiny ink cartridges; With this wireless document and photo printer each ink bottle set is equivalent to about 90 individual cartridges²
  • LESS FREQUENT INK REPLACEMENT — Replacement ink bottles don't have to be changed nearly as often as ink cartridges¹; When you choose this combination printer, scanner and copier you can print up to 4,500 pages black/7,500 color³
  • COLOR PRINTING — Up to 2 years of ink in the box4 (and with every replacement ink set) for fewer out-of-ink frustrations
  • ZERO CARTRIDGE WASTE — By using an Epson EcoTank printer you can help reduce the amount of cartridge waste ending up in landfills
  • HOME PRINTER DESIGNED FOR RELIABILITY — The Epson EcoTank ET-2800 All-in-One Supertank Color Printer creates vivid, detailed prints and documents thanks to Micro Piezo Heat-Free Technology; Fire off 10 ISO pages per minute1 to easily finish large jobs

Put recurring text in the correct template

Use headerTemplate for content at the top of every page and footerTemplate for content at the bottom. Each value is an HTML fragment. A simple template can contain plain text; inline styles are the reliable way to control its width, alignment, and font size.

Keep the template valid and self-contained

Use valid HTML and close every element. Avoid depending on your page’s application stylesheet: the header and footer are rendered separately from the document body, so page-level CSS is not a dependable way to style them. Keep templates compact, and test the generated PDF with the Puppeteer and Chromium versions deployed by your application.

Adding page numbers and other dynamic values

Puppeteer replaces specific classes in a header or footer template with values for the current PDF:

Class Inserted value Example
pageNumber Current page number <span class="pageNumber"></span>
totalPages Total page count <span class="totalPages"></span>
date Formatted print date <span class="date"></span>
title Document title <span class="title"></span>
url Document location <span class="url"></span>

For a “Page 2 of 8” footer, combine the two page-related classes:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
footerTemplate: `
  <div style="width:100%; font-size:9px; text-align:right;">
    Page <span class="pageNumber"></span> of <span class="totalPages"></span>
  </div>
`

These values are injected by Puppeteer during PDF generation. Do not try to calculate the page count in JavaScript before calling page.pdf().

Margins: the fix for overlapping content

Header and footer templates do not automatically create enough room in the document’s content area. Allocate space with margin.top and margin.bottom. If the footer is 9px text with padding or multiple lines, a 60px bottom margin is a safer starting point than a minimal value.

  • Increase margin.top when body content touches or covers the header.
  • Increase margin.bottom when body content runs into the footer.
  • Keep left and right margins large enough for long URLs, titles, or labels.
  • Render a representative long document, not just a one-page test, because collisions may appear only after pagination.

Margins are measured as PDF layout space. A template that visually appears small can still need additional room when it wraps onto two lines.

Rank #2
Sale
Epson EcoTank Photo ET-8550 Wireless Wide-Format All-in-One Tank Printer
  • CARTRIDGE-FREE PRINTING — Print lab-quality photos, graphics and creative projects; Get vibrant colors and sharp text with Epson's high-accuracy printhead and Claria ET Premium 6-color inks
  • INK BOTTLES — Save on photos1 and creative projects with affordable in-house printing; All-in-one printer allows you to print 4" x 6" photos for about 4 cents each vs. 40 cents with traditional ink cartridges1
  • LESS FREQUENT INK REPLACEMENT — Replacement ink bottles don't have to be changed nearly as often as ink cartridges¹; Printer, scanner and copier lets you print up to 6,200 color pages³
  • PRINT FOR LONGER — Up to 2 years of ink in the box² (and with every replacement ink set) for fewer out-of-ink frustrations with this wireless printer
  • ZERO CARTRIDGE WASTE — Epson EcoTank printer helps reduce the amount of cartridge waste ending up in landfills; Cartridge-free printer uses high-yield ink bottles; Each replacement ink bottle set is equivalent to about 100 individual ink cartridges⁴

Why the PDF differs from the browser view

page.pdf() generates using the print CSS media type by default. Print-specific rules, hidden elements, different colors, and print page-break behavior can therefore change the result compared with a screen tab.

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

If the PDF should use the page’s screen styles, switch media before generating it:

await page.emulateMediaType('screen');
await page.pdf({
  path: 'output.pdf',
  displayHeaderFooter: true,
  headerTemplate: '<div>Repeated header</div>',
  margin: { top: '60px' }
});

Choose one media mode deliberately. Switching to screen changes the body’s CSS; it does not remove the need for margins or valid templates.

Header or footer: choosing the placement

Use a header when

  • The repeated text identifies the report, customer, or section before the reader starts each page.
  • The bottom of the page is reserved for page numbers, legal text, or a timestamp.

Use a footer when

  • Page numbering is the primary requirement.
  • The document has a strong top title and you want to preserve vertical space near it.

You can use both options in one call. Give each enough vertical margin, and keep the templates short so they do not compete with the document body.

Reliable generation workflow

  1. Launch Puppeteer and create a page.
  2. Navigate with an appropriate waitUntil condition, and wait separately for application data or fonts when necessary.
  3. Choose print CSS (the default) or call page.emulateMediaType('screen').
  4. Call page.pdf() with displayHeaderFooter, the required template, and matching margins.
  5. Open the PDF in an automated or manual check and verify the first, middle, and final pages.
  6. Close the browser even when generation fails by placing cleanup in a finally block in production code.

Troubleshooting common problems

The text does not appear

Check that displayHeaderFooter: true is inside the same options object passed to page.pdf(). Confirm that the template property is spelled exactly headerTemplate or footerTemplate, and that your process is writing the PDF you just generated rather than an older file.

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

The footer covers body content

Increase margin.bottom. A footer is not a normal flow element in the page body, so adding bottom padding to the website alone does not reserve PDF space. If the footer wraps, increase the margin again or shorten the text.

Page numbers are blank

Use the exact class names pageNumber and totalPages, including capitalization. Put the class on a real element such as span; do not write the class name as ordinary text.

Rank #3
HP Smart Tank 5000 Ink Tank Printer | 2 Years of Ink Included | All-in-One
  • 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
  • PREMIUM SUPPORT - Strong technical expertise to solve issues faster
  • THE LAST PRINTER YOU'LL EVER NEED. Enjoy years of refillable, cartridge-free printing.

The layout looks different from Chrome

Check print CSS first. If screen styling is required, call page.emulateMediaType('screen') before page.pdf(). Also check viewport-dependent rules, webfont loading, and print-specific page-break declarations.

The header is clipped or wraps unexpectedly

Make the template’s outer element width:100%, reduce the font size, shorten the text, or increase the relevant margin. Long injected URLs and titles are especially likely to wrap.

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

Dynamic page content is missing

PDF creation captures the rendered state at the time of the call. Wait for the selector that signals your data is ready, wait for fonts or images as needed, and only then call page.pdf(). Header/footer options cannot repair a page that was captured before its body finished rendering.

Performance, reliability, and operational notes

  • PDF rendering is a browser operation; reuse a controlled browser process where appropriate, but isolate pages and close them after jobs.
  • Set an application timeout around navigation and PDF generation. A page waiting forever for a network resource can otherwise hold a worker indefinitely.
  • Use deterministic input data when comparing output between deployments. Chromium updates can alter pagination, font metrics, and print layout.
  • Store or stream the PDF deliberately. The path option writes a file; without it, Puppeteer returns PDF data that your application can send to object storage or an HTTP response.
  • Test long documents, empty documents, pages with large tables, and documents whose final page has little content. Pagination edge cases often expose margin or wrapping problems.

Or skip the browser setup

If you need a screenshot or PDF of a URL rather than custom Puppeteer code, ScreenshotNeo provides a website screenshot API and MCP server. Its clean-shot flow accepts cookie and consent banners and removes more than 60 known consent platforms, newsletter popups, and chat widgets before capture; each cleanup step can be disabled. Bot checks or CAPTCHAs, blank pages, timeouts, failed loads, and cache hits are not billed, and response headers identify the page verdict and billing status.

For a direct request, see the ScreenshotNeo API documentation:

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

ScreenshotNeo also offers an MCP server with take_screenshot, get_page_info, and capture_pdf tools for Claude, Cursor, and other MCP clients. It supports full-page captures, CSS-selector element captures, device and viewport settings, dark mode, retina scale, PDF paper and margin options, custom CSS and JavaScript, clicks, waits, request blocking, headers, cookies, user agents, authorization, timezone, geolocation, transparent backgrounds, resizing, caching, signed links, asynchronous webhooks, bulk capture of up to 100 URLs per call, and a usage API.

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

The Free plan includes 1,000 shots per month with no card. Paid plans start at $5 for 3,000 shots; yearly billing provides two months free. Sign up for the free plan to try it without a card.

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

FAQ

Can I put arbitrary HTML in a header or footer?

Yes, provide an HTML template string, but keep it valid, compact, and styled within the template. Verify the result with the Puppeteer and Chromium versions you deploy.

Rank #4
Sale
NDYIN Portable Printers Wireless for Travel, N80 Bluetooth Thermal Printer
  • Wireless Bluetooth Printer: Portable thermal printer compatible with iPhone, Android phones, iPad and tablet computers via Bluetooth. For smartphones, please download the "Nada Print" App. You can also connect to laptops and computers for printing using a USB-C cable. (Note: Laptops and computers can only be connected via USB and require the installation of a driver first. Bluetooth connection is not supported.)
  • No-ink printing: Only supports US Letter and A4 size thermal paper.(Doesn't support regular paper) The no-ink portable thermal printer uses direct thermal technology, requiring no ink, toner or ribbons, making it environmentally friendly, cost-effective and time-saving. The thermal printer package comes with a roll of US Letter thermal printing paper. Note: When installing the paper, remember to switch the paper size switch on APP
  • Clear Print: NDYIN N80 portable thermal printer adopts high-definition printing technology, with a 203DPI resolution to provide you with clear printing results. This mobile printer is compatible with roll paper, folded paper and tattoo transfer paper, supporting printing from your mobile phone PDF, Word, pictures and web pages anytime and anywhere. It is recommended to use our NDYIN thermal paper to achieve good printing quality
  • Portable wireless printer for travel: The thermal printer is equipped with a built-in 1500mAh rechargeable battery, which can print 160 sheets of 8.5" x 11" thermal paper after being fully charged. It weighs only 1.5 pounds and is compact in size. This ink-free portable printer can be easily carried in a backpack or briefcase! It is perfect for business travel, cars, small offices, construction sites, schools and homes. You can print documents, contracts, invoices and boarding passes anytime and anywhere
  • The N80 thermal printer has a wide range of uses. The package includes the N80 printer, a roll of US Letter paper(7m/roll), a user manual, a guide card, a type-C soft cable and a type C adapter. Note: The charging adapter is not included. Special thermal paper is required for use; ordinary paper cannot be used. This ink-free portable thermal printer is suitable for various scenarios such as home, school, travel, office, and outdoor, meeting the printing needs of different groups of people. This tattoo template printer is also compatible with tattoo transfer paper, making it an ideal choice for tattoo art

Can repeated text be added by inserting an element into the page body?

That approach does not reliably repeat content at PDF page boundaries. Use headerTemplate or footerTemplate with displayHeaderFooter.

Can one PDF have both a repeated header and footer?

Yes. Set both template options in the same page.pdf() call and reserve top and bottom margins independently.

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

Frequently Asked Questions

Can I put arbitrary HTML in a header or footer?

Yes, provide an HTML template string, but keep it valid, compact, and styled within the template. Verify the result with the Puppeteer and Chromium versions you deploy.

Can repeated text be added by inserting an element into the page body?

That approach does not reliably repeat content at PDF page boundaries. Use headerTemplate or footerTemplate with displayHeaderFooter.

Can one PDF have both a repeated header and footer?

Yes. Set both template options in the same page.pdf() call and reserve top and bottom margins independently.

The Bottom Line

For recurring text on every Puppeteer PDF page, enable displayHeaderFooter, put the text in a valid header or footer template, use the documented injected classes for dynamic values, and reserve enough margin to keep the body clear.

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

Quick Recap

Bestseller No. 3
HP Smart Tank 5000 Ink Tank Printer | 2 Years of Ink Included | All-in-One
HP Smart Tank 5000 Ink Tank Printer | 2 Years of Ink Included | All-in-One
PREMIUM SUPPORT - Strong technical expertise to solve issues faster; THE LAST PRINTER YOU'LL EVER NEED. Enjoy years of refillable, cartridge-free printing.
$192.07

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.