User Agents

Web browsers and everyday web tech, explained

Remove PDF Margins from Headless Chrome

· 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 (via the --print-to-pdf flag or the Puppeteer/Playwright API), the output includes default page margins. These margins can make your document look like a standard print job, but if you need a borderless PDF (e.g., for a full-bleed design, a certificate, or a custom layout), you can remove them. This guide shows you exactly how to do that, using both command-line flags and browser APIs, and covers common pitfalls.

What You'll Need

  • A working installation of Chrome or Chromium (version 59 or later for headless mode).
  • Basic familiarity with the command line (for the CLI method) or with Node.js (for the Puppeteer/Playwright method).
  • Optional: A text editor to create a small HTML file for testing.

Step-by-Step

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

  1. Prepare your HTML file – Create an HTML file (e.g., page.html) that you want to convert to PDF. Ensure it contains all the content you want in the PDF.
  2. Open a terminal – On Windows, use Command Prompt or PowerShell; on macOS/Linux, use Terminal.
  3. Run Chrome with the --print-to-pdf flag and the --no-margins flag – The key is to add --no-margins to the command. For example:
    chrome --headless --disable-gpu --print-to-pdf=output.pdf --no-margins page.html
    If Chrome is not in your PATH, provide the full path (e.g., /usr/bin/google-chrome or "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome").
  4. Check the output – After the command finishes, open output.pdf to verify that the margins are gone. If they are still present, you may need to use the CSS @page rule (see Method 2).

Method 2: Using CSS @page Rule (Works for Both CLI and API)

  1. Add a @page rule to your HTML – In the <style> section of your HTML, add:
    @page { margin: 0; }
    This tells the browser to use zero margins for the printed page.
  2. Ensure your body/content has no extra margin – Often the <body> element has default margins. Add body { margin: 0; } to your CSS to be safe.
  3. Generate the PDF as usual – Use the same command-line flag or API call. The @page rule should override any default margins.

Method 3: Using Puppeteer (Node.js)

  1. Install Puppeteer – If you haven't already, run npm install puppeteer in your project directory.
  2. Write a script – Create a JavaScript file (e.g., 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/page.html', { waitUntil: 'networkidle0' });
      await page.pdf({ path: 'output.pdf', format: 'A4', margin: { top: 0, right: 0, bottom: 0, left: 0 } });
      await browser.close();
    })();
        
    Note the margin option set to zero for all sides.
  3. Run the script – Execute node pdf.js in your terminal. The resulting PDF will have no margins.

Method 4: Using Playwright (Node.js, Python, etc.)

  1. Install Playwright – For Node.js: npm install playwright. For Python: pip install playwright and then playwright install.
  2. Use the pdf method with margin option – Example in Node.js:
    const { chromium } = require('playwright');
    
    (async () => {
      const browser = await chromium.launch();
      const page = await browser.newPage();
      await page.goto('file:///path/to/your/page.html');
      await page.pdf({ path: 'output.pdf', format: 'A4', margin: { top: 0, right: 0, bottom: 0, left: 0 } });
      await browser.close();
    })();
        
    Playwright uses the same margin option as Puppeteer.

Common Mistakes

  • Forgetting the --no-margins flag in CLI – Without it, Chrome uses its default margins.
  • Only setting body { margin: 0; } but not @page – The @page rule controls the page box; the body margin only affects content inside the page.
  • Using an old Chrome version – The --no-margins flag was added in Chrome 59. If you're on an older version, it won't work.
  • Not accounting for headers/footers – Headless Chrome may add default headers and footers (like page numbers) that appear as margins. You need to disable them (see Tips).
  • Using the wrong unit in CSS – If you set margin: 0 but your CSS has a conflicting rule with a higher specificity, it might not apply. Use !important if needed, but better to check specificity.

Tips

  • Disable headers and footers – In Puppeteer/Playwright, set displayHeaderFooter: false (it's false by default, but if you enable it, you'll get margins). In CLI, use --print-to-pdf-no-header (though this is not a standard flag; the standard is to use --no-pdf-header-footer in newer versions). Actually, the correct flag is --no-pdf-header-footer for Chrome 92+; for older versions, it's --print-to-pdf-no-header.
  • Use preferCSSPageSize if you have a specific page size – In Puppeteer/Playwright, set preferCSSPageSize: true to use the size defined in your CSS @page rule.
  • Test with a simple HTML file – Before running on complex pages, test with a minimal HTML to isolate margin issues.
  • Check the PDF in a viewer that shows margins – Some viewers (like browser PDF viewers) may not show page boundaries clearly. Use a tool like Adobe Acrobat or Preview to see the actual margins.
  • If using @page with size property – You can combine @page { size: A4; margin: 0; } to set both page size and zero margins.

Troubleshooting

  • Margins still appear after using --no-margins – Check if your HTML has a @page rule that sets a margin. If so, remove it or set it to 0. Also, ensure you are using the latest Chrome.
  • PDF is blank or content is cut off – Removing margins can cause content to be clipped if your layout relies on the margins for spacing. Add padding to your body or use box-sizing: border-box to keep content within the page.
  • Headers/footers appear with page numbers – In CLI, use --no-pdf-header-footer (or --print-to-pdf-no-header for older versions). In Puppeteer/Playwright, ensure displayHeaderFooter is false.
  • Margins are zero but the PDF has a white border when printing – This is often due to the printer's own margins. If you're printing the PDF, set the printer to "borderless" or "edge-to-edge" printing.
  • CSS @page margin not respected – Some Chrome versions have bugs with @page margins. Try using the API margin option instead, or update Chrome.

Frequently Asked Questions

Can I remove margins only on specific pages?

Yes, you can use CSS @page rules with named pages. For example, @page :first { margin: 0; } removes margins on the first page only. This works in Chrome 85+.

Does --no-margins work with --print-to-pdf in all Chrome versions?

No, it was introduced in Chrome 59. If you're using an older version, upgrade or use the CSS @page method.

Will removing margins affect the page size?

No, page size (e.g., A4, Letter) remains the same. Only the margins are removed, so the content area expands to fill the entire page.

How do I set custom margins (e.g., 0.5 inch) instead of zero?

In Puppeteer/Playwright, set the margin object to { top: '0.5in', right: '0.5in', bottom: '0.5in', left: '0.5in' }. In CSS, use @page { margin: 0.5in; }.

Can I remove margins using the Chrome DevTools Protocol directly?

Yes, you can use the Page.printToPDF command with the marginTop, marginRight, marginBottom, and marginLeft parameters set to 0. This is what Puppeteer/Playwright do under the hood.

Why does my PDF have a slight border even with zero margins?

This is likely due to the default body margin in HTML. Add body { margin: 0; } to your CSS. Also, check if your content has padding or borders that create a visual border.