pdfcrop

Crates.iopdfcrop
lib.rspdfcrop
version0.1.0
created_at2025-11-23 19:48:07.070224+00
updated_at2025-11-23 19:48:07.070224+00
descriptionPDF cropping library and command-line tool with rendering-based bbox detection
homepage
repositoryhttps://github.com/pdfcrop/pdfcrop
max_upload_size
id1946924
size266,456
Teddy van Jerry (Teddy-van-Jerry)

documentation

README

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
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

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

# 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

# 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
  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

License

MIT OR Apache-2.0

Commit count: 0

cargo fmt