File size: 2,750 Bytes
26b8deb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
"""PDF text extraction using PyMuPDF with OCR fallback."""
from __future__ import annotations
import io
import logging
from typing import Tuple

try:
    import fitz  # PyMuPDF
    FITZ_AVAILABLE = True
except ImportError:
    FITZ_AVAILABLE = False

try:
    from PIL import Image
    PIL_AVAILABLE = True
except ImportError:
    PIL_AVAILABLE = False

from .ocr import ocr_image

logger = logging.getLogger(__name__)

MIN_TEXT_LENGTH = 50  # chars


def extract_native_text(pdf_path: str) -> Tuple[str, int]:
    """Extract native text from a PDF. Returns (text, page_count)."""
    if not FITZ_AVAILABLE:
        raise RuntimeError("PyMuPDF (fitz) is not installed. Run: pip install pymupdf")
    doc = fitz.open(pdf_path)
    page_count = len(doc)
    pages = []
    for i, page in enumerate(doc):
        text = page.get_text("text")
        if text.strip():
            pages.append(f"--- Page {i + 1} ---\n{text.strip()}")
    doc.close()
    return "\n\n".join(pages), page_count


def render_pdf_page_to_image(pdf_path: str, page_index: int, dpi: int = 200) -> "Image.Image":
    """Render a PDF page to a PIL Image for OCR."""
    if not FITZ_AVAILABLE:
        raise RuntimeError("PyMuPDF is not installed.")
    if not PIL_AVAILABLE:
        raise RuntimeError("Pillow is not installed.")
    doc = fitz.open(pdf_path)
    page = doc[page_index]
    mat = fitz.Matrix(dpi / 72, dpi / 72)
    pix = page.get_pixmap(matrix=mat, colorspace=fitz.csGRAY)
    img_data = pix.tobytes("png")
    doc.close()
    return Image.open(io.BytesIO(img_data))


def extract_text_from_pdf(pdf_path: str) -> Tuple[str, str, int]:
    """
    Extract text from a PDF, falling back to OCR if native text is insufficient.
    Returns (text, method, page_count).
    method: 'native_pdf_text' or 'ocr'
    """
    if not FITZ_AVAILABLE:
        raise RuntimeError("PyMuPDF is not installed.")

    native_text, page_count = extract_native_text(pdf_path)

    if len(native_text.strip()) >= MIN_TEXT_LENGTH:
        logger.info("Using native PDF text extraction (%d chars)", len(native_text))
        return native_text, "native_pdf_text", page_count

    logger.info("Native text insufficient (%d chars); falling back to OCR", len(native_text))
    doc = fitz.open(pdf_path)
    n_pages = len(doc)
    doc.close()

    ocr_pages = []
    for i in range(n_pages):
        try:
            img = render_pdf_page_to_image(pdf_path, i)
            page_text = ocr_image(img)
            ocr_pages.append(f"--- Page {i + 1} ---\n{page_text}")
        except Exception as e:
            logger.warning("OCR failed on page %d: %s", i + 1, e)
            ocr_pages.append(f"--- Page {i + 1} ---\n[OCR failed]")

    return "\n\n".join(ocr_pages), "ocr", page_count