| import os |
| import pymupdf4llm |
| from langchain_core.documents import Document |
| from typing import List |
|
|
| class PDFLoader: |
| def __init__(self, file_path: str): |
| if not os.path.exists(file_path): |
| raise FileNotFoundError(f"The file {file_path} does not exist.") |
| self.file_path = file_path |
|
|
| def load(self) -> List[Document]: |
| documents = [] |
| page_markdowns = pymupdf4llm.to_markdown(self.file_path, page_chunks=True) |
| for i, md_output in enumerate(page_markdowns): |
| content = str(md_output) |
| doc = Document( |
| page_content=content, |
| metadata={ |
| "page": i + 1, |
| "source": self.file_path, |
| "source_file": os.path.basename(self.file_path), |
| } |
| ) |
| documents.append(doc) |
| return documents |