#4113·pypdf

BUG: 6.16.0 inserts spurious spaces inside words in `extract_text()` for subset TrueType fonts without `/Encoding` (regression from #3905)

Author: behnam-oneschemaCreated Sep 20, 2026Updated Sep 22, 2026
Labelsworkflow-text-extractionwhitespace

Since pypdf 6.16.0, plain page.extract_text() (default, non-layout mode — including the visitor_text callback) splits words in some PDFs, e.g. Total Annual Withholding Reported → To t a l A n n u a l W i t h h o l d i n g R e p o r t e d. 6.15.0 extracts the same text correctly. The behavior is unchanged through 6.19.0.

Environment

$ python -m platform
Linux-6.x-x86_64-with-glibc2.39
$ python -c "import pypdf;print(pypdf._debug_versions)"
pypdf==6.16.0, crypt_provider=('local_crypt_fallback', '0.0.0'), PIL=none

Python 3.13.14. Reproduced on pypdf 6.16.0, 6.16.1, 6.16.2, 6.19.0; not reproducible on 6.15.0.

Code + PDF

python
from pypdf import PdfReader

reader = PdfReader("az-a1-r.pdf")
for line in reader.pages[0].extract_text().splitlines():
    if line.startswith("To"):
        print(repr(line))

pypdf 6.15.0:

'Total Annual Withholding Reported'
'Total Arizona Tax Withheld per federal Forms W-2, W-2c, W-2G and 1099'

pypdf 6.16.0 – 6.19.0:

'To t a l  A n n u a l  W i t h h o l d i n g  R e p o r t e d'
'T otal Arizona T ax Withheld per federal Forms W-2, W-2c, W-2G and 1099'

Other examples from the same page: DATE (MMDDYYYY) → DATE (MMDDY Y Y Y), REVENUE USE ONLY. → REVENUE USE ONLY ., Arizona Joint Tax Application → Arizona Joint T ax Application. The same class of diffs appears with visitor_text (each visited run is split the same way), so this is not a layout-mode issue (#4110) but the default path's synthetic-space insertion.

The PDF is a public form: Arizona Form A1-R (Arizona Department of Revenue), attached (az-a1-r.pdf). It is the official https://azdor.gov/sites/default/files/document/FORMS_WITHHOLDING_2025_A1-R_f.pdf re-saved through macOS Preview (Quartz PDFContext), which rewrote the fonts and dropped their /Encoding entries. The original Distiller file (fonts carry /Encoding /WinAnsiEncoding) does not reproduce on 6.16.0, which points at the encoding-fallback path. Metadata of the attached file: /Creator AdobePS5.dll Version 5.2, /Producer macOS Version 26.2 Quartz PDFContext. Page 0 fonts:

/TT2 /TrueType /AAAAAC+Arial-BoldMT   /Encoding: absent  embedded FontFile2, /Widths present
/TT4 /TrueType /AAAAAE+ArialMT        /Encoding: absent  embedded FontFile2, /Widths present
/TT6 /TrueType /AAAAAG+Tahoma-Bold    /Encoding: absent  embedded FontFile2, /Widths present
/TT8 /TrueType /AAAAAI+Tahoma         /Encoding: absent  embedded FontFile2, /Widths present
/G1  /Type0    /AAAAAJ+Wingdings-Regular /Identity-H

(pypdf also logs Ignoring wrong pointing object 8 0 (offset 0) etc. for this file; the xref is slightly off, but 6.15.0 extracts it cleanly.)

A second public form (Maryland MW506A) shows the same regression; I can attach it too if useful. None of the PDFs under resources/ in this repo reproduce it, so the trigger seems to be the combination above: subset TrueType fonts with no /Encoding entry.

Bisect

git bisect between 6.15.0 and 6.16.0 lands on 1d92adf667418adc590d9f3c7ce7ae1a12c33eda, #3905 "ENH: AppearanceStream: Consider more encodings for Type1 core fonts". Its parent extracts Total Annual Withholding Reported; the commit itself produces To t a l ….

My reading (happy to be corrected): #3905 changed the default encoding resolution in _text_extraction / _font.py (the fallback moved from "charmap" to the cp1252 encoding dict), which changes how glyph widths / the space glyph are resolved for these fonts. crlf_space_check then compares kerning-sized advances against a much smaller reference width and emits a space for every intra-word adjustment. #4067 describes the same symptom class (spaces inserted for small positioning differences), but here the regression is version-specific and bisects to a single commit.

Expected

6.15.0 output: Total Annual Withholding Reported for az-a1-r.pdf