Background color on header / footer in Headless Chrome

July 26th, 2023 / by api2pdf /


If you are generating PDF files from URLs or HTML with Headless Chrome and are trying to have a background color in the header or footer template of the resulting PDF file, you might be running into issues. This is even the case when you are using puppeteer on top of headless chrome.

The goal is to achieve something like the screenshot below.

Add a colored background to your PDF header / footer in Headless Chrome and puppeteer.

The header template HTML would be in this github gist:

The key is to add css for html { -webkit-print-color-adjust: exact; margin-top: -20px; }. You could change it to margin-bottom: -20px if you are doing a footer. Otherwise you might see a white bar above or below your content.

Take note that the “printBackground”: true attribute that you pass in via options will not have any effect either way. You have to use the css above.

If you are using the API2PDF REST API, this gist payload is a working example:

Tags: ,

Comments are closed.