Ghostscript is a powerful, open-source tool for PDF manipulation, offering robust capabilities for watermarking, compression, and document processing. This guide explores how to leverage Ghostscript 10.04.0 to enhance your PDF workflow with automated watermarking and optimization techniques.

Install and configure Ghostscript

Install the latest version of Ghostscript (10.04.0) on your system. For Ubuntu and Debian-based systems:

sudo apt-get update
sudo apt-get install ghostscript

For macOS users with Homebrew:

brew install ghostscript

Verify the installation and version:

gs --version

Create a watermark using pdfmark

Instead of using basic PostScript, create a more reliable watermark using pdfmark operators. Create a file named watermark.ps with the following content:

%!PS-Adobe-3.0
/pdfmark where {pop} {userdict /pdfmark /cleartomark load put} ifelse
[ /Rect [ 50 300 550 400 ]
  /ShadingType 3
  /ColorSpace /DeviceGray
  /Coords [300 350 0 300 350 100]
  /Function
  << /FunctionType 2
     /Domain [0 1]
     /C0 [0.8]
     /C1 [0.3]
     /N 1
  >>
  /Extend [true true]
  /Shading pdfmark
[ /PageMode /UseNone
  /Page 1
  /View [/XYZ null null null]
  /Title (Watermarked Document)
  /Author (Your Company)
  /DOCINFO pdfmark
[ /Rect [ 50 300 550 400 ]
  /Font /Helvetica-Bold
  /FontSize 48
  /TextColor [0.5 0.5 0.5]
  /TextAlign /Center
  /Contents (CONFIDENTIAL)
  /Watermark pdfmark

Apply watermark securely

Use this enhanced command to apply the watermark while maintaining PDF security:

gs -dSAFER \
   -dBATCH \
   -dNOPAUSE \
   -sDEVICE=pdfwrite \
   -dCompatibilityLevel=1.7 \
   -dPDFSETTINGS=/prepress \
   -sOutputFile=watermarked.pdf \
   -dDEVICEWIDTHPOINTS=612 \
   -dDEVICEHEIGHTPOINTS=792 \
   input.pdf watermark.ps

Optimize PDF compression

Ghostscript offers several compression presets optimized for different use cases:

gs -dSAFER \
   -dBATCH \
   -dNOPAUSE \
   -sDEVICE=pdfwrite \
   -dCompatibilityLevel=1.7 \
   -dPDFSETTINGS=/ebook \
   -sOutputFile=compressed.pdf \
   input.pdf

Available -dPDFSETTINGS options:

  • /screen (72 dpi, lowest quality, smallest size)
  • /ebook (150 dpi, medium quality)
  • /printer (300 dpi, high quality)
  • /prepress (300 dpi, color preserving)
  • /default (unchanged settings)

Enhanced automation script

This improved script includes error handling and security checks:

#!/bin/bash

set -euo pipefail

INPUT_FILE="$1"
WATERMARK_FILE="watermark.ps"
STAMPED_FILE="stamped.pdf"
COMPRESSED_FILE="compressed.pdf"

if [ -z "$INPUT_FILE" ]; then
  echo "Error: Input file required"
  echo "Usage: $0 <input-pdf>"
  exit 1
fi

if [ ! -f "$INPUT_FILE" ]; then
  echo "Error: Input file '$INPUT_FILE' not found"
  exit 1
fi

if [ ! -f "$WATERMARK_FILE" ]; then
  echo "Error: Watermark file '$WATERMARK_FILE' not found"
  exit 1
fi

process_pdf() {
  local input=$1
  local output=$2
  local settings=${3:-/prepress}

  if ! gs -dSAFER \
        -dBATCH \
        -dNOPAUSE \
        -sDEVICE=pdfwrite \
        -dCompatibilityLevel=1.7 \
        -dPDFSETTINGS="$settings" \
        -sOutputFile="$output" \
        "$input"; then
    echo "Error: PDF processing failed"
    exit 1
  fi
}

echo "Adding watermark..."
process_pdf "$INPUT_FILE" "$STAMPED_FILE"

echo "Optimizing file size..."
process_pdf "$STAMPED_FILE" "$COMPRESSED_FILE" /ebook

echo "Processing complete: $COMPRESSED_FILE generated"

Security considerations

When processing PDFs with Ghostscript:

  1. Always use the -dSAFER flag to prevent unauthorized file access
  2. Verify input files before processing
  3. Use appropriate file permissions for output files
  4. Keep Ghostscript updated to the latest version for security patches
  5. Validate PDF output to ensure integrity

Version compatibility

Ghostscript 10.04.0 introduces improvements in PDF handling and security. Key compatibility notes:

  • PDF compatibility level 1.7 is recommended for modern workflows
  • The new PDF interpreter improves processing speed and reliability
  • Legacy PostScript workflows remain supported but may require updates

Conclusion

Ghostscript provides powerful tools for PDF manipulation, enabling secure and efficient document processing workflows. By implementing these techniques with proper error handling and security measures, you can create robust automation solutions for your document processing needs.

For automated document processing at scale, consider exploring Transloadit's document processing services, which handle PDF operations securely and efficiently in the cloud.