Skip to main content

pdf2llm - PDF to LLM Context Extractor

Extract content from PDFs in a format optimized for Large Language Model (LLM) consumption.

Features

  • Multiple formats: Extract as Markdown (preserves structure) or plain text
  • Image extraction: Automatically extracts and saves images with configurable DPI
  • Table preservation: Maintains table structure in Markdown format
  • Page boundaries: Optional page markers for maintaining document structure
  • Batch processing: Process multiple PDFs at once
  • Organized output: Clean directory structure for each PDF
  • Structure analysis: Analyze PDFs before extraction
  • Token estimation: Get token counts for LLM context planning

Installation

# Install from PyPI
pip install pdf2llm
# or
uv pip install pdf2llm

# Or install from source
git clone https://github.com/yourusername/pdf2llm.git
cd pdf2llm
uv sync

Usage

Command Line Interface

# Basic extraction
uv run ./pdf2llm document.pdf

# Extract to specific directory
uv run ./pdf2llm document.pdf -o extracted_docs/

# Batch process multiple PDFs
uv run ./pdf2llm *.pdf -o zoning_docs/

# Extract as plain text without images
uv run ./pdf2llm document.pdf --format text --no-images

# Analyze PDF structure only
uv run ./pdf2llm document.pdf --analyze-only

# High quality image extraction
uv run ./pdf2llm document.pdf --dpi 300

# Get JSON output for integration
uv run ./pdf2llm document.pdf --json

# Set token limit warning
uv run ./pdf2llm document.pdf --token-limit 4000

Python API

from pdf_utils import PDFExtractor

# Create extractor
extractor = PDFExtractor(
    output_dir=Path("extracted"),
    image_format="png",
    dpi=150
)

# Extract single PDF
result = extractor.extract(
    Path("document.pdf"),
    output_format="markdown"
)

print(f"Tokens: {result.token_estimate}")
print(f"Pages: {result.page_count}")
print(f"Has images: {result.has_images}")
print(f"Has tables: {result.has_tables}")

# Save to file
output_path = extractor.save_extraction(result, Path("document.pdf"))

# Batch extraction
pdf_files = list(Path("pdfs/").glob("*.pdf"))
results = extractor.batch_extract(pdf_files)

Output Structure

extracted/
├── document_name/
│   ├── content.md         # Extracted content
│   └── images/           # Extracted images (if any)
│       ├── page-1-0.png
│       └── page-2-0.png
└── another_document/
    ├── content.md
    └── images/

Use Cases

Zoning Documents Analysis

# Extract all zoning PDFs with high-quality images
uv run ./pdf2llm zoning_*.pdf -o zoning_analysis/ --dpi 300

# Then in your Python code:
with open("zoning_analysis/zoning_code_2024/content.md", "r") as f:
    content = f.read()

# Use with your LLM
response = llm.chat(
    messages=[{
        "role": "system",
        "content": f"You are analyzing zoning documents. Document: {content}"
    }, {
        "role": "user", 
        "content": "What are the setback requirements for R-1 zones?"
    }]
)

Document Q&A System

# Process all documents
uv run ./pdf2llm documents/*.pdf -o knowledge_base/

# Check token counts
uv run ./pdf2llm documents/*.pdf --json | jq '.token_estimate'

Research Paper Analysis

# Extract with tables and figures
uv run ./pdf2llm research_paper.pdf --dpi 200

# Extract text only for quick analysis
uv run ./pdf2llm research_paper.pdf --format text --no-images

CLI Options

Option Description Default
-o, --output-dir Output directory extracted/
--format Output format (markdown, text, both) markdown
--no-images Skip image extraction False
--image-format Image format (png, jpg, jpeg) png
--dpi DPI for image extraction 150
--no-page-chunks Disable page boundary markers False
--analyze-only Only analyze structure False
--quiet Minimal output False
--json JSON output False
--token-limit Warn if exceeds limit None

Package Structure

pdf_utils/
├── core/
│   └── extractor.py      # Core extraction logic
├── cli/
│   └── main.py          # CLI interface
└── __init__.py         # Package exports

Requirements

  • Python 3.12+
  • uv (for dependency management)
  • Dependencies managed in pyproject.toml

License

MIT License

Metadata

Release files for pdf2llm 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pdf2llm 0.1.1
File Size Uploaded Provenance Actions
pdf2llm-0.1.1.tar.gz 7.5 kB Without provenance Details for pdf2llm-0.1.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pdf2llm 0.1.1
File Interpreter ABI Platform Actions
pdf2llm-0.1.1-py3-none-any.whl Python 3 none any Details for pdf2llm-0.1.1-py3-none-any.whl

Total release size: 19.3 kB

Release files / pdf2llm-0.1.1.tar.gz

Download URL pdf2llm-0.1.1.tar.gz
Size 7.5 kB
Tags Source
SHA-256 checksum
How to use checksums
f963cfd0433de670ecbbaa2ff57db85da7fdc8908e928ab6914d22214b6e97ae
BLAKE2b-256 checksum
How to use checksums
724d2ba84e7a832a4c445819421bc6881e94138d0385a540d5718c4ed8b835db
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.6.6

Release files / pdf2llm-0.1.1-py3-none-any.whl

Download URL pdf2llm-0.1.1-py3-none-any.whl
Size 11.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6a0bc2d76b43779da0474c662fa5fa61345a4210545d508bc3b1d34ee12bbeeb
BLAKE2b-256 checksum
How to use checksums
f76b022097fab890a88612b7073d797fc03f52e2a84c20ece7df78016d08cfb6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.6.6

Security for pdf2llm 0.1.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page