Why your HTML invoice prints wrong

Seven layout defects that show up in every HTML-to-PDF invoice, and the CSS that fixes each one.

Written after building one. Every defect below is one I hit, with the symptom first and the fix second.

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.

1The table header vanishes on page two

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.

Symptom. Page one has a proper header row. Page two starts straight into data.

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:

2Margins faked with body padding

Symptom. Margins look correct on screen and wrong on paper. Text sits flush against the paper edge, or the same margin appears twice.

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.

3The total block splits across a page break

Symptom. "Subtotal" at the bottom of page one, "Total due" at the top of page two. This is the failure customers actually notice and email you about.

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; }

4A4 printed as US Letter, or the reverse

Symptom. Content is scaled down slightly, or clipped on the right. The layout looked correct in your tool.

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:

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.

5A line item splits mid-row

Symptom. One item's description occupies the bottom of page one and its amount appears at the top of page two.

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; }

6The footer prints over your last table row

Symptom. The page-number footer from 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:

  1. They do not inherit your CSS. Not your fonts, not your colours, not your variables. Style them inline, in the template itself.
  2. They have no page margin of their own. They span the full paper width, so add the horizontal padding yourself or they will sit flush against the paper edge.
  3. They do not push your content. Reserve the space in the page margin.

7Page numbers that never appear

Symptom. You added @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 habit that catches all seven

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.

PDF Template Generator

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)

Free, and staying free

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.

Want this built for you?

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

Get the template

Two versions, both MIT licensed, both run entirely in your browser with no data leaving the machine.

Free template
One HTML file, edit by hand. Pay what you want.
Full generator — $19
Form input to PDF, source + sample + docs.