thiagowfx's avatar

Β¬ just serendipity πŸ€ (not just serendipity)

β˜… new script: PDF remove password

β€’ 384 words β€’ 2 min β€’ updated

Previously.

Problem statement: Given a password-protected .pdf file, remove its password. It’s indifferent whether the file gets overwritten or whether a new one is created.

ghostscript can do it:

shell
gs -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=output.pdf -c .setpdfwrite -f input.pdf

qpdf can do it:

shell
qpdf --decrypt --password="{my pass}" input.pdf output.pdf

I am not expected to memorize these CLI flags.

I do not want to look them up every time either.

There are various approaches to tackle this:

  • a shell alias (alias foo=)
  • a shell function foo() {}
  • a shell script (*.sh)
  • a comma script
  • a copyable snippet (e.g. stored in Obsidian / LogSeq or some other digital garden / personal wiki / second brain app)
  • a snippet (e.g. in RayCast or Espanso)
  • ask the AI each time
  • google search for it each time

Lately I’ve been favouring the shell script approach, so here we go with pdf_password_remove.sh:

Usage #

Remove password protection from PDF files.

bash
# Interactive password prompt (most secure)
pdf_password_remove secret.pdf

# Process multiple files
pdf_password_remove report.pdf invoice.pdf contract.pdf

# Provide password via command line
pdf_password_remove --password=BANANA42SPLIT secret.pdf

# Custom output filename (single file only)
pdf_password_remove -o unlocked.pdf secret.pdf

Example Output #

% pdf_password_remove --password=PIZZA69SLICE financial-report.pdf tax-form.pdf
βœ“ Successfully processed: financial-report.pdf β†’ financial-report-unlocked.pdf
βœ“ Successfully processed: tax-form.pdf β†’ tax-form-unlocked.pdf

Summary: Successfully processed 2/2 file(s)

Features #

  • Process single or multiple PDF files in one command
  • Secure interactive password prompt (hidden input)
  • Command-line password option for automation/scripts
  • Default output naming: adds -unlocked suffix
  • Custom output filename with -o flag (single file only)
  • Clear success/failure indicators for each file
  • Validates PDF files before processing

Prerequisites #

Requires either ghostscript (preferred) or qpdf to be installed:

bash
# macOS
brew install ghostscript       # Preferred
brew install qpdf              # Alternative

# Linux
sudo apt install ghostscript   # Preferred - Debian/Ubuntu
sudo apt install qpdf          # Alternative

sudo dnf install ghostscript   # Preferred - Fedora
sudo dnf install qpdf          # Alternative

sudo pacman -S ghostscript     # Preferred - Arch
sudo pacman -S qpdf            # Alternative

The script automatically detects and uses whichever tool is available, preferring ghostscript if both are installed.

Security Note #

Using --password on the command line will expose the password in shell history and process lists. For sensitive documents, use the interactive prompt instead (default behavior when password is not provided).