Source code for aeat.adapters.inbound.justificante._parsers._pdfplumber_backend
"""pdfplumber-backed text extraction for justificante PDFs.Implements the ``PDFPLUMBER`` branch of:class:`~domain.justificante.JustificanteParserBackend` for the:mod:`adapters.inbound.justificante._parsers` dispatch layer.Concatenates the ``page.extract_text()`` output of every non-empty page;layout-sensitive parsing is left to the regex extractor downstream.Both path and bytes helpers translate extraction failures into:class:`~domain.justificante.JustificanteParseError`. The bytes helperuses the shared inbound PDF bytes primitive so secure-storage captures can beparsed without plaintext temporary files."""from__future__importannotationsfrompathlibimportPathfrom.....domain.justificanteimportJustificanteParseErrorfrom...pdfimport(extract_pages_text_concatenated,extract_pages_text_from_bytes,)
[docs]defextract_text_pdfplumber(pdf_path:Path)->str:"""Return the concatenated text of ``pdf_path`` using pdfplumber. Args: pdf_path: Path to the PDF to open. Returns: A single string with every page's ``extract_text`` result joined by newlines. Empty pages are skipped. Raises: JustificanteParseError: If pdfplumber cannot read any text. """returnextract_pages_text_concatenated(pdf_path,error_class=JustificanteParseError)
[docs]defextract_text_pdfplumber_bytes(pdf_bytes:bytes)->str:"""Return concatenated text from in-memory PDF bytes using pdfplumber. Raises: JustificanteParseError: If pdfplumber cannot read any text. """return"\n".join(extract_pages_text_from_bytes(pdf_bytes,error_class=JustificanteParseError,pdf_label="PDF",source_label="in-memory justificante PDF",))