PDFtk Password Protection Help

Posted by Dave W. on Ask Ubuntu See other posts from Ask Ubuntu or by Dave W.
Published on 2012-02-23T17:02:35Z Indexed on 2012/03/21 17:38 UTC
Read the original article Hit count: 234

Filed under:
|

I am using Ubuntu 11.10 and am looking for a solution to password protect a bunch of pdf files in a directory in batch. I came across PDFtk and it looks like it might do what I need, but I've reviewed the command line PDFtk examples and can't figure out if there is a way to do it in batch without having to individually specify the output file name for every file. I'm hoping a command-line guru can take a look at the PDFtk syntax and tell me if there is some trick / command that will allow me to password protect a directory of pdf files (e.g., *.pdf) and overwrite the existing files using the same name, or consistently rename the individual output files without having to specify each output name individually.

Here's a link to the PDFtk command line examples page: http://www.pdflabs.com/tools/pdftk-the-pdf-toolkit/

Thanks for your help.


I think I've answered my own question. Here's a bash script that appears to do the trick. I'd welcome help evaluating why the code I've commented out doesn't work...

#!/bin/bash
# Created by Dave, 2012-02-23
# This script uses PDFtk to password protect every PDF file
# in the directory specified. The script creates a directory named "protected_[DATE]"
# to hold the password protected version of the files.
# 
# I'm using the "user_pw" parameter, 
# which means no one will be able to open or view the file without
# the password.
#
# PDFtk must be installed for this script to work.
#
# Usage: ./protect_with_pdftk.bsh [FILE(S)]
# [FILE(S)] can use wildcard expansion (e.g., *.pdf)

# This part isn't working.... ignore. The goal is to avoid errors if the
# directory to be created already exists by only attempting to create
# it if it doesn't exists
#
#TARGET_DIR="protected_$(date +%F)"
#if [ -d "$TARGET_DIR" ]
#then
    #echo   # echo "$TARGET_DIR directory exists!"
#else
    #echo   # echo "$TARGET_DIR directory does not exist!"
#fi
#

mkdir protected_$(date +%F) 
for i in *pdf ; do pdftk "$i" output "./protected_$(date +%F)/$i" user_pw [PASSWORD]; done 
echo "Complete. Output is in the directory: ./protected_$(date +%F)"

© Ask Ubuntu or respective owner

Related posts about command-line

Related posts about pdf