Dev.to WebDev 🛠 Dev 👁 0 📖 3 min read

Why the font changes when you edit a PDF (and what I did about it)

In my last post I wrote about editing the real text of a PDF instead of painting a white box over it. Once that works, the next problem shows up fast: you change one word, and it comes out in a different font. It isn't

In my last post I wrote about editing the real text of a PDF instead of painting a white box over it. Once that works, the next problem shows up fast: you change one word, and it comes out in a different font.

It isn't a bug in any one editor. It's how PDFs are made.

A PDF only carries the letters it used

To keep files small, a PDF usually embeds a subset of each font: only the letters that the text actually uses. You can often see this in the font name, which gets a random six-letter prefix like ABCDEF+SomeFont.

Take a poster whose heading says "Harvest Supper". Its heading font holds an H, an S and eight lowercase letters. That's it.

Now change "Supper" to "Banquet". The a, u, e and t are there. The B, the n and the q are not. There is no shape for them anywhere in the file.

So where does the new letter come from?

An editor has two choices:

  1. Use the same font if it's installed on your computer. Sometimes it is. Often it isn't, or it's a different version.
  2. Use some other font. That's the common case, and it's why the new word stands out from the line around it.

Neither felt good enough to me, so in OasisPDF I tried a third way.

A Tailored font

When a letter is missing, the editor:

  1. Looks at the document's own letters. The letters the file does hold are measured: how wide and tall they are, how heavy, and how they are drawn.
  2. Picks the closest open font. Their shapes are compared with a library of more than 700 open font families, and the closest one is chosen.
  3. Tailors it to the document. The chosen font is fitted to the font in the file: its letter widths, its heights and its weight, as far as the file shows them. Lines keep about the same length.

This is the last resort, not the first. A letter the PDF already holds is typed in the PDF's own font. If the PDF's font is itself one of the open fonts, the missing letter comes from that very font. Only otherwise is a font tailored, and a label in the editor says so, for example "Close match: Bodoni Moda, fitted". If you don't like the pick, the label's menu lets you choose another font.

Accents get special treatment. A menu set in a font with no é or ñ still has an e and an n, so the editor takes the document's own letter and adds only the accent. That covers forty accented letters, small and capital.

All of this runs in the browser, on your own device. The font files come from the site; your PDF is not sent anywhere.

Where it falls short

It's close, not the same. With text and heading fonts that have a near relative among the open fonts, a new letter sits well beside the old ones. With handwriting, blackletter and other unusual fonts, a new letter can look plainly different from its neighbors.

Scanned PDFs are a different story: a scan is a picture and holds no font at all. There the words are read on your device, and the paragraph you edit is set again in the closest font the editor has.

Here's an 87-second video of it in the editor:

Before-and-after examples are on this page: https://oasispdf.com/tailored-font/

If you have a PDF where the new letters look wrong, let me know. Those are the files I learn the most from.

I wrote this post with the help of an AI assistant. The editor, the examples and the claims are mine, and I checked them.

📰 Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.