Spaces:
Sleeping

PK_LegalAssitant / src /utils /pdf_loader.py
Mehreen44's picture
Upload 5 files
0c0884c verified
Raw History Blame Contribute Delete
882 Bytes
import os
import fitz
def load_pdf_text(file_path: str) -> str:
"""
Reads a PDF file using PyMuPDF (fitz), extracts all text page by page,
and returns the combined text.
Args:
file_path (str): Absolute or relative path to the PDF file.
Returns:
str: Extracted raw text.
"""
if not os.path.exists(file_path):
raise FileNotFoundError(f"PDF file not found: {file_path}")
try:
doc = fitz.open(file_path)
text_list = []
for page_num in range(len(doc)):
page = doc.load_page(page_num)
text_list.append(page.get_text())
doc.close()
return "\n".join(text_list)
except Exception as e:
print(f"Error reading PDF {file_path}: {str(e)}")
raise RuntimeError(f"Failed to load PDF {file_path}: {str(e)}")