pdfmux icon indicating copy to clipboard operation
pdfmux copied to clipboard

Public API exposes no per-page text — DocumentResult.pages exists but isn't returned by process()/extract_json()

Open theoriginalpebkac opened this issue 5 months ago • 1 comments

pdfmux version: observed on 1.6.4 (latest installed here). v1.7.0 is the current release, but its notes state the programmatic API defaults are Unchanged, so this is expected to persist — happy to confirm on 1.7.0 if useful. Python: 3.12

Summary

There is no public way to get per-page text out of pdfmux, even though the library clearly models it internally. process() returns a ConversionResult that has only a single combined text blob (plus a page_count integer), and extract_json() / chunk() collapse a multi-page digital PDF into a single page/chunk entry. Meanwhile the library ships a DocumentResult dataclass whose pages field is a list[PageResult], and PageResult already carries exactly the per-page data consumers need:

PageResult fields: page_num, text, confidence, quality, extractor,
                   image_count, ocr_applied, tables, key_values,
                   cost_usd, tokens_used
DocumentResult fields: pages, source, confidence, extractor_used, format,
                       text, warnings, ocr_pages, total_cost_usd,
                       total_tokens, has_arabic
ConversionResult fields: text, format, confidence, extractor_used,
                         page_count, warnings, classification, ocr_pages

So the per-page structure exists (DocumentResult.pages → PageResult.text / .page_num / .confidence), but no public entry point returns a populated DocumentResult, and the pymupdf4llm (digital) extractor path doesn't seem to populate per-page text at all.

Why this matters

Per-page text is the basis for page-accurate provenance in a RAG pipeline: attributing each retrieved chunk to a source page, inserting page-boundary markers, and (in our case) iterating figures/tables per page. With only a single blob + a page_count integer, none of that is possible without re-running a separate extractor and trying to re-derive page boundaries — which defeats the point of pdfmux owning routing/extraction.

Reproduction

import pymupdf, pdfmux
from pdfmux import pipeline

# Build a clean 3-page digital PDF, one distinct paragraph per page.
doc = pymupdf.open()
for t in ["Page one paragraph.", "Page two paragraph.", "Page three paragraph."]:
    pg = doc.new_page()
    pg.insert_textbox(pymupdf.Rect(72, 72, 520, 720), t, fontsize=12)
doc.save("three.pdf")

r = pipeline.process(file_path="three.pdf", output_format="markdown", quality="standard")
print("page_count:", r.page_count)            # -> 3   (correct)
print("has .pages:", hasattr(r, "pages"))     # -> False
# ConversionResult.text is one combined blob; no per-page split.

ej = pdfmux.extract_json("three.pdf")
print("extract_json pages:", len(ej["pages"]))  # -> 1  (expected 3)

ch = pdfmux.chunk("three.pdf", max_tokens=200)
print("chunk page spans:", [(c["page_start"], c["page_end"]) for c in ch])
# -> [(1, 1)] for the whole document; all content reported as page 1

Observed on 1.6.4: page_count is correct (3), but process() has no pages, extract_json()["pages"] has length 1, and chunk() reports page_start == page_end == 1 for the entire document. extractor_used is pymupdf4llm.

Expected

A public way to obtain per-page text, e.g. one of:

  1. process(...) populates a per-page field (the DocumentResult.pages list of PageResult, each with page_num + text + confidence), or returns DocumentResult (or a flag like process(..., per_page=True)); and/or
  2. extract_json()["pages"] / chunk() actually segment per page for the pymupdf4llm (digital) path, as they presumably already do for the Docling path.

The minimal contract that would unblock downstream provenance work: per-page text, the source page_num, and per-page confidence — i.e. a populated DocumentResult.pages.

Notes

  • pymupdf4llm.to_markdown(doc, page_chunks=True) returns correct per-page markdown in ~0.24s, so the underlying extractor already produces per-page output — it just isn't surfaced through pdfmux's public API.
  • This looks like a regression or refactor: downstream code (ours) was written against result.pages, which suggests some prior/related API exposed it.

theoriginalpebkac avatar Jun 29 '26 23:06 theoriginalpebkac

Closing this — 1.7.0 already provides public per-page text, so the core ask is met. I'd missed it when filing.

pdfmux.streaming.process_streaming(file_path, quality) yields per-page events carrying exactly the "minimal contract" requested — page_num, text, and confidence (plus quality/extractor/ocr_applied), with a classified event first and a complete event last. There's also a pdfmux stream CLI command over the same generator. I verified the per-page output is 98.8–100% identical to process() on born-digital and simple-table PDFs, and it routes through the same pymupdf4llm.to_markdown path, so it's a faithful per-page view of the digital extractor.

Two small notes that may be worth a glance:

  1. Root cause of the mistaken version in this report: pdfmux.__version__ reports 1.6.4 even on a 1.7.0 install (pip show pdfmux correctly says 1.7.0). That stale attribute is what led me to file against "1.6.4". Might be worth syncing __version__ to the released version.
  2. Optional, not a request: process()'s multi-pass / Docling-table output still has no per-page equivalent — process_streaming is pymupdf4llm-only, so it can diverge from process() for table-heavy docs at quality=standard. A future process(..., per_page=True) that exposes the multi-pass PageResults would close that last gap, but process_streaming covers the realistic cases for now.

Thanks for the library — resolving as already-addressed by process_streaming.

theoriginalpebkac avatar Jun 30 '26 00:06 theoriginalpebkac