Seven layout defects that show up in every HTML-to-PDF invoice, and the CSS that fixes each one.
You design an invoice in the browser. It looks right. You print it, or hand it to a PDF renderer, and something is off — a table header missing on page two, a total split across a page break, margins that were fine on screen now pushing text against the paper edge.
The reason is simple and worth stating once: a print engine implements a different subset of CSS than a screen renderer. The modern layout features you reach for on the web are exactly the ones with patchy or absent print support.
Here are the seven that actually bite.
This is the single most common defect in a multi-page invoice. A line-item table runs past one page, and page two becomes a wall of unlabelled numbers.
The fix is markup, not styling. Put the header in <thead>, the rows in <tbody>, and the print engine repeats the header on every page automatically:
<table class="items">
<thead>
<tr><th>Description</th><th>Qty</th><th>Unit</th><th>Amount</th></tr>
</thead>
<tbody>
<tr><td>Consulting</td><td>10</td><td>150.00</td><td>1,500.00</td></tr>
</tbody>
</table>
Two things break it, both inherited from a screen stylesheet:
position: sticky on the header row. It does nothing in print, and it takes the header out of the repeat path. Scope it to @media screen.The most common mistake is faking margins with body { padding: 40px }. Padding scrolls with content; page margins do not. Set the page box:
@page {
size: A4;
margin: 18mm 16mm 22mm 16mm;
}
body {
margin: 0;
padding: 0;
font: 10pt/1.45 "Inter", system-ui, sans-serif;
}
Use pt and mm here, not px. Print units are absolute and mean the same thing regardless of the device pixel ratio the renderer happens to use. Your screen stylesheet can carry on using rem.
And pick one place for margins: the PDF render call, a CSS @page rule, or body padding. Margins set in two places add up, and margins set in none put text right against the paper edge.
Wrap the whole summary once, and only once:
.summary {
break-inside: avoid;
margin-top: 8mm;
}
/* do NOT also put break-inside: avoid on .totals or .payment-terms */
The rule that matters: break-inside: avoid is a request, and it is dropped whenever the box cannot fit on a page of its own. So keep the wrapper comfortably short. If your summary includes payment terms, bank details and a legal mention, it can approach a full page — at which point the avoid does nothing and you are back to a split total, with no warning.
If it is getting tall, split it deliberately: keep the numbers in the avoid wrapper, let the boilerplate flow after it. To stop the summary landing alone on a final page, pull it up with the table:
.items { break-after: avoid; }
A4 is 210 × 297 mm. US Letter is 216 × 279 mm — wider and shorter. If you design to the edges of one, you clip on the other. Puppeteer defaults to US Letter; your European customers print on A4.
Design to the intersection instead:
max-width: 178mm and nothing overflows on either.top. Let content flow and let the engine break it.Then let the format be a render-time parameter rather than a stylesheet fork:
const pdf = await client.render({
url: signedInvoiceUrl(invoice.id),
format: customer.country === "US" ? "Letter" : "A4",
margin: { top: "18mm", right: "16mm", bottom: "22mm", left: "16mm" }
});
One template, one stylesheet, two page sizes.
Keep break-inside: avoid on rows, not on tbody:
tr { break-inside: avoid; }
On rows it stops a single line item from splitting mid-cell. On tbody it asks the engine to keep the entire body together, which for a sixty-line invoice is impossible — and an impossible avoid is silently dropped.
Text blocks get the usual treatment:
p { orphans: 3; widows: 3; }
h2 { break-after: avoid; }
header_html/footer_html is drawn on top of the final line items.A footer template paints into the margin and does not affect layout. Its height is not accounted for anywhere. So the page margin has to be at least as tall as the footer. If it is not, the footer prints on top of your content — which is exactly what the asymmetric 22mm bottom margin in step 2 was reserving.
Three things about header and footer templates that cost people an afternoon each:
@page { @bottom-center { content: counter(page) } } and nothing rendered.Chromium does not support content: counter(page) in @page margin boxes. Every "add page numbers with pure CSS" answer you find either targets Paged.js or does not work in the engine you are actually using.
Page numbers come from the render call:
const pdf = await client.render({
url: signedInvoiceUrl(invoice.id),
format: "A4",
margin: { top: "18mm", bottom: "22mm" },
footer_html: `<div style="font:8pt sans-serif;width:100%;padding:0 16mm;color:#6b7280">
{page} / {total}
</div>`
});
The most expensive habit is checking your template in a browser and assuming the PDF matches. It frequently does not.
Render to an actual PDF on every change, and check page two. Most of these defects only exist on page two, or on the third page of a long invoice. A template that looks perfect with four line items can break with forty.
If you want a starting point that already has the page box, the repeating header, the row-level breaks and the reserved footer margin wired up, the tool below is what I built after hitting all seven of these.
A single 14 KB HTML file. Fill the form, watch the A4 page update, export. The CSS above is already in it, and the source is commented so you can see where each rule sits.
Open the generator Download the ZIP (42 KB)The tool is MIT licensed with no paywall, no account and no telemetry. Download it, read the source, change it, ship it inside your own product. No attribution required.
Reading this because your invoice printed wrong. I build print-exact A4 PDF templates — selectable text, embedded fonts, tables that paginate with repeated headers. $150 fixed, 3 days.
Order the template build →Or try the free generator first: generator.html
Two versions, both MIT licensed, both run entirely in your browser with no data leaving the machine.