Skip to main content
Glama

pdf_rotate_page

Rotate specific pages in PDF documents to correct orientation or adjust layout. Specify page number and rotation angle to modify PDF page positioning.

Instructions

Rotate a page in a PDF.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
pdf_pathYes
page_numberYes
rotationYes

Implementation Reference

  • The @mcp.tool() decorator registers the pdf_rotate_page function as an MCP tool in the FastMCP server.
    @mcp.tool()
  • The core implementation of the pdf_rotate_page tool. Validates input parameters, opens the PDF using PyMuPDF (fitz), rotates the specified page by the given angle (90, 180, or 270 degrees), generates a new timestamped output filename, saves the modified PDF, and returns a success message with the output path.
    async def pdf_rotate_page( pdf_path: str, page_number: int, rotation: int ) -> str: """Rotate a page in a PDF.""" if not os.path.exists(pdf_path): return f"Error: PDF file not found: {pdf_path}" if not validate_pdf_file(pdf_path): return f"Error: Invalid PDF file: {pdf_path}" if rotation not in [90, 180, 270]: return f"Error: Invalid rotation angle. Must be 90, 180, or 270 degrees." try: # Open PDF document doc = fitz.open(pdf_path) # Validate page number if not validate_page_number(doc, page_number): doc.close() return f"Error: Invalid page number {page_number}. Document has {len(doc)} pages." # Get the page and rotate it page = doc[page_number] page.set_rotation(rotation) # Generate output filename output_path = generate_output_filename(pdf_path) # Save the modified PDF doc.save(output_path) doc.close() return f"Successfully rotated page {page_number + 1} by {rotation} degrees. Output saved to: {output_path}" except Exception as e: return f"Error rotating page: {str(e)}"
  • Supporting utility functions used by the pdf_rotate_page handler: generate_output_filename (creates timestamped output paths), validate_pdf_file (checks if file is valid PDF), and validate_page_number (ensures page index is valid).
    def generate_output_filename(input_path: str, suffix: str = "modified") -> str: """Generate a new filename with timestamp to avoid overwriting originals.""" path = Path(input_path) timestamp = datetime.now().strftime("%Y%m%d_%H%M%S") return str(path.parent / f"{path.stem}_{suffix}_{timestamp}{path.suffix}") def validate_pdf_file(pdf_path: str) -> bool: """Validate that the file is a valid PDF.""" try: doc = fitz.open(pdf_path) doc.close() return True except Exception: return False def validate_page_number(doc: fitz.Document, page_num: int) -> bool: """Validate that the page number exists in the document.""" return 0 <= page_num < len(doc)

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/andr3medeiros/pdf-manipulation-mcp-server'

If you have feedback or need assistance with the MCP directory API, please join our Discord server