1

我有 10 个 PDF 要求输入用户密码才能打开。我知道那个密码。我想将它们保存为解密格式。它们的文件名遵循以下形式:static_part.dynamic_part_like_date.pdf

我想转换所有 10 个文件。我可以在静态部分之后加上 * 并处理所有这些,但我也想要相应的输出文件名。所以必须有一种方法来捕获文件名的动态部分,然后在输出文件名中使用它。

对一个文件执行此操作的正常方法是:

pdftk secure.pdf input_pw foopass 输出 unsecured.pdf

我想做类似的事情:

pdftk var=secured*.pdf input_pw foopass 输出 unsecured+var.pdf

谢谢。

4

1 回答 1

3

您的要求有点模棱两可,但这里有一些想法可能会对您有所帮助。

假设您的 10 个文件中有 1 个是

  # static_part.dynamic_part_like_date.pdf
  # SalesReport.20110416.pdf  (YYYYMMDD)

并且您只想将 SalesReport.pdf 转换为不安全的,您可以使用 shell 脚本来实现您的要求:

# make a file with the following contents, 
# then make it executable with `chmod 755 pdfFixer.sh`
# the .../bin/bash has to be the first line the file.

$ cat pdfFixer.sh

#!/bin/bash

# call the script like $ pdfFixer.sh staticPart.*.pdf  
# ( not '$' char in your command, that is the cmd-line prompt in this example,
#   yours may look different )

# use a variable to hold the password you want to use
pw=foopass

for file in ${@} ; do

    # %%.* strips off everything after the first '.' char
    unsecuredName=${file%%.*}.pdf

    #your example : pdftk secured.pdf input_pw foopass output unsecured.pdf
    #converts to
    pdftk ${file} input_pw ${foopass} output ${unsecuredName}.pdf
done

您可能会发现您需要将%.*事物修改为

  • 从末尾少剥离,(使用 %.*)只剥离最后一个 '.' 以及之后的所有字符(从右侧剥离)。
  • 从前部(使用#*。)剥离到静态部分,留下动态部分或
  • 从前面剥离(使用##*。)剥离所有内容,直到最后一个'。' 字符。

在 cmd-line 上弄清楚你需要什么真的会容易得多。使用 1 个示例文件名设置变量

myTestFileName=staticPart.dynamicPart.pdf

然后使用 echo 结合变量修饰符查看结果。

echo ${myTestFileName##*.}
echo ${myTestFileName#*.}
echo ${myTestFileName##.*}
echo ${myTestFileName#.*}
echo ${myTestFileName%%.*}

等等

另请注意我如何将修改后的变量值与纯字符串 (.pdf) 组合在一起,在unsecuredName=${file%%.*}.pdf

IHTH

于 2011-04-17T06:05:48.957 回答