PDF Compress Tool
Performs actual PDF compression, supporting both target size and percentage modes. Unlike existing solutions that only provide guidance, this tool compresses files directly.
Features
- - Two compression modes: target size (
--target-size 2MB) / percentage (--reduce 30) - Three-stage compression pipeline: pikepdf structural optimization → Ghostscript progressive compression → QPDF secondary optimization
- Quality floor protection: three levels (high / medium / low) to prevent excessive compression from damaging image quality
- Batch processing: compress all PDFs in a directory at once
- Auto dependency detection: detects available tools at runtime and adapts compression strategy accordingly
Usage
Compress to target size
CODEBLOCK0
CODEBLOCK1
Compress by percentage
CODEBLOCK2
CODEBLOCK3
Specify output path and quality
CODEBLOCK4
Batch processing
CODEBLOCK5
CODEBLOCK6
Compression Strategy
Stage 1: pikepdf structural optimization
Removes redundant objects, compresses streams, and packs object streams. Lossless operation that preserves maximum image quality.
Stage 2: Ghostscript progressive compression
Tries prepress → printer → ebook → screen progressively, checking at each level and stopping as soon as the target is met.
Also supports custom DPI downsampling (100 → 72 → 50 → 36 dpi), subject to quality floor protection.
Stage 3: QPDF secondary optimization
Object stream generation and stream recompression, saving an additional 5-15%. Requires qpdf installation (optional).
Quality floor protection
| Level | Meaning | Minimum compression level | Use case |
|---|
| INLINECODE2 | Quality priority | printer (300dpi) | Documents intended for printing |
| INLINECODE3 |
Balanced (default) | ebook (150dpi) | Everyday use |
|
low | Size priority | screen (72dpi) and below | Screen viewing only |
Dependencies
- - python3 (required)
- ghostscript (recommended): core compression engine
- qpdf (optional): secondary structural optimization, additional 5-15% compression
- pikepdf (auto-installed via pip): Python PDF library, fallback when Ghostscript is unavailable
Install by platform
| Dependency | macOS | Linux (Debian/Ubuntu) | Linux (RHEL/Fedora) | Windows |
|---|
| ghostscript | INLINECODE5 | INLINECODE6 | INLINECODE7 | INLINECODE8 or download |
| qpdf |
brew install qpdf |
sudo apt-get install qpdf |
sudo dnf install qpdf |
choco install qpdf or
download |
| pikepdf |
pip install pikepdf (auto) |
pip install pikepdf (auto) |
pip install pikepdf (auto) |
pip install pikepdf (auto) |
The script auto-detects your platform and shows the correct install command when a dependency is missing.
PDF 压缩工具
执行实际的PDF压缩,支持目标大小和百分比两种模式。与仅提供指导的现有解决方案不同,此工具可直接压缩文件。
功能特性
- - 两种压缩模式:目标大小(--target-size 2MB)/ 百分比(--reduce 30)
- 三级压缩流水线:pikepdf 结构优化 → Ghostscript 渐进式压缩 → QPDF 二次优化
- 质量底线保护:三级保护(高/中/低),防止过度压缩损害图像质量
- 批量处理:一次性压缩目录中的所有PDF文件
- 自动依赖检测:运行时自动检测可用工具并相应调整压缩策略
使用方法
压缩至目标大小
将此PDF压缩至2MB以下
将此PDF压缩至5MB以下
bash
python compress_pdf.py report.pdf --target-size 2MB
按百分比压缩
将文件大小减少30%
将文件大小减少50%
bash
python compress_pdf.py report.pdf --reduce 30
指定输出路径和质量
bash
python compress_pdf.py report.pdf --reduce 50 --quality low -o small.pdf
批量处理
将papers文件夹中的所有PDF压缩至2MB以下
bash
python compress_pdf.py --batch ./papers --target-size 2MB
python compress_pdf.py --batch ./papers --reduce 40 --quality high
压缩策略
第一阶段:pikepdf 结构优化
移除冗余对象,压缩数据流,打包对象流。无损操作,最大限度保留图像质量。
第二阶段:Ghostscript 渐进式压缩
依次尝试 prepress → printer → ebook → screen 级别,在每个级别检查压缩效果,一旦达到目标即停止。
同时支持自定义DPI降采样(100 → 72 → 50 → 36 dpi),受质量底线保护限制。
第三阶段:QPDF 二次优化
生成对象流和流重压缩,额外节省5-15%空间。需要安装qpdf(可选)。
质量底线保护
| 级别 | 含义 | 最低压缩级别 | 使用场景 |
|---|
| high | 质量优先 | printer(300dpi) | 需要打印的文档 |
| medium |
平衡(默认) | ebook(150dpi) | 日常使用 |
| low | 大小优先 | screen(72dpi)及以下 | 仅屏幕查看 |
依赖项
- - python3(必需)
- ghostscript(推荐):核心压缩引擎
- qpdf(可选):二次结构优化,额外5-15%压缩
- pikepdf(通过pip自动安装):Python PDF库,当Ghostscript不可用时作为后备方案
按平台安装
| 依赖项 | macOS | Linux(Debian/Ubuntu) | Linux(RHEL/Fedora) | Windows |
|---|
| ghostscript | brew install ghostscript | sudo apt-get install ghostscript | sudo dnf install ghostscript | choco install ghostscript 或 下载 |
| qpdf |
brew install qpdf | sudo apt-get install qpdf | sudo dnf install qpdf | choco install qpdf 或
下载 |
| pikepdf | pip install pikepdf(自动) | pip install pikepdf(自动) | pip install pikepdf(自动) | pip install pikepdf(自动) |
脚本会自动检测您的平台,并在缺少依赖项时显示正确的安装命令。