How to Redact PDFs on Linux Command Line Securely
Complete guide to redacting PDFs using Linux CLI tools like qpdf, Ghostscript, and ImageMagick. Fully offline workflows that actually remove data.
Why command-line redaction matters
GUI tools often create the illusion of redaction—a black rectangle drawn over text that remains fully selectable underneath. Command-line workflows give you explicit control over what happens to your PDF at the byte level. You can verify each step, script repeatable processes, and avoid the privacy risks of uploading documents to web services.
This guide covers the complete pipeline: inspecting a PDF for sensitive content, performing actual redaction (not cosmetic overlays), stripping metadata, and verifying the result. Every tool runs locally on your Linux machine.
The fundamental problem with naive redaction
Before diving into commands, understand why most redaction attempts fail:
- Overlay redaction: Drawing a black box over text leaves the original text intact in the PDF stream. Anyone can copy-paste it or remove the overlay.
- Hidden layers: PDFs can contain multiple layers, including previous revisions with unredacted content.
- Metadata leakage: Author names, creation dates, software versions, and even GPS coordinates (in embedded images) persist unless explicitly removed.
- Embedded fonts and comments: Subset fonts can reveal character usage patterns; annotations may contain reviewer names.
The only reliable redaction method is destructive: convert the page to a flat image, then reassemble. This eliminates selectable text entirely. For a deeper explanation, see Why Black Highlighter Redaction Fails to Secure PDFs.
Tool inventory
Install these packages (Debian/Ubuntu names; adjust for your distro):
sudo apt install qpdf ghostscript imagemagick poppler-utils mat2 exiftool
| Tool | Purpose |
|---|---|
qpdf | Decrypt, linearize, inspect PDF structure |
ghostscript | Rasterize pages, re-distill PDFs |
imagemagick | Image manipulation, adding redaction boxes |
pdftoppm / pdftocairo | High-quality PDF-to-image conversion (poppler) |
mat2 | Metadata removal (supports PDFs and images) |
exiftool | Inspect and strip EXIF/XMP metadata |
Step 1: Inspect the PDF
Before redacting, understand what you're dealing with.
Check for encryption or restrictions:
qpdf --show-encryption input.pdf
If encrypted, decrypt first (you'll need the password):
qpdf --decrypt --password="secret" input.pdf decrypted.pdf
List metadata:
exiftool input.pdf
Look for Author, Creator, Producer, Title, Subject, Keywords, and any XMP fields. For a thorough inspection process, the guide How to Check a PDF for Hidden Metadata covers additional techniques.
Extract all text (to locate sensitive content):
pdftotext input.pdf - | grep -in "social security\|ssn\|account"
Check for embedded files or JavaScript:
qpdf --list-attachments input.pdf
qpdf --json input.pdf | grep -i "/JS\|/JavaScript"
Step 2: The rasterize-and-rebuild method
This is the only method that guarantees text removal. You convert each page to a bitmap image, apply visual redactions, then reassemble into a PDF.
Convert PDF to images
Using pdftoppm (from poppler-utils) for high-quality output:
mkdir pages
pdftoppm -png -r 300 input.pdf pages/page
This creates pages/page-1.png, pages/page-2.png, etc., at 300 DPI—sufficient for most documents. Increase to 400 or 600 DPI for fine print or legal documents.
Apply redaction boxes
Use ImageMagick to draw black rectangles over sensitive regions. You'll need to identify coordinates manually (open an image in an image viewer and note pixel positions).
# Redact a region from (100,200) to (400,250) on page 1
convert pages/page-1.png -fill black -draw "rectangle 100,200 400,250" pages/page-1.png
For multiple regions:
convert pages/page-1.png \
-fill black \
-draw "rectangle 100,200 400,250" \
-draw "rectangle 50,800 300,830" \
pages/page-1.png
To redact the same region across all pages (e.g., a header with account numbers):
for img in pages/page-*.png; do
convert "$img" -fill black -draw "rectangle 100,50 500,100" "$img"
done
Reassemble into PDF
convert pages/page-*.png output-images.pdf
Or use img2pdf for better quality preservation (no recompression):
pip install img2pdf
img2pdf pages/page-*.png -o output-images.pdf
Step 3: Strip metadata
The reassembled PDF still contains metadata from the conversion process. Remove it:
Using mat2:
mat2 --inplace output-images.pdf
Using qpdf:
qpdf --linearize --replace-input output-images.pdf
Using exiftool (aggressive):
exiftool -all= -overwrite_original output-images.pdf
Using Ghostscript (nuclear option—also re-distills the PDF):
gs -dNOPAUSE -dBATCH -sDEVICE=pdfwrite \
-dCompatibilityLevel=1.4 \
-dPDFSETTINGS=/prepress \
-sOutputFile=final.pdf output-images.pdf
For comprehensive metadata removal, see Free Local PDF Metadata Scrubber: A Complete Guide.
Step 4: Flatten to remove hidden layers
Even after rasterization, some PDF structures can persist if you used certain conversion paths. Flatten explicitly:
qpdf --flatten-annotations=all --object-streams=disable \
output-images.pdf flattened.pdf
Or use Ghostscript to re-render:
gs -dNOPAUSE -dBATCH -sDEVICE=pdfwrite \
-dCompatibilityLevel=1.4 \
-sOutputFile=flattened.pdf output-images.pdf
The guide How to Flatten a PDF to Remove Hidden Layers Securely explains why this step matters.
Verify your work
Never trust the process—verify the output.
Attempt text extraction:
pdftotext flattened.pdf -
If redaction worked, this should return empty or only non-sensitive text. Any sensitive strings appearing here mean redaction failed.
Check metadata again:
exiftool flattened.pdf
Look for any remaining author names, software identifiers, or timestamps you didn't expect.
Visual inspection:
Open the PDF and try to select text in redacted areas. If you can highlight anything, the redaction is cosmetic only.
Search the raw PDF stream:
strings flattened.pdf | grep -i "confidential\|ssn\|password"
This catches text that might be hidden in object streams.
Scripting a complete pipeline
Here's a reusable script for batch redaction:
#!/bin/bash
set -e
INPUT="$1"
OUTPUT="$2"
TMPDIR=$(mktemp -d)
# Convert to images
pdftoppm -png -r 300 "$INPUT" "$TMPDIR/page"
# Apply redactions (customize coordinates)
for img in "$TMPDIR"/page-*.png; do
convert "$img" -fill black -draw "rectangle 400,30 600,60" "$img"
done
# Reassemble
img2pdf "$TMPDIR"/page-*.png -o "$TMPDIR/assembled.pdf"
# Strip metadata and flatten
gs -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 \
-sOutputFile="$OUTPUT" "$TMPDIR/assembled.pdf"
exiftool -all= -overwrite_original "$OUTPUT"
# Cleanup
rm -rf "$TMPDIR"
echo "Redacted PDF: $OUTPUT"
Key takeaways
- Overlay redaction is not redaction. Text under black boxes remains extractable.
- Rasterization is the only reliable method. Convert pages to images, redact visually, reassemble.
- Metadata survives conversion. Always strip it explicitly with mat2, exiftool, or Ghostscript.
- Verify every output. Use
pdftotext,strings, and visual inspection before sharing. - Script your workflow. Repeatable processes reduce human error.
For related workflows, see How to Redact Social Security Numbers From PDFs Securely and Remove Hidden Data From a PDF Before Sending It.