extract_pdf_pages
Extract specific pages from PDF files to create new documents. Supports both local files and URLs, allowing you to select and save only the pages you need.
Instructions
Extract specific pages from a PDF and create a new PDF.
Supports URLs for source PDF. The source PDF will be downloaded to a temporary
directory if it's a URL. Output path must be a local file path.
Args:
source_path: Path to the source PDF file or URL to PDF
page_numbers: List of page numbers to extract (1-indexed)
output_path: Path where the new PDF will be saved (must be local path)
Returns:
Success message with extraction details or error messageInput Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| source_path | Yes | ||
| page_numbers | Yes | ||
| output_path | Yes |
Implementation Reference
- pdf_tools_mcp/server.py:551-613 (handler)The `extract_pdf_pages` function is defined as an MCP tool and handles the logic for extracting specific pages from a PDF file.
async def extract_pdf_pages(source_path: str, page_numbers: List[int], output_path: str) -> str: """Extract specific pages from a PDF and create a new PDF. Supports URLs for source PDF. The source PDF will be downloaded to a temporary directory if it's a URL. Output path must be a local file path. Args: source_path: Path to the source PDF file or URL to PDF page_numbers: List of page numbers to extract (1-indexed) output_path: Path where the new PDF will be saved (must be local path) Returns: Success message with extraction details or error message """ # Resolve source path (download if URL) try: actual_source_path = resolve_path(source_path) # Validate local path if not URL if not is_url(source_path): is_valid, error_msg = validate_path(source_path) if not is_valid: return error_msg except Exception as e: return f"Error resolving source path: {str(e)}" # Validate output path (must be local) if is_url(output_path): return "Error: Output path cannot be a URL, must be a local file path" is_valid, error_msg = validate_path(output_path) if not is_valid: return error_msg try: with open(actual_source_path, 'rb') as source_file: pdf_reader = PyPDF2.PdfReader(source_file) total_pages = len(pdf_reader.pages) pdf_writer = PyPDF2.PdfWriter() extracted_pages = [] for page_num in page_numbers: if 1 <= page_num <= total_pages: pdf_writer.add_page(pdf_reader.pages[page_num - 1]) extracted_pages.append(page_num) else: logging.warning(f"Page {page_num} is out of range (1-{total_pages}), skipping") if not extracted_pages: return f"Error: No valid pages to extract from PDF (total pages: {total_pages})" # Write the new PDF with open(output_path, 'wb') as output_file: pdf_writer.write(output_file) return f"Successfully extracted {len(extracted_pages)} pages from '{source_path}' to '{output_path}'\nExtracted pages: {extracted_pages}\nSource PDF total pages: {total_pages}" except FileNotFoundError: return f"Error: File not found '{actual_source_path}'" except Exception as e: return f"Error extracting pages: {str(e)}"