PDFtk 密码保护帮助

PDFtk 密码保护帮助

我正在使用 Ubuntu 11.10,正在寻找一种解决方案,可以批量对目录中的一堆 pdf 文件进行密码保护。我遇到了 PDFtk,它看起来可以满足我的需要,但是我查看了命令行 PDFtk 示例,无法弄清楚是否有办法批量执行此操作,而不必为每个文件单独指定输出文件名。我希望命令行专家可以查看 PDFtk 语法并告诉我是否有一些技巧/命令可以让我用密码保护 pdf 文件目录(例如 *.pdf)并使用相同的名称覆盖现有文件,或者一致地重命名各个输出文件,而无需单独指定每个输出名称。

以下是 PDFtk 命令行示例页面的链接:http://www.pdflabs.com/tools/pdftk-the-pdf-toolkit/

感谢您的帮助。


我想我已经回答了自己的问题。这是一个似乎可以解决问题的 bash 脚本。我欢迎帮助评估为什么我注释掉的代码不起作用...

#!/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)"

答案1

我已经测试了您注释掉的部分(并且做了一些修改以便能够证明它有效):

#!/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

这是第一次运行后的输出:

protected_2012-02-23 directory does not exist!

这是第二次运行后的输出:

protected_2012-02-23 directory exists!

因此它在分支$TARGET_DIR中成功创建了else。您遇到了什么错误导致您注释掉该部分?

答案2

上面的一位发帖人要求我将我的答案重新发布为实际答案以供后人参考。这就是答案。感谢大家的帮助。


我已经回答了自己的问题。这是一个似乎可以解决问题的 bash 脚本。我欢迎您帮助评估为什么我注释掉的代码不起作用...

#!/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)"

答案3

有一个图形工具叫做pdfsam。您可以通过以下方式通过 Ubuntu 默认存储库安装它:

sudo apt-get install pdfsam

或者在 Ubuntu 软件中心用他的名字搜索。

它是用 Java 制作的,它的 UI 看起来有些旧而且有点复杂,但可以很好地完成工作。

相关内容