Skip to main content
Glama

extract-pages

Extract selected pages from a PDF document to create a new file. Specify input and output paths along with the desired page numbers for precise extraction.

Instructions

Extract specific pages from a PDF file

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
input_pathYesInput PDF file path
output_pathYesOutput path for new PDF
pagesYesList of page numbers to extract (1-based indexing)

Implementation Reference

  • Handler for the 'extract-pages' tool: extracts specified pages (1-based) from input PDF using PyPDF2, writes to output file, handles invalid pages and errors.
    elif name == "extract-pages": input_path = arguments.get("input_path") output_path = arguments.get("output_path") pages = arguments.get("pages", []) if not input_path or not output_path or not pages: raise ValueError("Missing required arguments") try: reader = PyPDF2.PdfReader(input_path) writer = PyPDF2.PdfWriter() # Convert 1-based page numbers to 0-based indices for page_num in pages: if 1 <= page_num <= len(reader.pages): writer.add_page(reader.pages[page_num - 1]) else: return [types.TextContent( type="text", text=f"Error: Page number {page_num} is out of range" )] # Write the extracted pages to the output file with open(output_path, 'wb') as output_file: writer.write(output_file) return [types.TextContent( type="text", text=f"Successfully extracted {len(pages)} pages to {output_path}" )] except Exception as e: return [types.TextContent( type="text", text=f"Error extracting pages: {str(e)}" )]
  • Registration of the 'extract-pages' tool in server.list_tools(), including name, description, and input schema.
    types.Tool( name="extract-pages", description="Extract specific pages from a PDF file", inputSchema={ "type": "object", "properties": { "input_path": { "type": "string", "description": "Input PDF file path" }, "output_path": { "type": "string", "description": "Output path for new PDF" }, "pages": { "type": "array", "items": {"type": "integer"}, "description": "List of page numbers to extract (1-based indexing)" } }, "required": ["input_path", "output_path", "pages"] } ),
  • Input schema for 'extract-pages' tool defining parameters: input_path (string), output_path (string), pages (array of integers).
    inputSchema={ "type": "object", "properties": { "input_path": { "type": "string", "description": "Input PDF file path" }, "output_path": { "type": "string", "description": "Output path for new PDF" }, "pages": { "type": "array", "items": {"type": "integer"}, "description": "List of page numbers to extract (1-based indexing)" } }, "required": ["input_path", "output_path", "pages"] }

Other Tools

Related Tools

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/hanweg/mcp-pdf-tools'

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