Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable Chrome's default headers & footers in headless print-to-pdf

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.

like image 293
d0n.key Avatar asked Mar 29 '19 13:03

d0n.key


People also ask

How do I turn off the header in Chrome?

You can turn off these headers and footers using the following steps: In the Chrome menu, click Print… to open the print preview screen. Click More Settings. Uncheck Headers and Footers.

How do I enable headers and footers in Google Chrome?

In Chrome, you can turn headers and footers on or off in print settings. To view the print settings, either hold down the Ctrl button and press 'p' or click on the vertical ellipsis at the top right of the browser window, as shown below: The print setup panel will appear on the left of the browser window.


1 Answers

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

like image 56
Nolan Avatar answered Oct 03 '22 20:10

Nolan