~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
"""Walk a pdfplumber document's pages lazily.
pdfplumber's ``PDF.pages`` builds a ``Page`` for every page of the
document on first access, and ``PDF.close()`` -- which leaving a
``with pdfplumber.open(...)`` block calls -- accesses ``PDF.pages`` too.
A small upload whose page tree declares 100,000 pages therefore costs
O(declared pages) time and memory before any page ceiling is consulted
(#6771). The upload validator (``security/file_upload_validator``)
avoids that by walking pdfminer's page tree with
``PDFPage.create_pages``, which yields one page at a time; this module
does the same for the text-extraction walks, so a walk that stops at
``MAX_PDF_EXTRACTION_PAGES`` builds at most that many pages (plus the
one that tells it there are more).
Callers that walk this way must not use ``with pdfplumber.open(...)``
nor call ``PDF.close()``, which would build every page anyway: they
open the stream themselves, close it themselves, and close each page
they were given.
A walk stopped early knows only a lower bound on the page count. The
page tree's declared ``/Count`` is deliberately not read to fill the
gap: it is normally an indirect reference, and resolving one can loop
inside pdfminer with no limit a caller can impose (a self-reference,
an object-stream entry whose stream is a self-reference, a stream whose
``/Length`` is one), outside the extraction's CPU budget.
"""
from __future__ import annotations
from typing import TYPE_CHECKING, Any, Iterator, Optional
if TYPE_CHECKING:
from .pdf_stream_release import PdfplumberWalkReleaser
def iter_pdfplumber_pages(
pdf: Any, releaser: Optional[PdfplumberWalkReleaser] = None
) -> Iterator[Any]:
"""Yield *pdf*'s pages as pdfplumber ``Page`` objects, one at a time.
The page tree is walked only as far as the caller iterates; each
``Page`` is built exactly as ``PDF.pages`` builds it (same page
number and running ``initial_doctop``). An error the page-tree walk
raises is re-raised as ``PdfminerException``, as ``PDF.pages`` does.
Building a page resolves its ``/Resources``, ``/Contents`` and the
page-tree nodes above it, which caches those objects on the
document. When a *releaser* is given, its ``before_page()`` is
called here, just before each page is built, so what building the
page caches is inside its window and counted by the ``after_page()``
the caller makes once done with the page. The caller then must not
call ``before_page()`` itself: that would move the window's start
past the page's construction.
"""
from pdfminer.pdfpage import PDFPage
from pdfplumber.page import Page
from pdfplumber.utils.exceptions import PdfminerException
generator = PDFPage.create_pages(pdf.doc)
doctop = 0
page_number = 0
while True:
if releaser is not None:
releaser.before_page()
try:
page_obj = next(generator)
except StopIteration:
return
except Exception as exc:
raise PdfminerException(exc) from exc
page_number += 1
page = Page(
pdf, page_obj, page_number=page_number, initial_doctop=doctop
)
doctop += page.height
yield page