Skip to content

Instantly share code, notes, and snippets.

@dannguyen
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dannguyen/c4752745d184d6526e64 to your computer and use it in GitHub Desktop.
Save dannguyen/c4752745d184d6526e64 to your computer and use it in GitHub Desktop.
This is just a quickie write-up of how to de-secure PDF files using ghostscript from the command line. There's some setup code to simulate a bunch of locked PDFs.
# First
# http://unix.stackexchange.com/questions/46948/how-to-make-duplicates-with-different-names-from-a-single-file
mkdir copies
newnames=("a" "b" "c" "d" "e")
orgfile=my_original_doc.pdf
for x in "${newnames[@]}"; do
cp $orgfile "copies/$x.pdf"
done
# password protect pdfs
# http://www.silicongadget.com/guides/security-guides/password-lock-pdf-files-with-pdftk/2059/
mkdir locked_files
for filename in copies/*.pdf; do
pdftk $filename output "locked_files/${filename}" owner_pw mypasswordz allow ScreenReaders
done
# allow ScreenReaders lets you view the file but not print/copy-paste it. You must specify a value for `allow`
# or else pdftk won't place any restrictions on the PDFs
# remove passwords
# http://www.commandlinefu.com/commands/view/4345/remove-security-limitations-from-pdf-documents-using-ghostscript
# this is better:
# http://www.localizingjapan.com/blog/2013/02/23/unlocking-secured-password-protected-pdf-files/
mkdir freed
for filename in locked_files/*.pdf; do
# gs -q -dNOPAUSE -dBATCH -sPDFPassword= -sDEVICE=pdfwrite -sOutputFile="freed/${filename}" -c .setpdfwrite -f $filename
gs -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile="freed/${filename}" -c .setpdfwrite -f $filename
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment