User Agents

Web browsers and everyday web tech, explained

Remove Timestamps and URLs from Headless Chrome PDFs

· 4 min read · Browsers

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

To remove timestamps, dates, and page URLs from PDFs generated via Headless Chrome, you must disable the browser's default print headers and footers. This is achieved by setting the displayHeaderFooter property to false within the Page.printToPDF method or the corresponding Puppeteer/Playwright configuration. By default, Chrome treats a PDF export as a print job, which triggers the addition of metadata to the margins; disabling this setting ensures a clean, professional document containing only the web content.

What You'll Need

  • A Node.js environment installed on your machine.
  • A browser automation library such as Puppeteer or Playwright.
  • A basic understanding of JavaScript or TypeScript.
  • Access to the URL or HTML content you wish to convert to PDF.

Step-by-Step

  1. Initialize Your Automation Project: Begin by creating a new project directory and initializing it with npm init -y. Install your preferred library, for example, npm install puppeteer.
  2. Launch the Browser Instance: Create a script to launch Headless Chrome. Ensure you are running in headless mode (which is the default) to allow the automation to run without a visible window.
  3. Navigate to the Target Page: Use the page.goto('URL') method to load the website you want to capture. It is recommended to use waitUntil: 'networkidle0' to ensure all assets, including images and fonts, are fully loaded before the PDF is generated.
  4. Configure the PDF Options: When calling the page.pdf() function, pass an options object. The critical key here is displayHeaderFooter: false. This explicitly tells Chrome not to include the default page titles, dates, and URLs in the margins.
  5. Define Page Margins: To prevent content from being cut off or appearing too close to the edge, define the margin object within the PDF options. Setting top, bottom, left, and right to specific values (e.g., '1cm') ensures the layout remains clean.
  6. Execute the PDF Generation: Specify a file path using the path property in the options object. Run the script using node your-script.js.
  7. Verify the Output: Open the resulting PDF file. The top and bottom margins should now be completely blank, with no date, page number, or URL visible.

Common Mistakes

  • Ignoring the Default Value: Many users assume that setting margins to zero will remove the header and footer. This is incorrect; the displayHeaderFooter flag is a separate boolean that must be explicitly set to false.
  • Using the Wrong Wait Condition: Generating the PDF before the page has fully rendered can lead to missing elements or layout shifts, even if the headers are removed. Always wait for the network to be idle.
  • Confusing CSS Print Media with Browser Settings: While CSS @page rules can modify margins, they cannot override the browser's internal setting to print the URL and date. You must use the API configuration for this.

Tips

  • Custom Headers: If you actually want a custom header (like a company logo) instead of the default Chrome one, set displayHeaderFooter: true and provide the headerTemplate and footerTemplate properties. These accept HTML strings.
  • Prefer CSS for Layout: Use a CSS print stylesheet (@media print) to hide unnecessary UI elements like navigation bars or footers from the website itself before generating the PDF.
  • Use 'printBackground': Ensure printBackground: true is set in your options if you want colors and images to appear in the PDF; otherwise, Chrome may strip them to save ink.

Troubleshooting

If you have set displayHeaderFooter: false but still see unwanted text in the margins, check the following:

  • Check for Hard-Coded HTML: Ensure the text you see is actually a browser-generated header and not a part of the website's own HTML footer or header that is being captured in the print area.
  • Verify Library Version: Ensure you are using a modern version of Puppeteer or Playwright. Very old versions of the Chrome DevTools Protocol handled PDF generation differently.
  • Inspect CSS @page Rules: Check if the website has a @page CSS rule that is forcing specific content into the margins. You can override this by injecting custom CSS using page.addStyleTag().

Frequently Asked Questions

Can I remove the header but keep the page numbers?

Not using the default settings. To keep only page numbers, you must set displayHeaderFooter: true and provide a custom footerTemplate that includes the class.

Does this work in non-headless mode?

Yes, the page.pdf() method works regardless of whether the browser is visible, as it triggers the print-to-PDF functionality of the Chrome engine.

Why is my content shifting when I disable headers?

Removing headers changes the available printable area. Adjust your margin settings in the PDF options to ensure the content is centered and balanced.

Can I remove these timestamps from an existing PDF?

No, this method only works during the generation process. To remove them from an existing PDF, you would need a PDF editing tool or a library like pdf-lib to crop the pages.

Does this affect the metadata of the PDF file?

No, this only affects the visual elements printed on the page. The PDF's internal metadata (like the creator or creation date) remains unchanged.