Generating print-ready PDFs is still an unsolved problem
At the agency I built two document generators for two different clients. Both take an Excel file and return a finished PDF, and both ran into the same walls, so this note is about the problem, not about either product.
The first one, at the end of 2024, builds one long document of about 120 pages: a cover, a table of contents, dozens of sections and an index of every entry with the page it is on. Until then it was put together by hand in Word. A person can do that, but code gives what a person cannot keep up over 120 pages:
- the same structure every time;
- no forgotten entry;
- a new version whenever the data changes.
The second one, in 2025, makes nine kinds of documents for a small team, each with its own look and its own hidden logic, such as values recalculated along the way. These documents did not exist before at all; there was only the wish to have them. Now the team edits a spreadsheet, uploads it and gets the PDF back. Over a year it produced more than 160 of them, and new templates kept arriving without rewriting the core.
It sounds like a weekend job. It was not.
Why a browser
There are three ways to make a PDF from code:
- A library where you place every line by coordinates. Works for an invoice, falls apart for anything with real layout.
- An engine made for print. Knows about pages, but its layout lags years behind browsers.
- A browser. Always supports the newest standards: flexbox, grid, everything a designer reaches for.
Ask for a grid of cards that reflows across pages, and most tools drop out right there.
The choice also came from the team. I worked on the backend; a friend who knew HTML and CSS deeply did the service's page and the document templates. With a browser, each of us worked in what we knew best, and the templates were ordinary web pages that my friend could open and fix without me. So the documents are HTML, rendered by a local headless Chrome (through pyppeteer) and printed to PDF. With Chrome on the server you can build about 90% of complex layouts.
The browser also has to be cleaned up after. Chromium started from code does not close when the program is done with it, and every document left one more browser behind, filling up the memory, until the builder learned to close it after each render. Even the Chromium version had to be pinned, because the one pyppeteer downloads by default had issues.
The other 10% is where the problems are.
Reading Excel is harder than it looks
Before rendering anything, the data has to come out of the spreadsheet, and I spent many hours on the parser alone. Formatting inside a cell (bold, italics, colours) has to survive into HTML, so cells are read as rich text and converted piece by piece.
Images were worse. An .xlsx file is a zip of XML files, and Excel has two kinds of images in it:
document.xlsx a zip archive
└── xl/
├── worksheets/sheet1.xml the cells
├── drawings/drawing1.xml floating images, each anchored to a cell
└── media/image1.png the image files themselves
A floating image sits on top of the sheet in the drawing layer, anchored to the cell under its top-left corner, so you can tell where it belongs. An image placed in a cell is not in that drawing layer at all, and nothing in the obvious places says which cell it belongs to.
Images are the slow part
Laying out text costs almost nothing, even for 120 pages. The time and the file size go to images. The page is handed to Chrome as one string, with no address it could load files from, so every image is embedded in the HTML as base64, and every one of them is first compressed with Pillow: JPEG for photos, PNG where transparency has to survive. Each image is a choice between how sharp it looks in print and how heavy and slow the document gets. There is no right setting, only a balance for each kind of image.
A table of contents needs page numbers you do not have yet
The long document has a table of contents and an index: every section and every entry with the page it is on. But you only know the pages after the document is laid out, and the layout depends on the content, the contents pages included.
The solution is two passes. First the document is rendered with an invisible marker next to every entry. Then the PDF is read back, the text of each page is searched for the markers, and that gives the page numbers. Only then are the contents and the index filled in and the whole document rendered again.
1. Render with markers
[id:Section A]→ 2[id:Entry B]→ 2[id:Entry C]→ 3
2. Find each marker's page
3. Render again with the numbers
This holds as long as filling in the numbers does not change how many pages the contents take. The marker is ordinary text that nobody can see:
<span style="position: absolute; font-size: 1px; scale: 0.01">[id:Section A]</span>
It cannot be hidden with display: none, because then it is not in the PDF's text either, and there is nothing to search for. And a heading can appear in the text of more than one page, so the first page where a marker is found wins.
The first page is different
Chrome can print a header and a footer with the page number on every page, but not "on every page except the first". The cover has to be clean. So the cover and the rest are two separate renders, glued together into one PDF at the end, and only then encrypted.
The cover
The rest, with a header and page numbers
One PDF
The second time
The second generator was easier, and it was also a chance to fix the first one's mistakes. The first had a PDFBuilder class: an object that collected pieces of the document, held a browser and was built up step by step. The second has a function: HTML in, PDF out. The difference is not only in the code. A document stopped being something you assemble and became something you compute from the data, and most of the state that could go wrong went away with the class.
Why "unsolved"
CSS even has a way to ask for the page number of an element, which would make the two passes unnecessary. Browsers do not support it. So every serious PDF generator ends up as a pile of workarounds like the ones above.
It works. I just would not call it solved.
After two generators I still do not have a model of this that I would call finished. The closest I have is an idea: most of a document is layout that a designer should be able to change without a developer, and a small part is logic that only code can do well. A tool that is 90% no-code and 10% code could make every document in this note. I would like to build it one day.