Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Fix Chrome Headless Screenshots Cut Off Print CSS

· 7 min read · Home Tech

In this article
  1. Why Headless Chrome Clips Print Pages
  2. The Core Fix: Set Window Size to Match Page Size
  3. Handling Multi-Page Print Styles
  4. Using the New Headless Mode
  5. Common Pitfalls and Troubleshooting
  6. Alternative: Use Puppeteer or Playwright
  7. Frequently Asked Questions

Chrome headless screenshots cut off print-style pages because headless Chrome's default viewport is 800x600 pixels, while print CSS applies a page width (often 8.5in or 210mm) that exceeds that viewport, causing the right side and bottom to be clipped. The fix is to set the window size to match your page size (e.g., --window-size=1240,1754 for A4 at 96 DPI) and use --force-device-scale-factor=1 to avoid scaling. This article explains why the clipping happens, the exact commands to fix it, and how to handle multi-page print styles.

Why Headless Chrome Clips Print Pages

Headless Chrome (the --headless mode in Chrome 59+, and the newer --headless=new in Chrome 112+) renders web pages in a virtual browser window. By default, that window is 800 pixels wide and 600 pixels tall. When you request a screenshot, Chrome captures the visible viewport—it does not automatically expand to fit the entire page unless you use --screenshot with a full-page flag (which has its own limitations).

Print-style pages use CSS @media print rules to set a page size, margins, and often page-break-after to split content into multiple pages. In headless mode, Chrome applies these print styles when you use --print-to-pdf, but when you use --screenshot, it uses the screen media type by default. However, if your page uses @page { size: A4; } or similar, Chrome's rendering engine still reserves space for the page box, which can cause the content to overflow the viewport.

The most common scenario: you have a page that looks fine in a regular browser, but when you run chrome --headless --screenshot=out.png --window-size=800,600 https://example.com/print, the resulting image is cut off on the right and bottom. This happens because the page's print CSS sets a width of 210mm (A4) which, at 96 DPI, is about 794 pixels—just under 800, so it might fit horizontally, but the height of the page (297mm ≈ 1123px) far exceeds 600px, so the bottom is clipped. With Letter size (8.5x11in), the width is 816px, which exceeds 800px, causing horizontal clipping.

The Core Fix: Set Window Size to Match Page Size

To prevent clipping, you must set the headless window size to at least the pixel dimensions of your print page. Here’s the formula for common page sizes at 96 DPI (the default CSS pixel density):

  • Letter (8.5 x 11 in): 816 x 1056 pixels
  • A4 (210 x 297 mm): 794 x 1123 pixels
  • Legal (8.5 x 14 in): 816 x 1344 pixels

You can calculate any size: pixels = inches * 96. For A4, 210mm = 8.27in, so 8.27 * 96 = 794px; 297mm = 11.69in, so 11.69 * 96 = 1122.24px (round to 1123).

Then run the screenshot with that window size:

  1. Open a terminal or command prompt.
  2. Navigate to your Chrome installation directory or use the full path to the Chrome executable.
  3. Run the command with --window-size=WIDTH,HEIGHT and --screenshot.

Example for A4:

chrome --headless --disable-gpu --screenshot=page.png --window-size=794,1123 --force-device-scale-factor=1 https://example.com/print

The --force-device-scale-factor=1 ensures that the CSS pixel size matches the physical pixel size of the screenshot. Without it, on high-DPI displays, Chrome might scale the output, making the image larger or smaller than expected, which can reintroduce clipping.

Handling Multi-Page Print Styles

If your print CSS splits content into multiple pages (using page-break-after: always or break-after: page), a single screenshot will only capture the first page—or, if you set the window height to the full document height, you might get a very tall image that includes all pages stacked vertically. That is often not what you want.

To capture each page separately, you have two options:

  • Use --print-to-pdf instead: This produces a proper multi-page PDF with each page in the correct size. You can then convert the PDF to images using a tool like ImageMagick (convert -density 96 file.pdf page-%d.png). This is the most reliable method for multi-page documents.
  • Take a full-page screenshot and crop: Use --screenshot --window-size=794,5000 (a very tall window) to capture the entire content, then use an image editor to crop each page section. This is hacky and not recommended for production.

For a single-page print layout (e.g., a one-page invoice or certificate), setting the window to the exact page size works perfectly.

Using the New Headless Mode

Chrome 112+ introduced --headless=new, which is a separate implementation of headless mode that more closely matches the full Chrome browser. The old --headless mode is still available but is considered legacy. The new mode has slightly different behavior with respect to window size and screenshot capture.

In --headless=new, the --window-size flag still works, but you might also need to use --hide-scrollbars to prevent scrollbars from appearing in the screenshot, which can add extra pixels and cause misalignment. Example:

chrome --headless=new --screenshot=page.png --window-size=794,1123 --hide-scrollbars --force-device-scale-factor=1 https://example.com/print

If you don't specify --headless=new, Chrome uses the legacy mode. For compatibility, test both modes to see which gives the correct output for your page.

Common Pitfalls and Troubleshooting

Even with the correct window size, you might still see clipping. Here are the most common causes and solutions:

  • Device scale factor: If you don't set --force-device-scale-factor=1, Chrome might render at a higher scale (e.g., 2x) on a high-DPI screen, making the screenshot twice as large but still only capturing the 800x600 viewport. Always include this flag.
  • Scrollbars: Scrollbars take up space. Use --hide-scrollbars to remove them.
  • Margins in @page: If your CSS sets @page { margin: 20mm; }, the content area is smaller than the page size. The screenshot will include the margins, so the window size should be the full page size, not the content area. If you want to capture only the content, you need to adjust the window size to the content area, but that requires knowing the exact margin in pixels.
  • Zoom level: If the page has a viewport meta tag or uses responsive design, the rendering might be affected. In headless mode, you can set --force-device-scale-factor to 1 to ensure 1 CSS pixel = 1 device pixel.
  • Images and fonts loading: Headless Chrome might take a screenshot before all resources load. Use --virtual-time-budget=5000 to wait 5 seconds (or more) for the page to fully render.

Alternative: Use Puppeteer or Playwright

If you need more control over the screenshot process, consider using a browser automation library like Puppeteer (Node.js) or Playwright (Python/Node.js). These tools allow you to set the viewport size, wait for network idle, and capture full-page screenshots programmatically.

In Puppeteer, you can do:

const browser = await puppeteer.launch({headless: true});
const page = await browser.newPage();
await page.setViewport({width: 794, height: 1123});
await page.goto('https://example.com/print', {waitUntil: 'networkidle0'});
await page.screenshot({path: 'page.png'});
await browser.close();

This gives you more flexibility, such as capturing full-page screenshots with fullPage: true, which automatically scrolls and stitches the entire page together. However, for a print-style page with fixed page breaks, the fullPage option might still not produce the desired pagination.

For multi-page PDFs, Puppeteer's page.pdf() method is the best choice, as it respects @page rules and generates a proper PDF.

Frequently Asked Questions

Why does my screenshot have a white border at the bottom?

This usually happens when the window height is larger than the page content height. Chrome captures the entire viewport, including empty space. Set the window height to exactly the page height (or the content height) to avoid extra white space.

Can I set the window size to a percentage of the page?

No, --window-size only accepts pixel values. You must calculate the pixel dimensions based on the page size in inches or millimeters. Use the formula: pixels = inches * 96, or mm / 25.4 * 96.

Does this work with Chrome on Linux, Windows, and Mac?

Yes, the flags are the same across all platforms. The only difference is the path to the Chrome executable. On Linux, it's often google-chrome or chromium; on Windows, it's chrome.exe; on Mac, it's /Applications/Google Chrome.app/Contents/MacOS/Google Chrome.

What if my page uses a custom page size like 100mm x 150mm?

Calculate the pixel equivalents: 100mm = 3.94in, so 3.94 * 96 = 378px; 150mm = 5.91in, so 5.91 * 96 = 567px. Use --window-size=378,567.

Why does the screenshot not match the print preview in my browser?

Print preview in a regular browser uses the full rendering engine with print media type. Headless Chrome's --screenshot uses screen media type by default. To emulate print media, you can use --print-to-pdf and then convert to an image, or use Puppeteer's page.emulateMediaType('print') before taking a screenshot.

Is there a way to capture all pages of a multi-page document as separate images?

The simplest method is to generate a PDF with --print-to-pdf and then split it into images using a tool like pdftoppm (from poppler-utils) or ImageMagick. For example: pdftoppm -png -r 96 output.pdf page creates page-1.png, page-2.png, etc.