25

I got a multipage HTML document, which I want to export to PDF using headless Google Chrome / Chromium from the command line: chrome --headless --print-to-pdf [...]. The issue with this is, that Chrome adds auto-generated headers and footers to the page when "printing". Others have suggested using @page {margin: 0} in my CSS, but as some others also stated, that only works for a few pages for some magic reason, hence there's a footer on the last page of my example.

I am aware of an NPM package that supports export without headers, but that export takes around 30% more time than headless Chrome itself and would require installing and setting up NPM and the package on my company's servers.

If anyone knows any way to hide Google Chrome's default headers / footers on headless PDF-export by CSS or some setting, please let me know.


By the way, since I did not figure out another solution, I went with the NPM package instead. It's been working very well and reliably so far, it just took about 30% more time in my tests, so keep that in mind.

d0n.key
  • 1,318
  • 2
  • 18
  • 39
  • 2
    I've been scouring the internet an answer to this question too. Google built a Node.js solution called Puppeteer which allows you to pass options to create custom headers/footers or to disable them entirely. CLI is simply not supported, so there's no way to remove this useless debug information. I wish they would have just disabled this garbage by default, it causes so much extra work and magical code just to remove this anti-feature. – G_V Jun 04 '19 at 14:26

2 Answers2

43

There is an option available in latest Google Chrome Canary builds for this. Use the --print-to-pdf-no-header option.

canary \
  --headless \
  --disable-gpu \
  --run-all-compositor-stages-before-draw \
  --print-to-pdf-no-header \
  --print-to-pdf=example.pdf \
  http://example.com

ref: source code

adius
  • 13,685
  • 7
  • 45
  • 46
Nolan
  • 748
  • 7
  • 11
  • This new option is very interesting. I found the [commit reference](https://source.chromium.org/chromium/chromium/src/+/731c566fb3cc28fb9159a99f955082b9e8649209). However, I didn't find in which _version number_ of Chrome it got released (if it did). – Pierre H. Nov 02 '20 at 21:10
  • 1
    Indeed, I'm on Chromium 83 (released in mid May, while the patch is from mid April) from Debian, and the `--print-to-pdf-no-header` has no effect (but no error either....). – Pierre H. Nov 02 '20 at 21:18
  • 2
    Using Google Chrome 89.0.4379.0 canary I was able to get this to work using `--headless --disable-gpu --print-to-pdf=output1.pdf --print-to-pdf-no-header`. – drajc Jan 06 '21 at 03:41
  • 1
    This will frustratingly disable _both_ the header and the footer. Is there a way to _only_ disable the header? – dearsina May 21 '21 at 15:56
  • 2
    I know `--print-to-pdf-no-header` is available in Chromium 87 – scravy Jan 16 '22 at 06:12
5

Maybe this thread could help you. basically you need to add CSS bellow

@media print {
  @page { margin: 0; }
  body { margin: 1.6cm; }
}

and the "--no-margins" parameter