Spaces:
Sleeping
Sleeping
| from fastapi import FastAPI, HTTPException, Request | |
| from fastapi.responses import Response | |
| import fitz | |
| import io | |
| # We will need to inject this into app.py | |
| def serve_page_image(pdf_path: str, page_num: int, dpi: int = 150): | |
| try: | |
| doc = fitz.open(pdf_path) | |
| if page_num < 1 or page_num > doc.page_count: | |
| return Response(status_code=404) | |
| page = doc.load_page(page_num - 1) | |
| zoom = dpi / 72.0 | |
| mat = fitz.Matrix(zoom, zoom) | |
| pix = page.get_pixmap(matrix=mat, alpha=False) | |
| img_bytes = pix.tobytes("png") | |
| doc.close() | |
| return Response(content=img_bytes, media_type="image/png") | |
| except Exception as e: | |
| print(f"Error serving page: {e}") | |
| return Response(status_code=500) | |