【问题标题】:Decrypt many PDFs in one go using pdftk使用 pdftk 一次性解密多个 PDF
【发布时间】:2011-04-14 14:29:03
【问题描述】:

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

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

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

pdftk secure.pdf input_pw foopass output unsecured.pdf

我想做这样的事情:

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

谢谢。

【问题讨论】:

    标签: unix pdftk


    【解决方案1】:

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

    假设您的 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%%.*}
    

    等等

    还要注意我如何在unsecuredName=${file%%.*}.pdf 中将修改后的变量值与纯字符串 (.pdf) 结合起来

    IHTH

    【讨论】:

    • 很抱歉,我还没有尝试过您的解决方案。我也想要一个单行命令而不是 shell 脚本。感谢您的努力和回答。
    猜你喜欢
    • 2016-09-19
    • 2012-08-13
    • 2023-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-21
    • 2013-09-16
    相关资源
    最近更新 更多