32 lines
885 B
Python
32 lines
885 B
Python
"""Rasterize a PDF page to PNG for layout ML / OCR crops."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
|
|
from app.services.convert.pdf_bridge import render_page_png
|
|
from app.services.convert.validation import run_with_timeout
|
|
|
|
|
|
def layout_ml_dpi(default: float = 150.0) -> int:
|
|
try:
|
|
return max(72, int(float(os.environ.get("CONVERT_LAYOUT_ML_DPI", str(default)))))
|
|
except ValueError:
|
|
return int(default)
|
|
|
|
|
|
def raster_page(
|
|
pdf_bytes: bytes,
|
|
page_index: int,
|
|
*,
|
|
dpi: int | None = None,
|
|
timeout: float = 15.0,
|
|
) -> bytes:
|
|
"""Return PNG bytes. Raises on hard failure (caller should fail-open)."""
|
|
d = dpi if dpi is not None else layout_ml_dpi()
|
|
return run_with_timeout(
|
|
lambda: render_page_png(pdf_bytes, page_index, dpi=d, allow_blank=True),
|
|
timeout,
|
|
label=f"layout raster page {page_index + 1}",
|
|
)
|