"""Scanned PDF to JSON: each page's text, and each word with its box."""

import json
import sys

import pymupdf
import pytesseract
from PIL import Image


def ocr_words(page, dpi=300):
    pix = page.get_pixmap(dpi=dpi)
    img = Image.frombytes("RGB", (pix.width, pix.height), pix.samples)
    d = pytesseract.image_to_data(img, output_type=pytesseract.Output.DICT)
    k = 72 / dpi  # pixels to PDF points
    for i, text in enumerate(d["text"]):
        if text.strip():  # skip page, block, paragraph and line rows
            x, y, w, h = d["left"][i], d["top"][i], d["width"][i], d["height"][i]
            yield {
                "text": text,
                "conf": float(d["conf"][i]),
                "box": [round(v * k, 1) for v in (x, y, x + w, y + h)],
                "line": [d["block_num"][i], d["par_num"][i], d["line_num"][i]],
            }


def page_entry(page):
    if page.get_text().strip():  # a text layer: read it, no OCR
        source = "text layer"
        words = [
            {"text": w[4], "box": [round(v, 1) for v in w[:4]], "line": [w[5], w[6]]}
            for w in page.get_text("words")
        ]
    else:
        source, words = "ocr", list(ocr_words(page))
    lines = {}
    for w in words:
        lines.setdefault(tuple(w["line"]), []).append(w["text"])
    text = "\n".join(" ".join(ws) for ws in lines.values())
    return {"page": page.number + 1, "source": source, "text": text, "words": words}


with pymupdf.open(sys.argv[1]) as doc:
    pages = [page_entry(p) for p in doc]
with open(sys.argv[2], "w", encoding="utf-8") as f:
    json.dump({"file": sys.argv[1], "pages": pages}, f, ensure_ascii=False, indent=1)
