Skip to content

fix(ocr): trigger OCR fallback when scanned PDF has only page numbers (fixes #1863) - #1933

Open
hanhan761 wants to merge 1 commit into
microsoft:mainfrom
hanhan761:fix-1863-ocr-page-header-only
Open

hanhan761 wants to merge 1 commit into
microsoft:mainfrom
hanhan761:fix-1863-ocr-page-header-only

Conversation

@hanhan761

Copy link
Copy Markdown
Contributor

Summary

When PdfConverterWithOCR processes a scanned PDF (no text layer), page headers (## Page N) are unconditionally appended to the markdown output before text extraction. When the page contains no extractable text, only page headers end up in the output — and since the markdown is non-empty, the OCR fallback (_ocr_full_pages) is never triggered.

Changes

  • Added a check after the pdfplumber extraction loop: if the assembled markdown consists entirely of page headers (## Page N) with no real text content, treat it as empty so the existing OCR fallback runs on the full pages.

Issue

Fixes #1863

Verification

python -c "
import re
# Only page headers → treat as empty
lines = [l.strip() for l in '## Page 1\n\n## Page 2'.split('\n') if l.strip()]
assert all(re.match(r'^## Page \d+$', l) for l in lines)
# Headers + content → keep as-is
lines = [l.strip() for l in '## Page 1\n\nSome text'.split('\n') if l.strip()]
assert not all(re.match(r'^## Page \d+$', l) for l in lines)
print('Logic verified')
"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

markitdown-ocr process non-text layer PDFs generate an MD document containing only page numbers

1 participant