52 lines
1.6 KiB
Python
52 lines
1.6 KiB
Python
"""Convert PDF files to per-page images using PyMuPDF (no poppler dependency)."""
|
|
|
|
import uuid
|
|
from datetime import date
|
|
from pathlib import Path
|
|
|
|
from config import UPLOAD_DIR
|
|
|
|
# Maximum pages we will extract from a single PDF upload.
|
|
# Beyond this limit pages are silently dropped to prevent runaway queue growth.
|
|
MAX_PDF_PAGES = 200
|
|
|
|
|
|
def pdf_to_images(pdf_bytes: bytes, original_name: str) -> list[dict]:
|
|
"""
|
|
Convert a PDF to individual page images.
|
|
|
|
Each page is rendered at 250 DPI — sufficient for Arabic OCR while keeping
|
|
file sizes manageable. For scanned/image-only PDFs PyMuPDF simply renders
|
|
the embedded raster; no additional image extraction step is needed.
|
|
|
|
Returns list of dicts: [{"image_path": "relative/path.png", "page_number": 1, ...}, ...]
|
|
"""
|
|
import fitz # PyMuPDF
|
|
|
|
doc = fitz.open(stream=pdf_bytes, filetype="pdf")
|
|
total_pages = len(doc)
|
|
group_id = uuid.uuid4().hex
|
|
today = date.today().isoformat()
|
|
dest_dir = Path(UPLOAD_DIR) / today
|
|
dest_dir.mkdir(parents=True, exist_ok=True)
|
|
|
|
pages = []
|
|
for page_num in range(min(total_pages, MAX_PDF_PAGES)):
|
|
page = doc[page_num]
|
|
# 250 DPI gives sharper Arabic text while staying under ~2 MB per PNG
|
|
pix = page.get_pixmap(dpi=250)
|
|
|
|
filename = f"{group_id}_p{page_num + 1}.png"
|
|
dest = dest_dir / filename
|
|
pix.save(str(dest))
|
|
|
|
pages.append({
|
|
"image_path": str(Path(today) / filename),
|
|
"page_number": page_num + 1,
|
|
"pdf_group_id": group_id,
|
|
"total_pages": total_pages,
|
|
})
|
|
|
|
doc.close()
|
|
return pages
|