Every business app ends up with the same request: "Can I print this list?" The obvious answers are both bad.
window.print() on the screen gives you a sidebar, a toolbar and a table cut in half across pages. A PDF library
means a second rendering engine, a second layout to keep in sync with the screen, and a big bundle.
We needed something better for the data table in Tekhx UI, because our users print constantly: overdue invoices
for a collections call, stock counts for the warehouse, payroll registers for sign-off. Here is the approach we
landed on. It uses nothing but the DOM, CSS and the browser's own print dialog, and every idea in it works in
any framework.
What "print-ready" has to mean
We wrote the requirements down first, because each one rules out a shortcut:
- Real pages. A4 or Letter, with margins. The preview on screen must be exactly what the printer gets.
- The header row on every page. Page 3 of a 6-page list is useless without its column names.
- A row never splits. Half a row at the foot of a page and half at the top of the next is unreadable.
- Totals and the row count on the last page only. They must share that page with at least one row; a totals line alone on a blank page looks like a bug.
- "Page 2 of 5" on every page. And the filters in force written out ("Status: Overdue · Amount: at least $2,000"), so a loose sheet still says what it is.
- The right orientation. A ten-column table squeezed onto an upright page is worse than no report.
CSS paged media covers part of this: thead { display: table-header-group } repeats the header in most browsers,
and break-inside: avoid on rows is mostly respected. But "Page n of N", totals pinned to the last page and an
exact on-screen preview are where pure CSS runs out. So we lay the pages out ourselves.
Step 1: measure, don't guess
The report renders once off-screen as a single unbounded page: the letterhead, the title block, the header row,
every body row, the totals and the closing line. Then we read real heights:
const rowHeights = [...root.querySelectorAll("tbody tr")]
.map((tr) => tr.getBoundingClientRect().height / scale);
Two details matter here. First, divide by the scale. If the page is zoomed or an ancestor is transformed,
getBoundingClientRect reports scaled pixels. We compare the rendered page width against its true width in CSS
pixels (210mm × 96/25.4 for A4) and divide by the ratio.
Second, wait for fonts. A row measured in the fallback font is shorter or taller than the same row in the real
one. We re-run the measurement when document.fonts.ready resolves.
From the measurements we get the room for rows on each page:
const room = pageHeight - padding - footer - headerRow - slack;
const first = room - letterheadAndTitle; // page one carries the letterhead
const rest = room - runningHead; // later pages carry a one-line running head
const tail = totals + closingLine; // what the last page must still fit
The slack is a few millimetres of safety. Screen and printer round differently, and without it one row in a
hundred spills onto a page of its own.
Step 2: pack rows into pages
With heights in hand, pagination is a small greedy loop:
function paginate(heights: number[], first: number, rest: number, tail: number) {
const pages: Array<[number, number]> = [];
let start = 0, used = 0, room = first;
heights.forEach((h, i) => {
if (i > start && used + h > room) { pages.push([start, i]); start = i; used = 0; room = rest; }
used += h;
});
// The totals must share the last page with at least one row.
if (heights.length && used + tail > room && heights.length - start > 1) {
pages.push([start, heights.length - 1]);
start = heights.length - 1;
}
pages.push([start, heights.length]);
return pages;
}
The i > start guard means a single row taller than a page still gets a page of its own instead of looping
forever. The tail check handles requirement 4: if the totals don't fit after the last rows, we carry the final row
over so the totals have company. It is a pure function, so it is trivial to unit-test with made-up heights.
Step 3: render the pages as pages
Now we render each [start, end) range as its own page element at the paper's exact size: a <table> with the
header row, that slice of rows, and on the last page the totals. Page one gets the letterhead and title; later
pages get a running head. Each foot says "Generated Dec 4, 2026 · Page 2 of 5". Because we produced the pages, we
know N.
The preview dialog shows these same elements, scaled to fit the window. Printing hands them to the browser with a
print stylesheet that sets @page { size: A4; margin: 0 } (the margins live inside our page elements) and forces
a page break after each one. The user prints, or chooses "Save as PDF" in the browser's dialog. No PDF library,
and no second layout to drift out of sync.
Step 4: decide the orientation from the columns
Each column gets a natural width from its content. We use the 90th-percentile cell length rather than the
longest, so one long note doesn't widen the column for everyone. Numbers and dates never wrap, and the bold
totals line is never cut. If the sum is wider than the upright page's printable width, the report turns
landscape on its own. The viewer can still switch it, and switch between A4 and Letter. The default paper comes
from the locale: Letter for the US, Canada, Mexico, the Philippines and a few others, A4 everywhere else.
What the cell prints
The screen and the paper want different things from a cell. On screen a status is a coloured badge and a customer
is an avatar plus a name. On paper we print the word: a pick list prints its label, a badge column prints its
text, money is formatted and right-aligned with tabular figures. Each column can supply its own export value, and
the same function feeds the PDF report, the CSV and the Excel file, so the three always agree.
Results
- One rendering path. The preview is the print.
- No dependency. The table's bundle does not grow with a PDF engine.
- It works for 5 rows or 5,000. Measuring is one layout pass; packing is linear.
- The same page shell prints our invoices, receipts and payslips, so every paper in the app looks like it came from the same office.
Try it
The data table in Tekhx UI does all of this behind one prop:
<DataTable title="Invoices" data={rows} columns={columns}
exportable={{ company: { name: "Acme", addressLines: ["…"], phone, email, taxId }, paper: "a4" }} />
You can press Export › PDF report on the live table in the docs (link below). The approach itself is yours to copy: measure, pack, render pages, let the browser print.
Try the table and its PDF export live: Tekhx UI data table docs.
Top comments (0)