File size: 881 Bytes
fd49a93
ae11f2d
fd49a93
 
 
 
 
ae11f2d
 
fd49a93
 
ae11f2d
fd49a93
ae11f2d
 
 
 
 
 
 
 
 
 
 
 
fd49a93
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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