Friday, April 26, 2024
1
rated 0 times [  1] [ 0]  / answers: 1 / hits: 3931  / 1 Year ago, tue, march 28, 2023, 12:02:02

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

More From » command-line

 Answers
6

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

[#40197] Wednesday, March 29, 2023, 1 Year  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
olfdit

Total Points: 118
Total Questions: 98
Total Answers: 97

Location: Honduras
Member since Fri, Nov 25, 2022
1 Year ago
;