These rules work in Chromium-based renderers: Chrome's Print to PDF, Puppeteer, Playwright and APIs built on them, including SlipKiln. Older engines like wkhtmltopdf support less of this.

Tables split across pages

Long tables get rows cut in half at the page edge, and the header row appears only on page one. Two rules fix both:

/* Keep each row whole and repeat the header row on every page */ tr { break-inside: avoid; } thead { display: table-header-group; } tfoot { display: table-footer-group; } 

Chromium repeats a <thead> on every printed page, so make sure your header row is actually in a <thead>, not the first <tr> of the body. A break-inside: avoid row that is taller than a whole page still has to break somewhere; keep rows short.

Controlling where pages break

/* Start each section on a new page */ .section { break-before: page; } .section:first-child { break-before: auto; } /* Don't split cards, figures or signature blocks */ .card, figure, .signature { break-inside: avoid; } /* Don't leave a heading alone at the bottom of a page */ h2, h3 { break-after: avoid; } /* At least 3 lines of a paragraph on each side of a break */ p { orphans: 3; widows: 3; } 

The older page-break-before, page-break-after and page-break-inside properties still work as aliases. New code should use break-*.

Page size and margins

@page { size: Letter; /* or A4, A5, "Letter landscape" */ margin: 18mm 16mm; } @page :first { margin-top: 30mm; } /* extra room for a letterhead */ 

Watch out: many HTML-to-PDF tools pass a paper size to the renderer, and in Chromium that overrides your CSS size unless the tool sets preferCSSPageSize. With SlipKiln, set the size with options.format (A4, A5, Letter, Legal) and options.landscape, and margins with options.margin, so your PDF comes out the same every time.

Backgrounds and colors disappear

Browsers drop backgrounds when printing to save ink. SlipKiln turns print_background on by default; in your own Chromium pass printBackground: true. For individual elements:

/* Keep background colors and shaded rows in the PDF */ * { -webkit-print-color-adjust: exact; print-color-adjust: exact; } 

Fonts and images

A server-side renderer only has the fonts installed on that machine, so text falls back to a default face. Embed fonts with @font-face and a base64 data: URL, and do the same for logos. SlipKiln requires this because its renderer has no network access; it also means your PDF never depends on a CDN being up.

Checklist

  • Header row inside <thead>; tr { break-inside: avoid }
  • break-before: page for sections, break-after: avoid on headings
  • Page size and margins set where your renderer actually reads them
  • Backgrounds on, print-color-adjust: exact for shaded rows
  • Fonts and images inlined

Want to try your HTML? The free plan renders 50 PDFs a month with no card: POST /v1/render/html.

Originally published on slipkiln.com. We build SlipKiln, an API that turns HTML or JSON into PDFs.