pdfcrop
A Rust library and CLI tool for cropping PDF files with rendering-based automatic bounding box detection.
Inspired by the original pdfcrop from TeX Live.
Web app using WASM available at pdfcrop.github.io.
Features
- Accurate bbox detection - Renders PDF pages to detect visible content boundaries
- Flexible margins - Uniform or per-side margins
- Manual override - Specify exact crop regions
- Library + CLI - Use as a Rust library or standalone command-line tool
- Pure Rust - WASM-compatible, no external dependencies
Quick Start
CLI Usage
# Install
# Basic crop (auto-detect bbox)
# With margins
# Verbose mode to see detection details
# Custom bbox
# Add content clipping (adds clipping path to stream - increases file size)
# Auto-shrink manual bbox to actual content (removes remaining margins)
Library Usage
use ;
use fs;
Examples
# Generate test PDFs with shapes and crop them
# Download a real PDF and crop it
Development
# Build
# Run tests
# Format and lint
How It Works
- Renders PDF pages to bitmaps using hayro
- Scans pixels to find bounding box of visible content
- Applies margins and sets the PDF CropBox
- Multi-page PDFs processed in parallel with rayon
License
MIT OR Apache-2.0