Spaces:
Sleeping
Sleeping
File size: 755 Bytes
cf25e9f | 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 | from pypdf import PdfReader
from fastapi import HTTPException, status
import io
def extract_text_from_pdf(file_bytes: bytes) -> str:
try:
reader = PdfReader(io.BytesIO(file_bytes))
text = ""
for page in reader.pages:
text += page.extract_text() or ""
if not text.strip():
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="Could not extract text from PDF. The file may be scanned/image-based."
)
return text.strip()
except HTTPException:
raise
except Exception:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="Invalid or corrupted PDF file"
) |