2

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)"
  • 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... – Dave W. Feb 23 '12 at 18:44
  • It would be nice if you provide your answer actually as an answer and accept it. :-) – qbi Feb 4 '13 at 13:23
1

I've tested the part you commented out (and modified just a bit to be able to demonstrate that it works):

#!/bin/bash

TARGET_DIR="protected_$(date +%F)"
if [ -d "$TARGET_DIR" ]
then
    echo "$TARGET_DIR directory exists!"
else
    echo "$TARGET_DIR directory does not exist!"
    mkdir "$TARGET_DIR"
fi

This is the output after the first run:

protected_2012-02-23 directory does not exist!

This is the output after the second run:

protected_2012-02-23 directory exists!

So it successfully creates the $TARGET_DIR in the else branch. What bug did you experience that made you comment out that section?

  • Thanks lgarzo. Hmm... I'm not sure what the error was. When I get a few minutes, I try your revisions and either repost the modified script or note the errors I'm getting. Again, thanks for your assistance. – Dave W. Mar 10 '12 at 20:23
1

A poster above asked that I re-post my answer as an actual answer for posterity. Here is the answer. Thanks to everyone for your help.


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)"
0

There's a graphical tool called pdfsam. You can install it by Ubuntu default repos this way:

sudo apt-get install pdfsam

or by searching with his name in Ubuntu Software Center.

It's made in Java and his UI seems old and a little tricky, but can do the job fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.