Naar de inhoud gaan
Terug naar blog
beste praktijken
5 november 2026
GuruPDF Team
7 min om te lezen

A PDF can contain the text and still show nothing

Our Chinese PDFs passed every check and showed empty boxes. Why text extraction proves nothing about what a reader sees.

We shipped a bug that passed every check we had. A user uploaded a .txt file in Chinese to our text-to-PDF tool. The job completed. The credits were spent. The PDF was valid and a plausible size. And pdftotext returned every character, correctly.

The user opened the file and saw a page of empty boxes.

How a PDF holds text

A PDF has two separate ideas of text that are easy to confuse.

The first is what gets drawn. A content stream says: use this font, at this position, and show these character codes. The renderer looks each code up in the font and paints a glyph.

The second is what gets extracted. A ToUnicode map tells a reader which Unicode character each code stands for, so that search and copy work.

Nothing forces these to agree. In our case the font was DejaVu Sans Mono, chosen because a code comment claimed it "covers full Unicode". It has no CJK glyphs at all. So every code pointed at the missing-glyph box, while the ToUnicode map still faithfully said "this is 中".

Text layer correct. Page useless.

Why the obvious checks passed

Every signal we looked at described the file, not what a person sees.

  • The job status said completed, because the library wrote a file.
  • The size looked right, because a page of boxes takes space too.
  • Extraction worked, because ToUnicode was right.

Three tests, all green, and none of them could see the page.

The test that also lied

So we wrote a real rendering test: rasterise the PDF and check that ink appears on the page. It passed. Then we reverted the fix, and it still passed.

A tofu box is ink. Counting dark pixels cannot tell a glyph from a box.

The version that works renders each character on its own and counts distinct bitmaps. A font that lacks the characters draws the same box every time, so the set collapses to one. A font that has them gives you as many shapes as characters.

We also added a control test that runs the measurement against the known-bad font and asserts it can still detect the original failure. If a change to the harness ever makes the check blind, the control fails first.

The general lesson

If you generate documents, test the thing the user consumes. For a PDF that means rendered pixels, and rendering has its own traps:

  • A bare container has no CJK fonts. Non-embedded CID fonts need the reader to supply them, so a CI image may render nothing at all. Probe the environment and skip, and don't report a false failure.
  • Readers disagree. Check more than one engine. pdf.js, PDFium, MuPDF and poppler each have different failure modes.
  • Prove the test can fail. Break the code on purpose and watch the test go red. A guard you have never seen fail is a hope.

The same bug had a twin. Our watermark drew user text in Helvetica, a Latin-1 core font, so Chinese and Russian watermarks came out as boxes. When you fix something, search for the second copy.

What we changed

Font choice is now driven by the text itself. Each line is cut into runs, and each run takes the first face that actually has all of its characters. One font per file could never work: no single face covers everything, and each face we tried lost some script.

If you are checking your own output, the quickest test is the least technical one: open the file and look at it. You can see the fixed version in our Text to PDF tool.