| 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, |
| ) |
|
|