PDF

The PDF should be the page you're looking at

For months every PDF in the product came from a second program. This week the PDF finally prints what the screen draws.

· 2 min read

This whole thing started with PDFs that didn't match. Even once the browser drew slides properly, the PDFs still came from headless LibreOffice for a long time. A second, completely different implementation of the format, with its own ideas about strokes, effects and text.

So the preview showed one thing and the PDF showed another. The same problem as before, just moved.

Breaking documents into pages

The first step, back in September, was boring and important. A Word document on screen is one long scroll. A PDF needs pages. At first I only broke pages where the file explicitly said to, because guessing breaks seemed worse than not breaking at all.

That stops being true the moment the PDF is the thing you deliver. One forty-page-tall page is not a document. And it isn't really guessing: the browser has already laid out all the text, so you can read where each block ends and cut where the page ends, splitting a paragraph mid-flow like Word does.

The first version had a fun bug: a page holding just one block too tall to fit created an empty page, moved nothing, and did it again. Five hundred times.

Writing our own PDFs

Then the real job, which has been most of this week: a PDF writer that prints what the page shows. Not a screenshot. Real vector shapes, real text in the same fonts the page uses, embedded, selectable, searchable.

Every time I compare the PDF to the screen, something else turns up missing. This week's list:

  • Charts weren't printed at all, only their text.
  • Rotated labels and watermarks printed upright.
  • Text in all caps (set by a style, not typed that way) came out lowercase.
  • Spaces between words disappeared.
  • After one stretched piece of text, every piece after it was stretched too.
  • Every Excel cell border printed twice as thick, once for each of the two cells sharing it.

None of these show on screen. They only show up when you put the PDF next to Office's own PDF and measure.

Where it is today

On the same set of real files, scored against Office's own PDFs, the PDF export went from 65.6 to 82.7 on Word documents and from 73.9 to 88.7 on PowerPoint over the last few days. On slides it's now within a couple of points of what the screen draws. Still open: gradients in Excel data bars, and text inside old EMF pictures. But it's finally close to what I wanted on day one: the PDF is the page.