I'm trying to create an HTML page which includes a <div>
with a background-color
which shows up when printed. This works by using -webkit-print-color-adjust: exact
in the CSS style for the div
, but only up to a point.
If the div
spans multiple pages then only the first page shows the background colour, the rest have white background. This is the case in Chrome, Safari and Edge, while Firefox doesn't even show it on the first page (just white background throughout).
Why this matters: I've got an automated process, using a headless Chrome browser + Puppeteer to convert HTML to formatted and justified text in a PDF, which is then superimposed on a different PDF template document, for production of reports. The background of the PDF template is coloured, and so when I embed the text onto it, I don't want a white rectangle appearing.
Here's a minimal test case to reproduce the problem. (The odd margins and padding are to get the text blocks the right size for superimposing on the template once converted to PDF.)
<html>
<head>
<style>
@media print {
@page {size: A4 portrait; margin: 144pt 72pt 252pt;}
.content {
padding-top: 186pt;
height: 260pt;
background-color: #fffaec;
-webkit-print-color-adjust: exact;
}
}
</style>
</head>
<body>
<div class="content">
<p>Here's some content.</p>
<p>Here's some content.</p>
<p>Here's some content.</p>
<p>Here's some content.</p>
<p>Here's some content.</p>
<p>Here's some content.</p>
<p>Here's some content.</p>
<p>Here's some content.</p>
<p>Here's some content.</p>
<p>Here's some content.</p>
<p>Here's some content.</p>
<p>Here's some content.</p>
<p>Here's some content.</p>
<p>Here's some content.</p>
<p>Here's some content.</p>
<p>Here's some content.</p>
<p>Here's some content.</p>
</div>
</body>
</html>
Any suggestions of tricks that would get the background colour onto pages 2+? Or is this just a case of having to live with the v poor support that there is for CSS print styles?