Skip to content

PyMuPDF loader

This loader is available on main after release 0.1.14. Until the next package release, install from source with the optional parser:

pip install "git+https://github.com/enoch3712/ExtractThinker.git" pymupdf

PyMuPDF is an optional dependency with its own license terms.

from extract_thinker import DocumentLoaderPyMuPDF, PyMuPDFConfig

loader = DocumentLoaderPyMuPDF(PyMuPDFConfig(
    include_bbox=True,
    extract_tables=True,
    vision_enabled=True,
    dpi=150,
))
pages = loader.load("invoice.pdf")

The loader accepts a PDF path or BytesIO stream. Each returned page contains content and a one-based page_number. Enable extract_tables for a tables list, include_bbox for regions, and vision mode for PNG image bytes.

This loader reads the PDF's existing text layer; it does not perform OCR on scanned text. Choose an OCR loader or use vision extraction for image-only PDFs. Native PyMuPDF calls from this adapter are serialized across threads.

Configuration Default Meaning
cache_ttl 300 Cache duration in seconds
password None Password for encrypted PDFs
extract_tables False Include tables detected by PyMuPDF
include_bbox False Include source text blocks and normalized coordinates
vision_enabled False Render a PNG of each page
dpi 150 Image resolution, a positive integer

loader.set_max_image_size(1200) bounds the rendered image's longest edge. loader.load_pages("invoice.pdf", [3, 1]) selects pages in the requested order. Selection currently loads the document before filtering.

Source coordinates

Each regions entry can be parsed as DocumentRegion:

from extract_thinker import DocumentRegion

region = DocumentRegion.model_validate(pages[0]["regions"][0])
print(region.text)
print(region.bounding_box.page, region.bounding_box.x0, region.bounding_box.y0)

Coordinates are fractions between 0 and 1, measured from the top-left of the rendered page, including its rotation. x0, y0, x1, y1 mean left, top, right and bottom. The loader supplies these coordinates; they are not estimates generated by an LLM. Empty pages have an empty regions list.

Extractor preserves these source regions and table metadata in model input. When asking an LLM to associate a field with a source region, verify that association separately; a valid rectangle does not prove a correct extraction.