Skip to main content
Glama

pdf_split

Split PDF files into individual pages or specific page ranges to extract, organize, or share selected content from larger documents.

Instructions

Split a PDF into individual pages or page ranges.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
pdf_pathYes
output_dirNo
page_rangesNo

Implementation Reference

  • The core implementation of the 'pdf_split' tool. This async function handles splitting a PDF into individual pages or custom page ranges, saving them to an output directory. It uses PyMuPDF (fitz) for PDF manipulation and includes validation and error handling. The @mcp.tool() decorator automatically generates the schema from parameters and registers the tool with the FastMCP server.
    @mcp.tool() async def pdf_split( pdf_path: str, output_dir: Optional[str] = None, page_ranges: Optional[List[Dict[str, int]]] = None ) -> str: """Split a PDF into individual pages or page ranges.""" 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}" try: # Open PDF document doc = fitz.open(pdf_path) # Determine output directory if not output_dir: pdf_file = Path(pdf_path) output_dir = str(pdf_file.parent / f"{pdf_file.stem}_split") # Create output directory os.makedirs(output_dir, exist_ok=True) split_files = [] if page_ranges: # Split by page ranges for i, page_range in enumerate(page_ranges): start_page = page_range["start"] end_page = page_range["end"] # Validate page range if start_page < 0 or end_page >= len(doc) or start_page > end_page: doc.close() return f"Error: Invalid page range {start_page}-{end_page}" # Create new document for this range new_doc = fitz.open() new_doc.insert_pdf(doc, from_page=start_page, to_page=end_page) # Save split PDF output_file = os.path.join(output_dir, f"pages_{start_page + 1}_to_{end_page + 1}.pdf") new_doc.save(output_file) new_doc.close() split_files.append(output_file) else: # Split into individual pages for page_num in range(len(doc)): new_doc = fitz.open() new_doc.insert_pdf(doc, from_page=page_num, to_page=page_num) output_file = os.path.join(output_dir, f"page_{page_num + 1}.pdf") new_doc.save(output_file) new_doc.close() split_files.append(output_file) doc.close() return f"Successfully split PDF into {len(split_files)} files. Output directory: {output_dir}" except Exception as e: return f"Error splitting PDF: {str(e)}"

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