Source code for langchain.document_loaders.parsers.pdf
"""Module contains common parsers for PDFs."""
from __future__ import annotations
from typing import TYPE_CHECKING, Any, Iterator, Mapping, Optional, Sequence, Union
from urllib.parse import urlparse
from langchain.document_loaders.base import BaseBlobParser
from langchain.document_loaders.blob_loaders import Blob
from langchain.schema import Document
if TYPE_CHECKING:
    import pdfplumber.page
[docs]class PyPDFParser(BaseBlobParser):
    """Load `PDF` using `pypdf` and chunk at character level."""
[docs]    def __init__(self, password: Optional[Union[str, bytes]] = None):
        self.password = password
[docs]    def lazy_parse(self, blob: Blob) -> Iterator[Document]:
        """Lazily parse the blob."""
        import pypdf
        with blob.as_bytes_io() as pdf_file_obj:
            pdf_reader = pypdf.PdfReader(pdf_file_obj, password=self.password)
            yield from [
                Document(
                    page_content=page.extract_text(),
                    metadata={"source": blob.source, "page": page_number},
                )
                for page_number, page in enumerate(pdf_reader.pages)
            ]
[docs]class PDFMinerParser(BaseBlobParser):
    """Parse `PDF` using `PDFMiner`."""
[docs]    def lazy_parse(self, blob: Blob) -> Iterator[Document]:
        """Lazily parse the blob."""
        from pdfminer.high_level import extract_text
        with blob.as_bytes_io() as pdf_file_obj:
            text = extract_text(pdf_file_obj)
            metadata = {"source": blob.source}
            yield Document(page_content=text, metadata=metadata)
[docs]class PyMuPDFParser(BaseBlobParser):