Files
pdf/gateway/app/routers/render.py
T

350 lines
12 KiB
Python
Raw Normal View History

2026-07-09 10:26:11 +05:30
import hashlib
2026-06-09 10:39:45 +05:30
from typing import Annotated
2026-07-09 10:26:11 +05:30
from fastapi import APIRouter, HTTPException, Path, Query, Request, Response, status
2026-05-16 11:01:20 +05:30
2026-06-29 10:51:04 +05:30
from app.schemas.font import FontInfoResponse
2026-05-22 15:47:48 +05:30
from app.services import engine
2026-06-29 10:51:04 +05:30
from app.services.font import font_info_to_response
2026-07-09 10:26:11 +05:30
from app.services.render_cache import RENDERER_VERSION, RenderMode, TileCacheKey, tile_cache
2026-05-22 15:47:48 +05:30
from app.services.store import document_store
2026-05-16 11:01:20 +05:30
router = APIRouter(prefix="/documents/{document_id}/pages", tags=["render"])
2026-05-22 15:47:48 +05:30
compat_router = APIRouter(tags=["render"])
2026-05-16 11:01:20 +05:30
2026-07-09 10:26:11 +05:30
def _etag(data: bytes) -> str:
return hashlib.sha256(data).hexdigest()[:16]
2026-05-22 15:47:48 +05:30
@router.get("/{page_index}/render")
2026-06-09 10:39:45 +05:30
def render_page(
2026-07-09 10:26:11 +05:30
request: Request,
document_id: str,
page_index: Annotated[int, Path(ge=0)],
dpi: int = 96,
zoom: float = 1.0,
rotation: int = 0,
render_mode: RenderMode = RenderMode.NORMAL
2026-06-09 10:39:45 +05:30
) -> Response:
2026-07-30 16:48:46 +05:30
print(f"[RENDER] document_id={document_id} page_index={page_index} zoom={zoom} dpi={dpi}")
2026-05-22 15:47:48 +05:30
if not engine.is_available():
raise HTTPException(
status_code=status.HTTP_501_NOT_IMPLEMENTED,
detail="Engine bridge (bindings/python) not yet available.",
2026-05-22 15:47:48 +05:30
)
2026-05-22 15:47:48 +05:30
doc_info = document_store.get_document(document_id)
if not doc_info:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Document not found")
2026-05-22 15:47:48 +05:30
try:
doc = doc_info["doc_instance"]
page = doc.get_page(page_index)
2026-07-09 10:26:11 +05:30
key = TileCacheKey(
doc_hash=doc_info.get("doc_hash", ""),
renderer_version=RENDERER_VERSION,
page=page_index,
dpi=dpi,
zoom=zoom,
rotation=rotation,
render_mode=render_mode,
tile_x=0.0,
tile_y=0.0,
tile_w=round(page.width, 2),
tile_h=round(page.height, 2)
)
cached = tile_cache.get(key)
if cached:
etag_val = f'"{_etag(cached)}"'
if request.headers.get("if-none-match") == etag_val:
return Response(status_code=304)
return Response(
content=cached,
media_type="image/png",
headers={"ETag": etag_val, "X-Cache": "HIT", "Cache-Control": "private, max-age=300"}
)
import time
start = time.perf_counter_ns()
2026-05-22 15:47:48 +05:30
img = page.render(dpi)
2026-07-09 10:26:11 +05:30
elapsed = time.perf_counter_ns() - start
tile_cache.put(key, img.data)
tile_cache.record_render_time(elapsed)
etag_val = f'"{_etag(img.data)}"'
return Response(
content=img.data,
media_type="image/png",
headers={"ETag": etag_val, "X-Cache": "MISS", "Cache-Control": "private, max-age=300"}
)
2026-05-22 15:47:48 +05:30
except IndexError:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND, detail="Page index out of bounds"
)
2026-05-22 15:47:48 +05:30
except Exception as e:
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(e))
2026-05-16 11:01:20 +05:30
@router.get("/{page_index}/render-tile")
def render_page_tile(
2026-07-09 10:26:11 +05:30
request: Request,
document_id: str,
page_index: Annotated[int, Path(ge=0)],
x: float,
y: float,
width: float,
height: float,
2026-07-09 10:26:11 +05:30
dpi: int = 96,
zoom: float = 1.0,
rotation: int = 0,
render_mode: RenderMode = RenderMode.NORMAL
) -> Response:
if not engine.is_available():
raise HTTPException(
status_code=status.HTTP_501_NOT_IMPLEMENTED,
detail="Engine bridge (bindings/python) not yet available.",
)
doc_info = document_store.get_document(document_id)
if not doc_info:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Document not found")
try:
doc = doc_info["doc_instance"]
page = doc.get_page(page_index)
2026-07-09 10:26:11 +05:30
key = TileCacheKey(
doc_hash=doc_info.get("doc_hash", ""),
renderer_version=RENDERER_VERSION,
page=page_index,
dpi=dpi,
zoom=zoom,
rotation=rotation,
render_mode=render_mode,
tile_x=round(x, 2),
tile_y=round(y, 2),
tile_w=round(width, 2),
tile_h=round(height, 2)
)
cached = tile_cache.get(key)
if cached:
etag_val = f'"{_etag(cached)}"'
if request.headers.get("if-none-match") == etag_val:
return Response(status_code=304)
return Response(
content=cached,
media_type="image/png",
headers={"ETag": etag_val, "X-Cache": "HIT", "Cache-Control": "private, max-age=300"}
)
import time
start = time.perf_counter_ns()
img_width, img_height, img_data = page.render_tile(dpi, x, y, width, height)
import io
from PIL import Image
img = Image.frombytes("RGBA", (img_width, img_height), img_data)
out_buf = io.BytesIO()
img.save(out_buf, format="PNG")
2026-07-09 10:26:11 +05:30
png_bytes = out_buf.getvalue()
elapsed = time.perf_counter_ns() - start
tile_cache.put(key, png_bytes)
tile_cache.record_render_time(elapsed)
etag_val = f'"{_etag(png_bytes)}"'
return Response(
content=png_bytes,
media_type="image/png",
headers={"ETag": etag_val, "X-Cache": "MISS", "Cache-Control": "private, max-age=300"}
)
except IndexError:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND, detail="Page index out of bounds"
)
except Exception as e:
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(e))
2026-05-22 15:47:48 +05:30
@router.get("/{page_index}/text")
2026-06-05 10:27:39 +05:30
def extract_page_text(document_id: str, page_index: Annotated[int, Path(ge=0)]):
2026-05-22 15:47:48 +05:30
if not engine.is_available():
raise HTTPException(
status_code=status.HTTP_501_NOT_IMPLEMENTED,
detail="Engine bridge (bindings/python) not yet available.",
2026-05-22 15:47:48 +05:30
)
2026-05-22 15:47:48 +05:30
doc_info = document_store.get_document(document_id)
if not doc_info:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Document not found")
2026-05-22 15:47:48 +05:30
try:
doc = doc_info["doc_instance"]
page = doc.get_page(page_index)
text = page.extract_text()
2026-05-26 15:52:04 +05:30
annots = page.extract_annotations_text()
if annots:
text += "\n" + "\n".join(annots)
page_height = page.height
glyphs = page.extract_text_with_bounds()
for g in glyphs:
g["y"] = page_height - (g["y"] + g["h"])
return {"text": text, "glyphs": glyphs}
2026-05-22 15:47:48 +05:30
except IndexError:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND, detail="Page index out of bounds"
)
2026-05-22 15:47:48 +05:30
except Exception as e:
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(e))
2026-05-16 11:01:20 +05:30
2026-05-22 15:47:48 +05:30
@compat_router.get("/render/{document_id}")
def render_page_compat(
2026-07-30 16:48:46 +05:30
request: Request, document_id: str, page: int = 0, zoom: float = 1.0, rotation: int = 0, dpi: int | None = None
) -> Response:
2026-07-30 16:48:46 +05:30
if dpi is None:
dpi = int(96 * zoom)
print(f"[RENDER_COMPAT] document_id={document_id} page={page} zoom={zoom} dpi={dpi}")
2026-07-09 10:26:11 +05:30
return render_page(request, document_id, page, dpi, zoom, rotation, RenderMode.NORMAL)
2026-05-16 11:01:20 +05:30
2026-05-26 17:39:03 +05:30
@router.get("/{page_index}")
2026-06-05 10:27:39 +05:30
def get_page_info(document_id: str, page_index: Annotated[int, Path(ge=0)]):
2026-05-26 17:39:03 +05:30
if not engine.is_available():
raise HTTPException(
status_code=status.HTTP_501_NOT_IMPLEMENTED,
detail="Engine bridge (bindings/python) not yet available.",
2026-05-26 17:39:03 +05:30
)
2026-05-26 17:39:03 +05:30
doc_info = document_store.get_document(document_id)
if not doc_info:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Document not found")
2026-05-26 17:39:03 +05:30
try:
doc = doc_info["doc_instance"]
page = doc.get_page(page_index)
return {"width": page.width, "height": page.height}
except IndexError:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND, detail="Page index out of bounds"
)
2026-05-26 17:39:03 +05:30
except Exception as e:
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(e))
2026-05-26 17:39:03 +05:30
@router.get("/{page_index}/transform/page-to-device")
def transform_page_to_device(
document_id: str,
page_index: int,
x: float,
y: float,
device_width: int,
device_height: int,
rotate: int = 0,
):
2026-05-26 17:39:03 +05:30
if not engine.is_available():
raise HTTPException(
status_code=status.HTTP_501_NOT_IMPLEMENTED, detail="Engine unavailable"
)
2026-05-26 17:39:03 +05:30
doc_info = document_store.get_document(document_id)
if not doc_info:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Document not found")
2026-05-26 17:39:03 +05:30
try:
pdfengine = engine.require()
doc = doc_info["doc_instance"]
page = doc.get_page(page_index)
2026-05-26 17:39:03 +05:30
pt = pdfengine.Point2D(x=x, y=y)
res = page.page_to_device(pt, device_width, device_height, rotate)
return {"x": res.x, "y": res.y}
except Exception as e:
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(e))
2026-05-26 17:39:03 +05:30
@router.get("/{page_index}/transform/device-to-page")
def transform_device_to_page(
document_id: str,
page_index: int,
x: int,
y: int,
device_width: int,
device_height: int,
rotate: int = 0,
):
2026-05-26 17:39:03 +05:30
if not engine.is_available():
raise HTTPException(
status_code=status.HTTP_501_NOT_IMPLEMENTED, detail="Engine unavailable"
)
2026-05-26 17:39:03 +05:30
doc_info = document_store.get_document(document_id)
if not doc_info:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Document not found")
2026-05-26 17:39:03 +05:30
try:
pdfengine = engine.require()
doc = doc_info["doc_instance"]
page = doc.get_page(page_index)
2026-05-26 17:39:03 +05:30
pt = pdfengine.DevicePoint(x=x, y=y)
res = page.device_to_page(pt, device_width, device_height, rotate)
return {"x": res.x, "y": res.y}
except Exception as e:
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(e))
@router.get("/{page_index}/fonts", response_model=list[FontInfoResponse])
def get_page_fonts(document_id: str, page_index: int) -> list[FontInfoResponse]:
2026-05-26 15:55:48 +05:30
if not engine.is_available():
raise HTTPException(
status_code=status.HTTP_501_NOT_IMPLEMENTED,
detail="Engine bridge (bindings/python) not yet available.",
2026-05-26 15:55:48 +05:30
)
2026-05-26 15:55:48 +05:30
doc_info = document_store.get_document(document_id)
if not doc_info:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Document not found")
2026-05-26 15:55:48 +05:30
try:
doc = doc_info["doc_instance"]
page = doc.get_page(page_index)
fonts = page.get_fonts()
2026-06-29 10:51:04 +05:30
return [font_info_to_response(f) for f in fonts]
2026-05-26 15:55:48 +05:30
except IndexError:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Page index out of range")
except Exception as e:
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(e))
2026-06-03 10:51:33 +05:30
@router.get("/{page_index}/fonts/glyph-width")
2026-06-09 10:39:45 +05:30
def get_page_glyph_width(
document_id: str,
page_index: Annotated[int, Path(ge=0)],
font_name: str,
charcode: int,
font_size: Annotated[float, Query(gt=0)] = 12.0,
):
2026-06-03 10:51:33 +05:30
if not engine.is_available():
raise HTTPException(
status_code=status.HTTP_501_NOT_IMPLEMENTED,
2026-06-09 10:39:45 +05:30
detail="Engine bridge (bindings/python) not yet available.",
2026-06-03 10:51:33 +05:30
)
2026-06-09 10:39:45 +05:30
2026-06-03 10:51:33 +05:30
doc_info = document_store.get_document(document_id)
if not doc_info:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Document not found")
2026-06-09 10:39:45 +05:30
2026-06-03 10:51:33 +05:30
try:
doc = doc_info["doc_instance"]
page = doc.get_page(page_index)
width = page.get_glyph_width(font_name, charcode, font_size)
return {"width": width}
except IndexError:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Page index out of range")
except Exception as e:
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(e))