User Agents

Web browsers and everyday web tech, explained

Remove Headers, Footers & Gray Backgrounds from Headless Chrome PDFs

· 6 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

When you generate a PDF with headless Chrome using the --print-to-pdf flag or the Puppeteer/Playwright API, the output often includes default headers, footers, and a gray background behind the page content. This guide shows you how to remove those elements so your PDFs look clean and professional. You'll need a basic understanding of the command line or a Node.js environment, depending on the method you choose.

What You'll Need

  • A computer with Google Chrome or Chromium installed (version 60+ for command-line, or any recent version for Puppeteer/Playwright).
  • For the command-line method: access to a terminal or command prompt.
  • For the Puppeteer/Playwright method: Node.js installed and a project with the corresponding library added.
  • An HTML file or a URL you want to convert to PDF.

Step-by-Step

Method 1: Using the Command Line (Chrome/Chromium)

  1. Open your terminal (Command Prompt on Windows, Terminal on macOS/Linux).
  2. Navigate to the directory containing your HTML file, or note the full path to it.
  3. Run the basic command to generate a PDF:
    chrome --headless --disable-gpu --print-to-pdf=output.pdf file:///path/to/your/file.html
    Replace chrome with the actual path to your Chrome executable (e.g., "C:\Program Files\Google\Chrome\Application\chrome.exe" on Windows, /Applications/Google Chrome.app/Contents/MacOS/Google Chrome on macOS).
  4. Add the flag to remove headers and footers: Include --no-pdf-header-footer in the command:
    chrome --headless --disable-gpu --no-pdf-header-footer --print-to-pdf=output.pdf file:///path/to/your/file.html
    This flag removes the default date, page number, and title that Chrome adds.
  5. Remove the gray background: The gray background typically comes from Chrome's default print background setting. To disable it, add --print-to-pdf-no-header (which is an alias for the above) and also use the --no-pdf-header-footer flag. However, the gray background is often caused by the background CSS property on the html or body element. If your page has a background color, you need to override it in your CSS. For a quick fix, you can inject a style using --virtual-time-budget and a data URL, but the cleanest way is to modify your HTML to set background: white on the root elements. For example, add this to your CSS:
    @media print {
      html, body {
        background: white !important;
      }
    }
    If you can't modify the HTML, you can use a script to inject the style (see Puppeteer method below).
  6. Run the command with the flags. The PDF will be saved to the specified output file.

Method 2: Using Puppeteer (Node.js)

  1. Install Puppeteer in your Node.js project if you haven't already:
    npm install puppeteer
  2. Create a script (e.g., generate-pdf.js) with the following code:
    const puppeteer = require('puppeteer');
    
    (async () => {
      const browser = await puppeteer.launch();
      const page = await browser.newPage();
      await page.goto('file:///path/to/your/file.html', { waitUntil: 'networkidle0' });
      await page.pdf({
        path: 'output.pdf',
        format: 'A4',
        printBackground: false, // This removes gray backgrounds
        displayHeaderFooter: false // This removes headers and footers
      });
      await browser.close();
    })();
  3. Run the script with Node.js:
    node generate-pdf.js
  4. If you need to keep the background (e.g., for a colored page), set printBackground: true. But if you want to remove the gray background that Chrome adds by default (which is actually a light gray on the page margins), setting printBackground: false will make it white.

Method 3: Using Playwright (Node.js/Python)

  1. Install Playwright (e.g., npm install playwright or pip install playwright).
  2. Use the following code (Node.js example):
    const { chromium } = require('playwright');
    
    (async () => {
      const browser = await chromium.launch();
      const page = await browser.newPage();
      await page.goto('file:///path/to/your/file.html');
      await page.pdf({
        path: 'output.pdf',
        format: 'A4',
        printBackground: false,
        displayHeaderFooter: false
      });
      await browser.close();
    })();
  3. Run the script as usual.

Common Mistakes

  • Forgetting the --no-pdf-header-footer flag in the command line – without it, headers and footers appear.
  • Using printBackground: true when you don't need it – this can cause gray backgrounds to appear if your page has any background color set on the html or body.
  • Not setting displayHeaderFooter: false in Puppeteer/Playwright – the default is true, so you must explicitly disable it.
  • Assuming the gray background is from Chrome – often it's from your own CSS. Always check your stylesheets for background colors on html, body, or container elements.
  • Using an old version of Chrome – some flags may not work in older versions. Update Chrome to the latest version.

Tips

  • Test with a simple HTML file first to isolate issues. Create a minimal page with no CSS and see if the gray background disappears.
  • Use --virtual-time-budget=1000 in the command line to give the page time to load fonts or scripts before printing, which can sometimes affect rendering.
  • If you need headers/footers but want to customize them, use Puppeteer's headerTemplate and footerTemplate options, but keep displayHeaderFooter: true.
  • For dynamic pages, wait for network requests to finish using waitUntil: 'networkidle0' to ensure the layout is stable.
  • Consider using a CSS @page rule to set margins and size, which can help with layout consistency.

Troubleshooting

  • Headers/footers still appear: Double-check that you're using the correct flag or option. In the command line, make sure you didn't misspell --no-pdf-header-footer. In Puppeteer/Playwright, verify that displayHeaderFooter is set to false.
  • Gray background persists: If you've set printBackground: false and the gray is still there, it might be from a CSS background on a parent element. Use browser DevTools to inspect the page and see which element has the background. Then override it with background: white !important in a print media query.
  • PDF is blank or not generated: Check that the file path is correct and that Chrome can access it. For local files, use the file:// protocol. Also ensure that your HTML is valid and doesn't have JavaScript errors that prevent rendering.
  • Command not found: If chrome isn't recognized, use the full path to the executable. On Windows, you may need to use quotes around the path.
  • Puppeteer/Playwright errors: Make sure you've installed the library correctly and that your Node.js version is compatible. Run npm install in your project directory.

Frequently Asked Questions

Why does Chrome add a gray background to PDFs by default?

Chrome's default print settings include a light gray background on the page margins, which is part of its print preview styling. Setting printBackground: false or using the command-line flag removes it.

Can I remove headers/footers but keep page numbers?

Yes, with Puppeteer/Playwright you can set displayHeaderFooter: true and provide custom headerTemplate and footerTemplate that only include page numbers. For example, a footer template with .

Does the command-line method work on all operating systems?

Yes, as long as Chrome/Chromium is installed. The exact path to the executable varies by OS, but the flags are the same.

What if my page has a background image I want to keep?

Set printBackground: true in Puppeteer/Playwright, or omit the --no-pdf-header-footer flag (but that will also add headers/footers). To keep the background but remove headers, use --print-to-pdf-no-header (which is the same as --no-pdf-header-footer). Actually, that flag only removes headers/footers, not the background. So you can use --print-to-pdf without the header flag, and the background will be preserved. But if you want to remove the gray margin background, you need to set printBackground: false in Puppeteer, which will also remove your background image. To keep your background image, you need to ensure your CSS sets the background on the html element and use printBackground: true – the gray margin background is not from your CSS, it's from Chrome's default print styles, so it will still appear. The only way to remove that gray margin while keeping your background is to use a custom CSS that sets @page { margin: 0; } and then add your own margins via padding on the body.

Can I automate this for multiple files?

Yes, you can write a loop in your script to process multiple HTML files and generate PDFs for each. In Puppeteer, you can reuse the same browser instance and open new pages for each file.

Is there a way to do this without installing anything?

If you have Chrome installed, you can use the command-line method without any extra software. For Puppeteer/Playwright, you need Node.js and the library, but that's the only dependency.