I build a resume builder that renders PDFs with @react-pdf/renderer. Resumes are a special kind of PDF: a human reads them, but first a machine usually does. Applicant tracking systems and importers pull the text out, and if the text comes out wrong, the nicest layout in the world does not help.
So I wrote an audit: render every template, extract the text with two common open-source extractors, pdf.js and Poppler, and compare. The first run was humbling. In 28 of our 30 templates, pdf.js returned the section headings spelled out letter by letter: E X P E R I E N C E, E D U C A T I O N, and the job title under the name too. Poppler joined them correctly in all but one.
Nothing looked wrong on screen. The cause was a design choice almost every resume template makes.
PDFs don't store spaces, extractors guess them
A PDF page is a list of glyphs with positions. There is no guarantee that a space character exists between two words; often there is just a gap. Every extractor has to decide, for each gap, whether it is a space or just the normal distance between two letters.
pdf.js makes that decision with a threshold relative to the font size. In pdfjs-dist 6.2.108 it lives in pdf.worker.mjs:
const TRACKING_SPACE_FACTOR = 0.102; // ... textContentItem.trackingSpaceMin = fontSize * TRACKING_SPACE_FACTOR; If the gap between two glyphs is larger than about 10.2% of the font size, pdf.js inserts a space.
Now look at a typical resume heading: small caps, uppercase, generous letter spacing for that airy look. Our templates used letterSpacing between 0.11 and 0.16 em on headings. Every single gap was above the threshold, so every letter became its own word.
The fixes that don't work
My first idea was to write the spacing differently. The PDF content stream has a character spacing operator (Tc), and I thought an extractor might treat "spacing the font asked for" differently from "glyphs placed far apart". It does not. pdf.js measures the actual distance between glyph positions, so how the spacing got there is irrelevant.
The second idea was to fix it per template. With 30 templates and two rendering engines (the PDF and an HTML preview that must match it pixel for pixel), that is 60 places to forget something.
The fix: cap the spacing in the one primitive everything uses
All our templates render text through a shared Text primitive, with one implementation for the PDF engine and one for the HTML preview. That made it the right place for a single rule: letter spacing never goes above 0.095 em, just under pdf.js's 0.102, with a margin.
/** Maximum letter spacing, in em: below pdf.js's 0.102, with a margin. */ export const MAX_TRACKING_EM = 0.095; /** The capped letterSpacing, or null if nothing needs to change. */ export function trackingCap(style: unknown): number | null { const s = flatStyle(style); const ls = s.letterSpacing; const fs = s.fontSize; if (typeof ls !== 'number' || typeof fs !== 'number' || !(ls > 0) || !(fs > 0)) return null; const max = Math.floor(fs * MAX_TRACKING_EM * 100) / 100; return ls > max ? max : null; } /** Same style with the cap appended last (so it wins), or the same object if unchanged. */ export function withTrackingCap<T>(style: T): T { const cap = trackingCap(style); if (cap == null) return style; return [...(Array.isArray(style) ? style : [style]), { letterSpacing: cap }] as unknown as T; } Both Text implementations call withTrackingCap on their style. Two details mattered:
- The cap only applies when the same style sets
fontSize.letterSpacingin points means nothing without the font size it is relative to, and guessing an inherited size would make the preview and the PDF disagree. - The cap is appended last instead of mutating the style, so it wins over anything a template sets, and styles that don't need it are returned untouched (same object, no re-render).
Visually the headings are still spaced, just a little less: on an 11 pt heading, 1.04 pt instead of 1.2 pt. Nobody has noticed the difference by eye.
Results
After the change, the same audit:
| pdf.js | Poppler | |
|---|---|---|
| English, before | 28 of 30 templates | 1 of 30 |
| English, after | 0 | 0 |
| Spanish, after | 0 | 0 |
| German, after | 0 | 5 (one label) |
The German leftover is honest bookkeeping: one long uppercase label at 0.07 em still gets split by Poppler in five templates. It was like that before the change, it is below our cap, and Poppler uses a different heuristic, so it needs its own look.
Keeping it fixed
A rule that lives in one helper is easy to delete in a refactor. Two things guard it:
- A regression test that renders a real PDF and reads it back with pdf.js. It fails if any heading comes out as single letters, and it fails if you remove the cap. A test that only checks
trackingCap()returns a number would pass happily while the PDF is broken. - A rule for new templates: use the shared
Textprimitive, never the renderer's ownTextdirectly, or the cap silently disappears.
Check your own PDFs
If you generate PDFs that machines will read (resumes, invoices, reports), this takes a minute:
import { getDocument } from 'pdfjs-dist/legacy/build/pdf.mjs'; const doc = await getDocument('resume.pdf').promise; const page = await doc.getPage(1); const { items } = await page.getTextContent(); console.log(items.map((i) => i.str + (i.hasEOL ? '\n' : '')).join('')); Look for single capital letters separated by spaces. If you see them, compare your letterSpacing with 10% of the font size. The non-programmer version works too: open the PDF in Firefox, select all, copy, paste into a plain text editor.
I build Folium, a free resume builder in 23 languages. The PDFs are made to be read by software as well as people, which is how I ended up reading pdf.js source at midnight.
