User Agents

Web browsers and everyday web tech, explained

Emulate Screen Media When Printing PDFs in Headless Chrome

· 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

Emulating screen media when printing PDFs in headless Chrome ensures that the styles intended for display on a screen—rather than print—are preserved in the final PDF output. By default, Chrome applies print-specific CSS when generating PDFs, which often strips colors, hides elements, or alters layouts to suit paper formatting. This guide shows how to override that behavior so the resulting PDF mirrors exactly what users see in the browser, which is essential for documentation, design previews, and consistent visual reporting.

What You'll Need

  • Chrome or Chromium installed (version 79 or later)
  • Access to command line or a Node.js environment
  • Basic understanding of Chrome DevTools Protocol (CDP) or Puppeteer
  • A target webpage or local HTML file to convert
  • Optional: Puppeteer or Playwright for scripting automation

Step-by-Step

  1. Launch Chrome in headless mode using the command line or via a browser automation library. The base command starts with: chrome --headless=new --disable-gpu --no-margins --print-to-pdf= followed by the output path and input URL or file.
  2. Add the flag --media=screen to force Chrome to use screen CSS instead of print media queries. This is the key step. Without it, Chrome defaults to @media print styles, which can hide backgrounds, remove colors, and reflow content.
  3. Include --no-pdf-header-footer if you want to eliminate default headers and footers (like URLs and page numbers) that are added automatically during PDF generation.
  4. Set viewport size using --window-size=1200,800 or equivalent in your automation script to ensure responsive layouts render correctly. Headless Chrome may default to a small viewport, affecting layout.
  5. If using Puppeteer, configure the emulateMedia option in the page.pdf() method. Use await page.emulateMediaType('screen') before calling page.pdf(). Example:
    const browser = await puppeteer.launch();
    const page = await browser.newPage();
    await page.emulateMediaType('screen');
    await page.pdf({ path: 'output.pdf', format: 'A4' });
    await browser.close();
  6. Ensure all external resources (CSS, fonts, images) are loaded before printing. Use await page.waitForTimeout(1000) or better, await page.waitForNetworkIdle() to avoid missing assets in the PDF.
  7. Test the output by opening the generated PDF and comparing it visually with the original screen render. Check for missing colors, incorrect fonts, or layout shifts.
  8. For advanced control, inject CSS overrides to disable any remaining print-specific rules. Use await page.addStyleTag({ content: '@media print { * { display: initial !important; color: black !important; background: transparent !important; } }' }) to neutralize aggressive print styles.
  9. Adjust scaling, margins, and paper format as needed using the page.pdf() options like scale, margin, and format.
  10. Save and repeat with different inputs to confirm consistency across pages and environments.

Common Mistakes

  • Forgetting the --media=screen flag or emulateMediaType('screen')—this is the most frequent cause of incorrect styling.
  • Not waiting for full page load, leading to missing images or incomplete layout in the PDF.
  • Using outdated Puppeteer methods; page.emulateMedia is deprecated—use page.emulateMediaType instead.
  • Ignoring viewport settings, resulting in mobile layouts being captured instead of desktop.
  • Assuming all CSS will carry over; some frameworks apply print styles via JavaScript, requiring additional overrides.

Tips

  • Always test with a simple HTML page first to verify the workflow before scaling to complex sites.
  • Use --dump-dom or Puppeteer's page.content() to inspect the rendered HTML and confirm styles are applied.
  • For dynamic content, trigger rendering manually (e.g., wait for a specific element) rather than relying on fixed timeouts.
  • Set printBackground: true in page.pdf() options to preserve background colors and images.
  • Run Chrome with --no-sandbox only in secure, controlled environments—avoid in production.

Troubleshooting

If the PDF still uses print styles, double-check that emulateMediaType('screen') is called before page.pdf(). If colors are missing, ensure printBackground: true is set. If layout is broken, verify the viewport size and wait for network idle. If fonts appear incorrect, confirm they are loaded and not blocked by CORS. If running in Docker, ensure fonts are installed in the container. If using older Chrome versions, upgrade to Chrome 79+ for full --media=screen support. For silent failures, enable logging with --verbose and check stderr output.

Frequently Asked Questions

Does --media=screen work in all Chrome versions?

No, it requires Chrome 79 or later. Earlier versions ignore the flag and default to print media.

Why are my background colors still missing?

Ensure both --media=screen and printBackground: true are set. Some CSS may still force white backgrounds in print media.

Can I emulate screen media without Puppeteer?

Yes, use the command-line flag --media=screen directly in Chrome’s headless mode.

Does this work with JavaScript-heavy sites?

Yes, but you must wait for JS execution to complete. Use waitForFunction or waitForSelector to ensure dynamic content is rendered.

Are there security risks in headless Chrome?

Running with --no-sandbox can pose risks. Always use sandboxing in production and restrict input sources.

Can I preserve interactive elements like buttons?

No—PDFs are static. You can style them to look clickable, but interactivity is lost in conversion.