203 lines
6.9 KiB
Python
203 lines
6.9 KiB
Python
from fastapi import APIRouter, HTTPException, Response, status
|
|
|
|
from app.routers.documents import FontInfoResponse
|
|
from app.services import engine
|
|
from app.services.store import document_store
|
|
|
|
router = APIRouter(prefix="/documents/{document_id}/pages", tags=["render"])
|
|
compat_router = APIRouter(tags=["render"])
|
|
|
|
|
|
@router.get("/{page_index}/render")
|
|
def render_page(document_id: str, page_index: int, dpi: int = 96) -> 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)
|
|
img = page.render(dpi)
|
|
return Response(content=img.data, media_type="image/png")
|
|
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))
|
|
|
|
|
|
@router.get("/{page_index}/text")
|
|
def extract_page_text(document_id: str, page_index: int):
|
|
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)
|
|
text = page.extract_text()
|
|
annots = page.extract_annotations_text()
|
|
if annots:
|
|
text += "\n" + "\n".join(annots)
|
|
glyphs = page.extract_text_with_bounds()
|
|
return {"text": text, "glyphs": glyphs}
|
|
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))
|
|
|
|
|
|
@compat_router.get("/render/{document_id}")
|
|
def render_page_compat(
|
|
document_id: str, page: int = 0, zoom: float = 1.0, rotation: int = 0
|
|
) -> Response:
|
|
dpi = int(96 * zoom)
|
|
return render_page(document_id, page, dpi)
|
|
|
|
|
|
@router.get("/{page_index}")
|
|
def get_page_info(document_id: str, page_index: int):
|
|
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)
|
|
return {"width": page.width, "height": page.height}
|
|
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))
|
|
|
|
|
|
@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,
|
|
):
|
|
if not engine.is_available():
|
|
raise HTTPException(
|
|
status_code=status.HTTP_501_NOT_IMPLEMENTED, detail="Engine unavailable"
|
|
)
|
|
|
|
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:
|
|
pdfengine = engine.require()
|
|
doc = doc_info["doc_instance"]
|
|
page = doc.get_page(page_index)
|
|
|
|
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))
|
|
|
|
|
|
@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,
|
|
):
|
|
if not engine.is_available():
|
|
raise HTTPException(
|
|
status_code=status.HTTP_501_NOT_IMPLEMENTED, detail="Engine unavailable"
|
|
)
|
|
|
|
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:
|
|
pdfengine = engine.require()
|
|
doc = doc_info["doc_instance"]
|
|
page = doc.get_page(page_index)
|
|
|
|
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]:
|
|
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)
|
|
fonts = page.get_fonts()
|
|
return [
|
|
FontInfoResponse(
|
|
fontName=f.font_name,
|
|
type=f.type,
|
|
isEmbedded=f.is_embedded,
|
|
isSubset=f.is_subset,
|
|
isVertical=f.is_vertical,
|
|
encoding=f.encoding,
|
|
hasToUnicode=f.has_to_unicode,
|
|
cmapName=f.cmap_name,
|
|
cidSystemInfo=f.cid_system_info,
|
|
subsetTag=f.subset_tag,
|
|
sourceType=f.source_type,
|
|
substitutedFrom=f.substituted_from,
|
|
substitutedTo=f.substituted_to,
|
|
normalizedFamily=f.normalized_family,
|
|
internalFontId=f.internal_font_id,
|
|
flags=f.flags,
|
|
ascent=f.ascent,
|
|
descent=f.descent,
|
|
capHeight=f.cap_height,
|
|
)
|
|
for f in fonts
|
|
]
|
|
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))
|