Remove PDF Margins from Headless Chrome
In this article
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)
- 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. - Open a terminal – On Windows, use Command Prompt or PowerShell; on macOS/Linux, use Terminal.
- Run Chrome with the
--print-to-pdfflag and the--no-marginsflag – The key is to add--no-marginsto 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-chromeor"/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"). - Check the output – After the command finishes, open
output.pdfto verify that the margins are gone. If they are still present, you may need to use the CSS@pagerule (see Method 2).
Method 2: Using CSS @page Rule (Works for Both CLI and API)
- Add a
@pagerule 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. - Ensure your body/content has no extra margin – Often the
<body>element has default margins. Addbody { margin: 0; }to your CSS to be safe. - Generate the PDF as usual – Use the same command-line flag or API call. The
@pagerule should override any default margins.
Method 3: Using Puppeteer (Node.js)
- Install Puppeteer – If you haven't already, run
npm install puppeteerin your project directory. - 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 themarginoption set to zero for all sides. - Run the script – Execute
node pdf.jsin your terminal. The resulting PDF will have no margins.
Method 4: Using Playwright (Node.js, Python, etc.)
- Install Playwright – For Node.js:
npm install playwright. For Python:pip install playwrightand thenplaywright install. - Use the
pdfmethod 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-marginsflag in CLI – Without it, Chrome uses its default margins. - Only setting
body { margin: 0; }but not@page– The@pagerule controls the page box; the body margin only affects content inside the page. - Using an old Chrome version – The
--no-marginsflag 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: 0but your CSS has a conflicting rule with a higher specificity, it might not apply. Use!importantif 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-footerin newer versions). Actually, the correct flag is--no-pdf-header-footerfor Chrome 92+; for older versions, it's--print-to-pdf-no-header. - Use
preferCSSPageSizeif you have a specific page size – In Puppeteer/Playwright, setpreferCSSPageSize: trueto use the size defined in your CSS@pagerule. - 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
@pagewithsizeproperty – 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@pagerule 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
bodyor usebox-sizing: border-boxto keep content within the page. - Headers/footers appear with page numbers – In CLI, use
--no-pdf-header-footer(or--print-to-pdf-no-headerfor older versions). In Puppeteer/Playwright, ensuredisplayHeaderFooteris 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
@pagemargin not respected – Some Chrome versions have bugs with@pagemargins. 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.