aeat.adapters.inbound.declaracion._parsers._pdfplumber_backend module

Pdfplumber-backed page text extraction for declaración PDFs.

Wraps pdfplumber behind extract_pages_text() and extract_pages_text_from_bytes(), which return one stripped text string per page. Errors from the underlying library and pathological inputs (missing file, scan-only PDF without an OCR layer) are translated into DeclaracionParseError.

A pypdfium2 fast path is consulted before the canonical pdfplumber primitive. The fast path only commits its output when at least one declaration-content canary (NIF row or declarant row) matches; this keeps unrelated PDFs out of the fast lane and avoids cache poisoning. The bytes route follows the same canary discipline without writing decrypted content to disk.

extract_pages_text(pdf_path)[source]

Extract the text of each page in order.

The declaration backend first tries the pypdfium2 fast path and falls back to the shared pdfplumber primitive when the fast path is unavailable or its declaration canaries do not match.

Parameters:

pdf_path (Path) – Filesystem path of the PDF to read.

Return type:

tuple[str, ...]

Returns:

Tuple with one stripped string per page in the source order. Empty pages preserve their slot as the empty string.

Raises:

DeclaracionParseError – When the PDF is missing or no extractable text can be read from any page.

extract_pages_text_from_bytes(pdf_bytes, *, source_label='in-memory PDF')[source]

Extract text from PDF bytes without materialising a plaintext file.

The bytes path mirrors extract_pages_text: pypdfium2 gets the first chance to return canary-validated page text, then the shared pdfplumber bytes primitive handles the fallback while keeping the caller’s decrypted bytes in memory.

Raises:

DeclaracionParseError – When no extractable text can be read from the supplied PDF bytes.

Return type:

tuple[str, ...]

Parameters: