File size: 774 Bytes
a9d5e1b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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)