---
name: anthropic-pdf
description: PDF manipulation — reading, merging, splitting, extracting text, and creating basic PDFs. Use when users need to work with PDF files programmatically, extract content, modify documents, or generate PDF output.
license: Complete terms in LICENSE.txt
---

# PDF Skill

A skill for working with PDF files — reading, extracting content, merging, splitting, and creating PDFs.

## Core Libraries

- **PyMuPDF (fitz)** — Best for reading, extracting text/images, and basic manipulation
- **pypdf** — Good for merging, splitting, and metadata operations
- **reportlab** — For creating PDFs from scratch
- **pdf2image** — Converting PDF pages to images

## Installation

```bash
pip install pymupdf pypdf reportlab pdf2image
```

## Reading and Extracting

```python
import fitz  # PyMuPDF

doc = fitz.open("document.pdf")
for page_num in range(len(doc)):
    page = doc[page_num]
    text = page.get_text()
    print(f"--- Page {page_num + 1} ---")
    print(text)

doc.close()
```

## Merging PDFs

```python
from pypdf import PdfWriter

writer = PdfWriter()
for pdf_file in ["file1.pdf", "file2.pdf"]:
    writer.append(pdf_file)

writer.write("merged.pdf")
writer.close()
```

## Creating PDFs from Scratch

```python
from reportlab.lib.pagesizes import A4
from reportlab.pdfgen import canvas

c = canvas.Canvas("output.pdf", pagesize=A4)
c.drawString(100, 750, "Hello, World!")
c.save()
```

## Best Practices

- Use PyMuPDF for text extraction (it's the most accurate)
- Use pypdf for structural operations (merge, split, rotate)
- Use reportlab for creating new PDFs
- Always close PDF files after use
- Handle encrypted PDFs by providing a password when opening
