Stop Memorizing 140-Character Ghostscript Flags: Offline PDF Compression on Ubuntu with Rust (PDFGraph)

2026-12-03来源 dev.toEnglish

Originally published on CatalystCode — includes an interactive Ubuntu CLI command builder where you pick your document type and copy the exact command + verification checks.

If you have ever tried to shrink a sensitive PDF (tax returns, contracts, medical scans, or research papers) on Ubuntu without uploading it to a third-party website, you have probably copy-pasted this Ghostscript incantation from AskUbuntu:

gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/ebook \ -dNOPAUSE -dQUIET -dBATCH -sOutputFile=output.pdf input.pdf 

It works great for 300–600 DPI scans—until you run it on a text/vector report and discover two annoying problems:

  1. Ghostscript can actually make text-heavy PDFs larger or degrade sharp vector diagrams.
  2. If you run qpdf instead, it cleans up object streams losslessly, but won't shrink a 25 MB scanned receipt because it doesn't downsample embedded images.

A Cleaner Offline Workflow: QPDF + Ghostscript + Validation

That friction is why I built PDFGraph, an open-source MIT-licensed Rust CLI wrapper for Ubuntu/Linux that coordinates QPDF and Ghostscript locally—with zero uploads, no accounts, and no telemetry.

Instead of guessing which engine will work best, PDFGraph:

  • Runs a lossless QPDF pass by default (preserving 100% of image resolution and vector sharpness).
  • Optionally runs a Ghostscript image-downsampling candidate (--mode balanced at ~150 DPI, or --mode small at ~100–110 DPI).
  • Validates candidate outputs and protects your existing files from accidental overwrite unless --force is passed.

Quick Start on Ubuntu

# 1. Text reports & diagrams (100% lossless image resolution) pdfgraph report.pdf -o report-lossless.pdf # 2. Scanned papers & photo-heavy decks (~150 DPI balanced) pdfgraph scan.pdf --mode balanced -o scan-balanced.pdf # 3. Strict email / upload portal limits (inspect small footnotes after!) pdfgraph scan.pdf --mode small -o scan-small.pdf 

Always Verify Before Sharing

Never trust a compression pass blindly—check file size, page count, and selectable text locally:

ls -lh scan.pdf scan-balanced.pdf pdfinfo scan-balanced.pdf | grep -E "Pages|File size" 

Full Setup Guide + Interactive Ubuntu Command Builder

The full installation steps (including qpdf, ghostscript, and Rust/Cargo setup), troubleshooting checklist, and an interactive command builder (pick your PDF type → get the exact pdfgraph + pdfinfo verification command) are live in the original post:

👉 Read the full guide & try the interactive command builder on CatalystCode

Source code: github.com/catalystcode-dev/PDFGraph

原文:https://www.a8q9.cn/en/article/stop-memorizing-140-character-ghostscript-flags-offline-pdf-compression-on-ubunt-b9c6b9.html

相关文章