fix(pdf): preserve visual layout when extracting PDF text - #336
Open
duncanmcqueen wants to merge 1 commit into
Open
fix(pdf): preserve visual layout when extracting PDF text#336duncanmcqueen wants to merge 1 commit into
duncanmcqueen wants to merge 1 commit into
Conversation
extractPdfText joined every pdfjs text item with a blanket space, which merged line fragments across the whole page: wrapped clauses lost their line structure, kerning splits gained spurious spaces, and indentation and column layout (signature blocks, simple tables) were destroyed. Downstream consumers — chat document context, citation quote matching, and any text built from extracted PDF content — all inherited that mangled text. Rebuild each page's text from the positioned pdfjs items instead: - lines are reconstructed from y-coordinates and hasEOL markers (with a y-jump fallback for producers that don't set hasEOL reliably) - words are joined or split based on measured x-gaps, so kerning fragments like 'constitut' + 'e' join without a space while real word gaps keep one - paragraph-scale vertical gaps become blank lines - indentation is kept relative to the page's left margin, and large x-gaps are preserved as columns (capped so justified text isn't exaggerated) Adds unit tests with a mocked pdfjs covering line reconstruction, paragraph breaks, column gaps, page markers, and the unreadable-buffer fallback.
|
|
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.
Problem
extractPdfTextjoined every pdfjs text item on a page with a blanket space. For real legal documents that mangles the text:constitut+e→constitut e)Every downstream consumer — chat document context, citation quote matching, anything built from extracted PDF text — inherits that mangled text.
Fix
Rebuild each page's text from the positioned pdfjs items (
transform,width,hasEOL) instead of the raw string list:hasEOLmarkers, with a y-jump fallback for producers that don't sethasEOLreliably; lines are ordered top-to-bottom, items left-to-right.The page-marker format (
[Page N]) and the unreadable-buffer `` fallback are unchanged.Tests
New
documentOps.test.tswith a mocked pdfjs covers: line reconstruction from positions, paragraph breaks, column-gap preservation, page markers/ordering, and the failure fallback. Full backend suite passes (607 tests),tsc --noEmitclean.