Fix Missing Background Colors in Headless Chrome PDFs
In this article
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
- 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.
- Enable the print background graphics option. In all cases, the core requirement is to set the
"printBackground": trueoption. This tells Chrome to include CSS backgrounds, background colors, and images when rendering the page to PDF. - If using Puppeteer, modify your code. In your Puppeteer script, ensure the
page.pdf()method includes theprintBackgroundoption. For example:
Omitting this option results in missing background colors.await page.pdf({ path: 'output.pdf', format: 'A4', printBackground: true }); - If using Playwright, set the correct option. In Playwright, the equivalent option is also
printBackground: true. Example:
Ensure this is not set toawait page.pdf({ path: 'output.pdf', format: 'A4', printBackground: true });falseor omitted. - 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.printToPDFmethod must include"printBackground": truein the parameters. - Verify CSS compatibility. Ensure your CSS uses standard syntax for background colors (e.g.,
background-color: #f0f0f0;orbackground: #ccc;). Avoid using unsupported features likebackground-clip: textunless you’ve tested them in headless mode. - Test with a minimal HTML file. Create a simple page with a colored background:
Generate a PDF with and without<html> <body style="background: #ffcc00; padding: 40px;"> <h1>Test Page</h1> </body> </html>printBackgroundto confirm the fix works. - Check for overridden styles in print media. Some CSS includes
@media printrules that remove backgrounds. For example:
If present, either remove such rules or override them specifically for your PDF context.@media print { * { background: transparent !important; } } - Ensure the page has fully loaded. Backgrounds may not appear if the PDF is generated before CSS is applied. Use
await page.waitForTimeout(500)orwaitUntil: 'networkidle0'in Puppeteer/Playwright to ensure full load. - 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: noneortransparentin 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 printCSS 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 printstyles. 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
styleattributes 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.