feat(pdf): add to_markdown_pages for per-page extraction - #91
Open
HaoChiBao wants to merge 1 commit into
Open
Conversation
Expose a thin wrapper over pdf-inspector's page API so callers get page boundaries and OCR flags without forcing PDFs through the document model (Closes firecrawl#62).
There was a problem hiding this comment.
1 issue found across 4 files
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="src/formats/pdf.rs">
<violation number="1" location="src/formats/pdf.rs:67">
P3: The OCR warning block added here duplicates the one already in `to_markdown` in the same file (same message and count structure, differing only in the denominator: `result.pages.len()` here vs `result.page_count` there). Extract a small shared helper, e.g. `fn warn_ocr_pages(need_ocr: usize, total: usize)`, and call it from both functions so the message and the OCR-recovery policy stay consistent in one place.</violation>
</file>
Reply with feedback, questions, or to request a fix.
Fix all with cubic | Re-trigger cubic
| /// with an external OCR path. | ||
| pub fn to_markdown_pages(bytes: &[u8]) -> Result<Vec<MarkdownPage>, ConvertError> { | ||
| let result = pdf_inspector::extract_pages_markdown_mem(bytes, None).map_err(map_error)?; | ||
| if !result.pages_needing_ocr.is_empty() { |
There was a problem hiding this comment.
P3: The OCR warning block added here duplicates the one already in to_markdown in the same file (same message and count structure, differing only in the denominator: result.pages.len() here vs result.page_count there). Extract a small shared helper, e.g. fn warn_ocr_pages(need_ocr: usize, total: usize), and call it from both functions so the message and the OCR-recovery policy stay consistent in one place.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At src/formats/pdf.rs, line 67:
<comment>The OCR warning block added here duplicates the one already in `to_markdown` in the same file (same message and count structure, differing only in the denominator: `result.pages.len()` here vs `result.page_count` there). Extract a small shared helper, e.g. `fn warn_ocr_pages(need_ocr: usize, total: usize)`, and call it from both functions so the message and the OCR-recovery policy stay consistent in one place.</comment>
<file context>
@@ -37,6 +56,33 @@ pub fn to_markdown(bytes: &[u8]) -> Result<String, ConvertError> {
+/// with an external OCR path.
+pub fn to_markdown_pages(bytes: &[u8]) -> Result<Vec<MarkdownPage>, ConvertError> {
+ let result = pdf_inspector::extract_pages_markdown_mem(bytes, None).map_err(map_error)?;
+ if !result.pages_needing_ocr.is_empty() {
+ log::warn!(
+ "{} of {} pages need OCR and were not extracted",
</file context>
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
MarkdownPageplusto_markdown_pages/to_markdown_pages_byteswrapping pdf-inspector's per-page extraction (page boundaries, OCR flags)to_documentunsupported; leave existing single-blobto_markdown/to_markdown_bytesbehavior unchangedCloses #62
Why not
to_documentfor PDFPDFs intentionally bypass the document model (pdf-inspector emits Markdown directly). A thin per-page API is the low-risk path from the issue and does not race the OCR work in #61.
Test plan
cargo fmt --allcargo clippy --locked --all-targets -- -D warningscargo test --locked(fixturetests/fixtures/pdf/text.pdf)Summary by cubic
Adds per-page Markdown extraction for PDFs so callers can keep page boundaries and detect OCR needs. Previously we only returned a single Markdown blob and errored on fully scanned PDFs; now
to_markdown_pages/to_markdown_pages_bytesreturn one item per page with aneeds_ocrflag, while single-blob APIs remain unchanged.Exposes
MarkdownPage { page, markdown, needs_ocr, ocr_reason }and exports it from the crate.Keeps
to_markdown/to_markdown_bytesbehavior: still one string; still errors on image-only PDFs and logs when some pages need OCR.Adds
to_markdown_pages/to_markdown_pages_bytes: never fail solely due to OCR; pages that need OCR have emptymarkdownandneeds_ocr = trueand may includeocr_reason. Logs a warn when any pages need OCR.Leaves
to_documentunsupported forFormat::Pdf; updates theUnsupportedmessage to mentionto_markdown_pages.Updates README and adds a basic per-page test.
Migration (optional): If you need page boundaries or to route OCR per page, switch PDF callers to
to_markdown_pages/to_markdown_pages_bytes. Otherwise, keep usingto_markdown/to_markdown_bytes.Written for commit 9853fac. Summary will update on new commits.