render_pdf_page
Generate a high-quality image of a specified PDF page by providing the file path, page number, and zoom factor. Ideal for previewing or extracting PDF content without annotations or highlights.
Instructions
Generate an image of a PDF page without any highlighting
Args:
pdf_path: Path to the PDF file
page_num: Page number to render (0-indexed)
zoom: Zoom factor for rendering (higher values for better quality)
Returns:
Image of the specified page
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| page_num | No | ||
| pdf_path | Yes | ||
| zoom | No |
Implementation Reference
- mcp_pdf_forms/server.py:204-244 (handler)The handler function for the 'render_pdf_page' tool. It opens the PDF using PyMuPDF (fitz), renders the specified page at the given zoom level as a pixmap, converts it to a PIL Image, then to PNG bytes, and returns an MCP Image object. Includes error handling for invalid page numbers and other exceptions. The @mcp.tool() decorator also handles registration.@mcp.tool() def render_pdf_page(pdf_path: str, page_num: int = 0, zoom: float = 2.0) -> Image: """ Generate an image of a PDF page without any highlighting Args: pdf_path: Path to the PDF file page_num: Page number to render (0-indexed) zoom: Zoom factor for rendering (higher values for better quality) Returns: Image of the specified page """ try: doc = fitz.open(pdf_path) # Check if page number is valid if page_num < 0 or page_num >= len(doc): raise ValueError(f"Page number {page_num} is out of range (0-{len(doc)-1})") # Get the requested page page = doc[page_num] # Render the page as an image mat = fitz.Matrix(zoom, zoom) pix = page.get_pixmap(matrix=mat) # Convert to PIL Image img = PILImage.frombytes("RGB", [pix.width, pix.height], pix.samples) # Convert to bytes buffer = io.BytesIO() img.save(buffer, format="PNG") img_bytes = buffer.getvalue() doc.close() # Return MCP Image object return Image(data=img_bytes, format="png") except Exception as e: raise Exception(f"Error rendering PDF page: {str(e)}")