fix(pdf): align link offsets with decoded text
Summary
Keep PDF link offsets aligned with the Unicode text that is actually extracted.
get_words_from_obj() removes duplicate glyphs when a PDF simulates bold text by drawing each character twice. Its offsets still counted the original layout objects, so a link after the deduplicated prefix pointed too far into the text. In a real PDF containing a double-drawn BOLD prefix followed by LINK, the resulting text is BOLD LINK, but the old raw offset was 10 instead of 5. FAST partitioning then dropped the link when applying its whitespace index mapping. Links on subsequent lines could retain an incorrect offset instead.
One PDF glyph can also decode to multiple Unicode characters through its ToUnicode map, such as ffi or a combining sequence. Counting glyph objects placed later links too early in those cases.
Advance the offset by the length of each retained glyph's decoded text and each emitted annotation separator. Skipped duplicate glyphs contribute nothing. This uses the existing deduplication and word grouping rules and covers both the FAST partitioner and the PDFMiner layout pipeline used by high-resolution partitioning.
This is distinct from #4680, which avoids the word walk for text boxes without overlapping annotations; it does not change how that walk computes offsets. The earlier fake-bold correction in #4215 changed text extraction but retained object-based offsets.
Validation
- Added 20 real in-memory PDF cases through
partition_pdf(strategy="fast")andprocess_data_with_pdfminer(). The PDFs contain actual link annotations, repeated text drawing operators, and explicit multi-characterToUnicodemappings. No parser, model output, or link result is mocked. - Covered same-line and preceding-line prefixes, ligatures, combining sequences, ordinary text, and disabled deduplication. Against the unchanged base: 12 failed, 8 passed. With the correction: 20 passed.
- New cases plus existing duplicate-character and fake-bold PDF integration tests: 38 passed.
- The repository's existing three FAST hyperlink/word-box checks also pass against their real PDF fixtures. Their exact test-function assertions were invoked in a small harness so the unrelated optional model test imports were not required.
- Scoped Ruff lint/format and
git diff --checkpass.
pytest -q \
test_unstructured/partition/pdf_image/test_pdf_link_decoded_indices.py \
test_unstructured/partition/pdf_image/test_pdfminer_utils.py::TestIsDuplicateChar \
test_unstructured/partition/pdf_image/test_pdfminer_utils.py::TestDeduplicateCharsInTextLine \
test_unstructured/partition/pdf_image/test_pdfminer_utils.py::TestGetTextWithDeduplication \
test_unstructured/partition/pdf_image/test_pdfminer_utils.py::TestFakeBoldPdfIntegration
Validated with Python 3.13.5, pdfminer.six 20260107, pypdf 6.10.0, and unstructured-inference 1.6.17. The full make test gate, OCR, and model inference have not been completed locally.
Validation follow-up
- The formal repository-wide
make checkcommand passes on this contribution's worktree. - The official
make test-no-extras CI=truetarget passes: 461 tests with one worker and no test deselection. This shared baseline does not exercise PDF parsing; the 20 real-PDF regression cases above cover this change. - The current CI workflow run is completed with
action_requiredand has started zero jobs. It supplies no CI test result yet. The fullmake testand coverage gates remain unverified and await hosted CI approval.
Checklist
- The PR title is a user-facing release-note entry with a conventional commit prefix.
- A maintainer can apply the
bugcategory label. - No breaking change or migration is required.