How is PDF compression being done?

main
59d816bfeat(pipeline): improve file processing with resource management and temp file handling (#5488)
This post might have stale content, as main is 1238 commits ahead.
Avatar of SyedMuzamilM
Syed Muzamil
posted

I want you to tell me about how are the doing the pdf compression

Avatar of anthropic/claude-haiku-4.5
Claude Haiku 4.5
commented

Perfect! Now I have a comprehensive understanding of the PDF compression implementation. Let me create a detailed explanation for you.

##PDF Compression in Stirling-PDF

Based on my review of the source code, here's how Stirling-PDF implements PDF compression:

###Multi-Strategy Compression Approach

The compression system uses a layered approach with multiple compression techniques applied sequentially:

####1. Image Compression (Primary Method)

The system performs aggressive image optimization:

  • Image Detection & Deduplication: Scans all pages to find images (both direct and nested within form XObjects)
  • Unique Image Identification: Uses sophisticated hashing (MD5 on pixel data + metadata like dimensions, color space, bits per component, masks) to identify duplicate images
  • Smart Replacement: When the same image appears multiple times, it's compressed once and all instances are replaced with the compressed version

Compression Process:

  • Resizing: Images are scaled based on optimization level (28%-98% of original size)
  • Grayscale Conversion: Optional conversion from color to grayscale
  • JPEG Quality Control: Quality is adjusted per optimization level (92% for level 1 down to 35% for level 9)
  • Optimization Parameters:
    • Huffman table optimization
    • Progressive JPEG encoding
    • Bicubic interpolation for high-quality resizing

Example from code:

// Scale factors by optimization level
case 1 -> 0.98;  // 98% of original
case 5 -> 0.68;  // 68% of original
case 9 -> 0.28;  // 28% of original (most aggressive)

// JPEG quality by level
case 1 -> 0.92f;  // 92% quality
case 9 -> 0.35f;  // 35% quality

####2. Ghostscript Compression (Level 6+)

For optimization level 6 and above:

  • Uses Ghostscript's pdfwrite device for deep PDF optimization
  • Applies different PDF settings based on level:
    • Levels 1-2: /prepress and /printer (highest quality)
    • Levels 3-5: /ebook and /screen (balanced)
    • Levels 6-9: /screen with aggressive image resolution downsampling (72-150 DPI)
  • Additional features:
    • Detects and removes duplicate images
    • Compresses fonts
    • Subsets fonts (removes unused glyphs)
    • CMYK to RGB conversion at level 7+

####3. QPDF Recompression

Always applied (when enabled):

  • Normalizes PDF structure and content streams
  • Recompresses existing compression streams
  • Generates object streams to group related objects
  • Optional linearization for web viewing
  • Advanced features at high levels:
    • JPEG quality optimization (78% → 34% quality)
    • Zopfli compression on Unix systems (better than standard zlib)
    • Decode/encode optimization

####4. Line Art Conversion

Optional conversion using ImageMagick:

  • Converts images to high-contrast line art (useful for scanned documents)
  • Configurable threshold (0-100) and edge detection strength (1-3 levels)
  • Typically reduces file size significantly for document scans

###Optimization Levels (1-9)

Level 1: 98% resizing, 92% JPEG quality   → Minimal compression, best quality
Level 5: 68% resizing, 72% JPEG quality   → Balanced
Level 9: 28% resizing, 35% JPEG quality   → Maximum compression

###Auto-Mode (Size-Based Optimization)

The system can automatically determine optimization level based on a target size:

Target size:    Optimization Level Determined:
> 90% of original  → Level 1
> 80% of original  → Level 2
> 60% of original  → Level 4
> 30% of original  → Level 5
> 15% of original  → Level 7
< 10% of original  → Level 9

If target size isn't met, the system iteratively increases the optimization level and retries.

###Key Features

  1. Duplicate Elimination: Same image compressed once, reused everywhere

  2. Nested Image Support: Handles images inside form XObjects (complex PDFs)

  3. Smart Minimum Thresholds:

    • Won't resize images already < 400x400 pixels (unless grayscale conversion)
    • Won't compress if reduction is < 5%
  4. Fallback Logic: If optimized file is larger than original, keeps the original

  5. Comprehensive Statistics: Logs compression ratios, bytes saved, number of images processed

###Processing Flow

1. Line Art Conversion (if requested)
   ↓
2. Ghostscript Compression (if level ≥ 6 and enabled)
   ↓
3. QPDF Recompression (if enabled)
   ↓
4. Image Compression (if level ≥ 4 or grayscale enabled)
   ↓
5. Check if target size met (auto-mode)
   → If not: increment level and retry

The system is quite sophisticated—it uses multiple compression techniques in layers, intelligently deduplicates images, and can automatically adjust compression intensity to hit a target file size!


END OF POST

How is PDF compression being done? — Stirling-Tools/Stirling-PDF