# pdfcrop
A Rust library and CLI tool for cropping PDF files with **rendering-based automatic bounding box detection**.
Inspired by the original [`pdfcrop`](https://ctan.org/pkg/pdfcrop) from TeX Live.
Web app using WASM available at [pdfcrop.github.io](https://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
```bash
# Install
cargo install --path .
# Basic crop (auto-detect bbox)
pdfcrop input.pdf output.pdf
# With margins
pdfcrop --margins "10" input.pdf output.pdf
# Verbose mode to see detection details
pdfcrop --verbose input.pdf output.pdf
# Custom bbox
pdfcrop --bbox "50 50 500 700" input.pdf output.pdf
# Add content clipping (adds clipping path to stream - increases file size)
pdfcrop --clip input.pdf output.pdf
# Auto-shrink manual bbox to actual content (removes remaining margins)
pdfcrop --bbox "0 0 612 792" --shrink-to-content input.pdf output.pdf
```
### Library Usage
```rust
use pdfcrop::{crop_pdf, CropOptions, Margins};
use std::fs;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let pdf_data = fs::read("input.pdf")?;
let options = CropOptions {
margins: Margins::uniform(10.0),
verbose: true,
..Default::default()
};
let cropped = crop_pdf(&pdf_data, options)?;
fs::write("output.pdf", cropped)?;
Ok(())
}
```
## Examples
```bash
# Generate test PDFs with shapes and crop them
cargo run --example crop_shapes_pdf
# Download a real PDF and crop it
cargo run --example crop_online_pdf
```
## Development
```bash
# Build
cargo build --release
# Run tests
cargo test
# Format and lint
cargo fmt
cargo clippy
```
## How It Works
1. Renders PDF pages to bitmaps using [hayro](https://github.com/LaurenzV/hayro)
2. Scans pixels to find bounding box of visible content
3. Applies margins and sets the PDF CropBox
4. Multi-page PDFs processed in parallel with [rayon](https://github.com/rayon-rs/rayon)
## License
MIT OR Apache-2.0