Last updated: February 4, 2025

<span aria-hidden="true" id="automate-pdf-watermarking-with-ghostscript"></span>

# Automate PDF watermarking with Ghostscript

![Tim Koschützki](/assets/images/teammates/avatar-tim-kos-1.jpg?dpl=dpl_B2d4XACVUtA1h8m6kRxZhWsda77Q)

#### Tim Koschützki

Co-founder · Berlin, Germany · Show bio

[](https://x.com/tim%5Fkos)[](https://github.com/tim-kos)

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.

<span aria-hidden="true" id="install-and-configure-ghostscript"></span>

## Install and configure Ghostscript

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

```bash
sudo apt-get update
sudo apt-get install ghostscript

```

For macOS users with Homebrew:

```bash
brew install ghostscript

```

Verify the installation and version:

```bash
gs --version

```

<span aria-hidden="true" id="create-a-watermark-using-pdfmark"></span>

## 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:

```postscript
%!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

```

<span aria-hidden="true" id="apply-watermark-securely"></span>

## Apply watermark securely

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

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

```

<span aria-hidden="true" id="optimize-pdf-compression"></span>

## Optimize PDF compression

Ghostscript offers several compression presets optimized for different use cases:

```bash
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)

<span aria-hidden="true" id="enhanced-automation-script"></span>

## Enhanced automation script

This improved script includes error handling and security checks:

```bash
#!/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"

```

<span aria-hidden="true" id="security-considerations"></span>

## 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

<span aria-hidden="true" id="version-compatibility"></span>

## 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

<span aria-hidden="true" id="conclusion"></span>

## 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.

\#ghostscript#pdf#automation#document-processing-service#file-compressing-service

### 👩‍💻 Join 20k+ developers

Sign up for our [monthly newsletter](/newsletters.md) to receive direct links to 3 exclusive tech — and 2 product updates. No less, no more.

Your email:

Get access

## File uploading and encoding. Made simple.

Transloadit streamlines file handling for developers, trusted by brands like Coursera and The New York Times. We’re known for a reliable API, top-notch support, and a strong commitment to open source, with projects like [Uppy⁠](https://uppy.io) and [Tus⁠](https://tus.io) setting standards in file processing.

[Sign up](/c/)[Book a Demo](https://survey.typeform.com/to/kRg47Xi5)

No credit card needed · 5 GB included in the free plan

Cancel anytime
