How can I batch decrypt a series of PDF files?

Solution 1:

Nautilus script

Automating this task can be easily achieved through a script:

#!/bin/bash

# AUTHOR:       (c) Glutanimate 2012 (http://askubuntu.com/users/81372/)
# NAME:         PDFdecrypt 0.3
# DESCRIPTION:  A script to batch decrypt PDF files.
# DEPENDENCIES: qpdf zenity libnotify-bin 
#               (install via sudo apt-get install qpdf zenity libnotify-bin)
# LICENSE:      GNU GPL v3 (http://www.gnu.org/licenses/gpl.html)
# CHANGELOG:    0.3 - added notifications and basic error checking
#               0.2 - replaced obsolete gdialog with zenity

password=$(zenity --password --title "PDF Password required")

RET=$?

if [[ $RET = 0 ]]; then

  while [ $# -gt 0 ]; do
      ENCRYP=$1
      DECRYP=$(echo "$ENCRYP" | sed 's/\.\w*$/_decrypted.pdf/')
      qpdf --password=$password --decrypt "$ENCRYP" "$DECRYP"
      RET=$?
      if [[ $RET != 0 ]]; then
        ERR=1
      fi
      shift
  done

  if [[ $ERR = 1 ]]
    then
        notify-send -i application-pdf "PDFdecrypt" "All documents processed.There were some errors"
    else
        notify-send -i application-pdf "PDFdecrypt" "All documents decrypted."
  fi

else
  exit
fi

Note: This script depends on qpdf, zenity and libnotify-bin. Install them with the command provided in the script.


Usage

Copy and paste the contents of the text box above into a new empty document (right click in file manager: Create new document --> Empty document) and save it as Decrypt PDFs.

Make it executable by marking it as such (right click on file --> Properties --> Permissions --> check Allow executing file as program).

If you are running Ubuntu you can easily install this script to the context menu of your file manager by copying it to ~/.gnome2/nautilus-scripts. You will now be able to decrypt PDFs by selecting them, right clicking and heading to Scripts --> Decrypt PDFs. Enjoy!