#pdf-file #pdf #filesize #trim #crop #margin #input-file

app pdftrim

Removes white margins from PDF files without inflating file sizes

4 releases (2 stable)

new 1.0.1 Feb 7, 2025
1.0.0-rc.5 Feb 5, 2025
0.1.0 Feb 5, 2025

#124 in Command line utilities

Download history 503/week @ 2025-02-04

503 downloads per month

MIT/Apache

53KB
244 lines

pdftrim

CI status crates.io Apache 2.0 or MIT Licenses

Removes white margins from PDF files without inflating file sizes.

pdftrim is a tool to trim white margins from PDF files. It is designed to be fast and efficient, producing minimal output file sizes.

This tool uses Ghostscript to calculate the bounding boxes for each page in the input PDF file. It then generates an output PDF file with updated CropBox values to remove white margins.

While similar to the pdfcrop script in the TeX Live distribution, this tool offers a key advantage: it maintains minimal output file sizes. The traditional pdfcrop processes PDFs by importing them into TeX documents and converting them back to PDF using pdfTeX, XeTeX, or LuaTeX, which often results in significantly larger files. In contrast, this tool directly modifies the PDF dimension data, avoiding file size inflation.

Installation

Prebuilt binaries are available for Linux, macOS, and Windows on the latest release page.

You need to have Ghostscript installed on your system.

brew install ghostscript      # macOS
sudo apt install ghostscript  # Ubuntu
sudo yum install ghostscript  # CentOS
sudo dnf install ghostscript  # Fedora

Alternative installation methods

Cargo

With Rust installed:

cargo install pdftrim

You need to have Ghostscript installed on your system.

Cargo Binstall

With Rust and cargo-binstall installed:

cargo binstall pdftrim

You need to have Ghostscript installed on your system.

Usage

Usage: pdftrim [OPTIONS] [input[.pdf]] [output file]

Arguments:
  [input[.pdf]]
          The input PDF file to crop.
          
          The `.pdf` extension is optional.

  [output file]
          The output PDF file to write.
          
          If not specified, the input file name is used with `-crop.pdf` appended. Existing files will be overwritten.

Options:
      --gscmd <command>
          The Ghostscript command to use.
          
          If not specified, it will search for Ghostscript in the system.

      --generate-completion <GENERATE_COMPLETION>
          If provided, outputs the completion file for given shell and exits
          
          [possible values: bash, elvish, fish, powershell, zsh]

  -h, --help
          Print help (see a summary with '-h')

  -V, --version
          Print version

Verbose output can be enabled by setting the `RUST_LOG` environment variable to `debug`.

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~17–25MB
~292K SLoC