PDF Processing
Overview
PDF processing involves extracting content from existing PDFs, creating new PDF documents, and modifying or transforming PDF files. This skill should be invoked when working with PDF files for operations like text extraction, table data extraction, document generation, merging, splitting, or watermarking.
Core Principles
- Library Selection: Choose the right library for your specific task (pypdf, pdfplumber, reportlab)
- Text Extraction: Use pdfplumber for text with layout preservation, pypdf for simple extraction
- Creation: Use reportlab for programmatic PDF generation with precise control
- Transformation: Understand that PDFs are final-form documents - editing is limited
Preparation Checklist
Step-by-Step Process
- Read/Load: Open the PDF file using appropriate library
- Extract/Process: Get text, tables, or metadata as needed
- Transform: Apply any modifications (merge, split, rotate, watermark)
- Create: If generating new PDF, build content programmatically
- Save: Write output to new file or return processed content
Do's and Don'ts
- ✅ Do use pdfplumber for tables - it excels at table extraction
- ✅ Do handle encoding issues when extracting international text
- ✅ Do close files properly to release resources
- ❌ Don't expect perfect formatting preservation when extracting text
- ❌ Don't attempt complex layout editing - PDFs aren't designed for that
- ❌ Don't skip error handling for corrupted or password-protected PDFs
Anti-Patterns
- OCR Blindness: Not using OCR when dealing with scanned documents
- Encoding Ignorance: Not handling UTF-8/encoding issues in extracted text
- Memory Waste: Loading large PDFs entirely into memory
- Format Assumptions: Assuming all PDFs have the same structure
1---2name: pdf3description: Process PDF files for extraction, creation, modification, and manipulation.4---56# PDF Processing78## Overview9PDF processing involves extracting content from existing PDFs, creating new PDF documents, and modifying or transforming PDF files. This skill should be invoked when working with PDF files for operations like text extraction, table data extraction, document generation, merging, splitting, or watermarking.1011## Core Principles12- **Library Selection**: Choose the right library for your specific task (pypdf, pdfplumber, reportlab)13- **Text Extraction**: Use pdfplumber for text with layout preservation, pypdf for simple extraction14- **Creation**: Use reportlab for programmatic PDF generation with precise control15- **Transformation**: Understand that PDFs are final-form documents - editing is limited1617## Preparation Checklist18- [ ] Identify the operation type: extraction, creation, or transformation19- [ ] For extraction: Determine if you need text, tables, or metadata20- [ ] For creation: Define the document structure and content21- [ ] Install required libraries: pypdf, pdfplumber, reportlab2223## Step-by-Step Process241. **Read/Load**: Open the PDF file using appropriate library252. **Extract/Process**: Get text, tables, or metadata as needed263. **Transform**: Apply any modifications (merge, split, rotate, watermark)274. **Create**: If generating new PDF, build content programmatically285. **Save**: Write output to new file or return processed content2930## Do's and Don'ts31- ✅ **Do** use pdfplumber for tables - it excels at table extraction32- ✅ **Do** handle encoding issues when extracting international text33- ✅ **Do** close files properly to release resources34- ❌ **Don't** expect perfect formatting preservation when extracting text35- ❌ **Don't** attempt complex layout editing - PDFs aren't designed for that36- ❌ **Don't** skip error handling for corrupted or password-protected PDFs3738## Anti-Patterns39- **OCR Blindness**: Not using OCR when dealing with scanned documents40- **Encoding Ignorance**: Not handling UTF-8/encoding issues in extracted text41- **Memory Waste**: Loading large PDFs entirely into memory42- **Format Assumptions**: Assuming all PDFs have the same structure