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
| Name | Required | Description | Default |
|---|---|---|---|
| input_path | Yes | Input PDF file path | |
| output_path | Yes | Output path for new PDF | |
| pages | Yes | List of page numbers to extract (1-based indexing) |
Implementation Reference
- src/pdf_tools/server.py:184-219 (handler)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)}" )]
- src/pdf_tools/server.py:40-62 (registration)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"] } ),
- src/pdf_tools/server.py:43-61 (schema)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"] }