How to Fix PDF Page Breaks in HTML
Page breaks are the #1 developer complaint about HTML-to-PDF conversion. Tables get cut mid-row, headings appear at the bottom of pages with no content following them, and images get sliced in half.
PDF page breaks are the points where content splits across pages during HTML-to-PDF conversion, often causing tables, images, and text blocks to be cut awkwardly unless controlled with CSS properties or smart rendering engines.
Here's how to fix every common page break problem.
The CSS Properties You Need
/* Prevent elements from being split across pages */
tr, .card, .item, figure, blockquote {
break-inside: avoid;
page-break-inside: avoid; /* Legacy support */
}
/* Keep headings with the content that follows */
h1, h2, h3 {
break-after: avoid;
page-break-after: avoid;
}
/* Repeat table headers on every page */
thead {
display: table-header-group;
}
/* Control orphans and widows */
p, li {
orphans: 3;
widows: 3;
}
/* Force a page break before an element */
.page-break {
break-before: page;
page-break-before: always;
}
The Problem with Most PDF Generators
Most HTML-to-PDF tools (wkhtmltopdf, Puppeteer, headless Chrome) rely entirely on the browser's CSS page break implementation. This works okay for simple documents but fails for:
- Tables: Rows get cut in half. Headers don't repeat.
- Cards/Grids: Flexbox and grid items split unpredictably.
- Images: Large images get cropped at page boundaries.
LightningPDF's Smart Page Breaks
LightningPDF solves this with a page_break_mode option:
{
"html": "<table>...</table>",
"options": {
"page_break_mode": "auto"
}
}
Modes
| Mode | Behavior |
|---|---|
auto |
Smart page breaks: avoids splitting rows, repeats headers, respects CSS |
css |
Only respects your CSS break rules |
none |
No special page break handling |
What "auto" mode does:
- Adds
break-inside: avoidto all table rows, cards, and block elements - Forces
display: table-header-groupon<thead>elements - Prevents breaks after headings
- Sets orphan/widow minimums to 3 lines
- Runs a JavaScript pre-render pass to detect elements that would be split
The Native Engine Advantage
For structured documents (invoices, receipts, reports), LightningPDF's native Go engine handles page breaks perfectly because it controls the layout directly:
- Tables automatically split between rows (never mid-row)
- Headers repeat on every page
- Content height is pre-calculated before rendering
This is why native engine PDFs generate in under 100ms — no browser rendering needed.
Quick Fix Checklist
- Add
break-inside: avoidto table rows and card elements - Add
break-after: avoidto headings - Set
display: table-header-groupon<thead> - Set
orphans: 3; widows: 3on paragraphs - Use LightningPDF's
page_break_mode: "auto"for automatic handling
Try It Free
Sign up for LightningPDF and get 50 free PDFs per month with smart page break handling built in.
Frequently Asked Questions
Why do my PDF tables get cut in the middle of rows?
Tables get cut mid-row because most HTML-to-PDF tools rely on the browser's basic page break algorithm, which does not understand table row boundaries. Fix this by adding CSS break-inside avoid to table rows, or use LightningPDF's automatic page-break-mode which prevents mid-row splits and repeats headers on every page.
What CSS properties control PDF page breaks?
The key CSS properties for PDF page breaks are break-inside avoid to prevent elements from splitting, break-before page to force a new page, break-after avoid to keep headings with their content, and orphans and widows set to 3 to prevent lonely lines. The legacy page-break prefix versions work in older tools.
How do I repeat table headers on every PDF page?
Add the CSS rule display table-header-group to your thead element. This tells the PDF renderer to repeat the table header row at the top of each page when a table spans multiple pages. LightningPDF's auto mode applies this rule automatically.
Does LightningPDF handle page breaks automatically?
Yes, LightningPDF offers a page-break-mode auto setting that automatically prevents mid-row table splits, repeats table headers, keeps headings with following content, and sets minimum orphan and widow lines. The native Go engine handles page breaks perfectly for structured documents like invoices and reports.
Related Reading
- HTML to PDF: The Complete Guide — All approaches to HTML-to-PDF conversion
- Generate PDFs in Node.js — Step-by-step Node.js tutorial
- Generate PDFs in Python — Python integration guide
- Generate PDFs in Go — Go tutorial with invoice example
Top comments (0)