Spaces:
Runtime error
Runtime error
File size: 506 Bytes
ae379dd | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | import io
import fitz # PyMuPDF
def pdf_to_image_bytes(pdf_bytes: bytes, dpi: int = 200) -> bytes:
"""Render the first page of a PDF to JPEG bytes."""
doc = fitz.open(stream=pdf_bytes, filetype="pdf")
if doc.page_count == 0:
raise ValueError("The uploaded PDF has no pages.")
page = doc.load_page(0)
mat = fitz.Matrix(dpi / 72, dpi / 72)
pix = page.get_pixmap(matrix=mat, colorspace=fitz.csRGB)
buffer = io.BytesIO(pix.tobytes("jpeg"))
return buffer.getvalue()
|