User Agents

Web browsers and everyday web tech, explained

Fix Missing Background Colors in 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

When generating PDFs using headless Chrome or Chromium via tools like Puppeteer or Playwright, background colors and images in HTML content may not appear in the final output. This happens because, by default, Chrome disables background graphics during PDF generation to save ink and improve print performance. Fixing this issue ensures your exported PDFs accurately reflect the visual design of your web content, including styled elements like colored headers, shaded sections, or gradient backgrounds.

What You'll Need

  • A working environment with headless Chrome or Chromium (version 60 or later)
  • A script or tool that generates PDFs (e.g., Puppeteer, Playwright, or direct Chrome DevTools Protocol usage)
  • Access to modify the PDF generation command or API options
  • Basic understanding of command-line flags or API parameters for Chrome

Step-by-Step

  1. Identify your PDF generation method. Determine whether you're using Puppeteer, Playwright, a direct Chromium command-line call, or another tool. This affects how you pass the required flag or option.
  2. Enable the print background graphics option. In all cases, the core requirement is to set the "printBackground": true option. This tells Chrome to include CSS backgrounds, background colors, and images when rendering the page to PDF.
  3. If using Puppeteer, modify your code. In your Puppeteer script, ensure the page.pdf() method includes the printBackground option. For example:
    await page.pdf({
      path: 'output.pdf',
      format: 'A4',
      printBackground: true
    });
    Omitting this option results in missing background colors.
  4. If using Playwright, set the correct option. In Playwright, the equivalent option is also printBackground: true. Example:
    await page.pdf({
      path: 'output.pdf',
      format: 'A4',
      printBackground: true
    });
    Ensure this is not set to false or omitted.
  5. If calling Chrome via command line, use the DevTools Protocol. Direct CLI PDF generation requires more setup. Launch Chrome with remote debugging enabled, then send a JSON-RPC request to the DevTools endpoint. The Page.printToPDF method must include "printBackground": true in the parameters.
  6. Verify CSS compatibility. Ensure your CSS uses standard syntax for background colors (e.g., background-color: #f0f0f0; or background: #ccc;). Avoid using unsupported features like background-clip: text unless you’ve tested them in headless mode.
  7. Test with a minimal HTML file. Create a simple page with a colored background:
    <html>
      <body style="background: #ffcc00; padding: 40px;">
        <h1>Test Page</h1>
      </body>
    </html>
    Generate a PDF with and without printBackground to confirm the fix works.
  8. Check for overridden styles in print media. Some CSS includes @media print rules that remove backgrounds. For example:
    @media print {
      * { background: transparent !important; }
    }
    If present, either remove such rules or override them specifically for your PDF context.
  9. Ensure the page has fully loaded. Backgrounds may not appear if the PDF is generated before CSS is applied. Use await page.waitForTimeout(500) or waitUntil: 'networkidle0' in Puppeteer/Playwright to ensure full load.
  10. Re-generate the PDF and inspect the output. Open the resulting file and verify that background colors are now visible. If not, proceed to troubleshooting.

Common Mistakes

  • Forgetting to set printBackground: true — this is the most frequent cause.
  • Using background: none or transparent in CSS, which overrides intended colors.
  • Assuming the option is enabled by default — it is not.
  • Generating the PDF before the page fully renders, missing dynamic background styles.
  • Using @media print CSS that strips backgrounds for physical printing, inadvertently affecting PDFs.

Tips

  • Always test with a known background color to isolate the issue.
  • Use inline styles during testing to rule out CSS loading issues.
  • Set waitUntil: 'networkidle0' to ensure all resources load before PDF creation.
  • If using complex layouts, test with emulateMedia: 'screen' to prevent print-specific styles from applying.
  • For debugging, generate a screenshot (page.screenshot()) first to confirm visuals appear in headless Chrome before PDF export.

Troubleshooting

If backgrounds still don’t appear after enabling printBackground, check the following:

  • Confirm the option is correctly spelled and placed. In Puppeteer and Playwright, it must be inside the page.pdf() options object, not in launch or page settings.
  • Check for CSS specificity overrides. Use browser DevTools (in headful mode) to inspect the element and verify the background style is applied and not crossed out.
  • Test in headful mode first. Run Chrome with a visible window to see if backgrounds render on screen. If they don’t appear there, the issue is with HTML/CSS, not PDF settings.
  • Disable @media print styles. Temporarily remove or comment out any print-specific CSS to test if it’s suppressing backgrounds.
  • Validate the Chrome version. Older versions may have bugs with printBackground. Update to the latest stable release.
  • Ensure no JavaScript is removing backgrounds dynamically. Scripts that modify style attributes after load can interfere. Delay PDF generation or freeze the DOM state.

Frequently Asked Questions

Does printBackground work with images too?

Yes, printBackground: true includes both background colors and background images defined in CSS.

Why do backgrounds show in HTML but not in PDF?

Chrome disables background graphics by default in PDF export to mimic print behavior. Enabling printBackground overrides this.

Can I enable this globally without code changes?

No, this setting must be passed per PDF generation request. There’s no global Chrome flag to permanently enable it.

Does this affect file size?

Slightly. Including backgrounds increases PDF size, especially with large background images, but the impact is usually minimal for solid colors.

Is this supported in all Chromium-based browsers?

Yes, any browser using the Chromium engine and supporting the DevTools Protocol (e.g., Edge, Brave) supports this option in headless PDF generation.

What if only some backgrounds are missing?

This may indicate CSS specificity issues or conditional styling. Inspect the elements to see which rules are applied and whether !important or media queries are interfering.