File size: 1,328 Bytes
734b5b4 | 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 27 28 29 30 31 32 33 34 35 36 37 38 | from __future__ import annotations
from pathlib import Path
import fitz
from src.core.models import InspectionReport
class OfflinePDFStructureInspector:
name = "structure_inspector"
def inspect(self, pdf_path: Path) -> InspectionReport:
doc = fitz.open(pdf_path)
pages, ocr_required = [], []
has_text, has_images = False, False
metadata = dict(doc.metadata or {})
for i, page in enumerate(doc):
text = page.get_text("text").strip()
images = page.get_images(full=True)
drawings = page.get_drawings()
scanned = bool(images) and len(text) < 30
if text: has_text = True
if images: has_images = True
if scanned: ocr_required.append(i)
pages.append({
"page_index": i,
"text_chars": len(text),
"image_count": len(images),
"drawing_count": len(drawings),
"ocr_required": scanned,
"page_size": [page.rect.width, page.rect.height],
})
return InspectionReport(
page_count=doc.page_count,
metadata=metadata,
pages=pages,
ocr_required_pages=ocr_required,
has_text=has_text,
has_images=has_images,
)
|