Conversation
Extract glyph Widths from Type1/TrueType font dicts to detect word boundaries in PDFs that render each character individually via Tj+Td. - Fix readKeyword infinite loop on control characters - Add buildFontWidths() to extract font glyph metrics - Add flushPending() with font-metric word gap detection using previous glyph width and advance comparison - Extract cidToUint16 helper to reduce byte-shifting duplication - Move nolint directive to line-scoped on func declaration - Replace generated test PDF with minimal static per-char-test.pdf - Add control-char.pdf test fixture
Add pdflatex-per-char-text.pdf from py-pdf/sample-files (CC-BY-SA-4.0) to verify font metric word gap detection works on genuine pdfTeX output. Without the fix, pdfTeX-rendered text loses all word spaces (e.g. "Loremipsumdolorsitamet").
Full rewrite replacing the flat char collector with CTM/Tm matrix tracking, q/Q save/restore, cm operator, geometric line grouping (Y-proximity), and gap-based word detection (wordGapRatio=0.09). Add standard 14 font width tables from pdfminer.six (Helvetica, Times, Courier, Symbol, ZapfDingbats) as fallback when font dicts omit /Widths arrays — needed for payslip PDFs using standard fonts. Fix decodeText to use WriteRune for Latin-1 bytes 0x80-0xFF, route literal strings through decodeText in all paths, and derive lastCharCode from the decoded rune for correct font-metric lookup.
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Rewrites the
pdftotextpackage to extract text from PDF content streams usinga matrix-tracked positioned-character pipeline instead of the previous flat char
collector. This fixes extraction quality for bank statement PDFs that
use per-character positioning with standard 14 fonts.
Changes
q/Q(save/restore),cm(matrix concat),Tm(text matrix),Td/TD(position offset) operators — essential for PDFs where each character is
positioned individually via
Tj+Td.(
lineGroupTol=2.0), inserts spaces on X-gaps exceedingcharSize×0.09.Times, Courier, Symbol, and ZapfDingbats from pdfminer.six FontMetricsDB.
Used as fallback when PDF font dicts omit
/Widthsarrays — the root causeof garbled payslip text like "Cotisationm aladie".
decodeTextso bytes0x80-0xFF are decoded to UTF-8; font-metric word gap uses the decoded rune's
byte value for correct character width lookup.
pdflatex-per-char-text.pdf(pdfTeX per-char layout),latin1-word-gap.pdf(Type1 font, Latin-1 accented chars), 12 PDF samplesfrom pdfminer.six in
testdata/pdfminer-samples/.Extraction quality
line (beats pdfminer which interleaves watermark text between real content)
pdftotextpackage, linter clean (0 issues)