开发者

Decrypt many PDFs in one go using pdftk

开发者 https://www.devze.com 2023-02-24 11:04 出处:网络
I have 10 PDFs that ask for a user password to open. I know that password. I want to keep them in a decrypted format. Their filenames follow the form:

I have 10 PDFs that ask for a user password to open. I know that password. I want to keep them in a decrypted format. Their filenames follow the form: static_part.dynamic_part_like_date.pdf

I want to convert all 开发者_如何学JAVAthe 10 files. I can give a * after the static part and work on all of them, but I also want the corresponding output filenames. So there has to be a way to capture the dynamic part of the filename and then use it in the output filename.

The normal way of doing this for one file is:

pdftk secured.pdf input_pw foopass output unsecured.pdf

I want to do something like:

pdftk var=secured*.pdf input_pw foopass output unsecured+var.pdf

Thanks.


Your request is a little ambiguous, but here are some ideas that might help you.

Assuming 1 of your 10 files is

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

And you want only the SalesReport.pdf converted as unsecured, you can use a shell script to achieve your requirement:

# 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

You may find that you need to modify the %.* thing to

  • strip less from end, (use %.*) to strip just the last '.' and all chars after (strip from right).
  • strip from the fron (use #*.) to just the static part, leaving the dynamic part OR
  • strip from the front (use ##*.) to strip everything until the last '.' char.

It will really be much easier for you to figure out what you need at the cmd-line. Set a variable with 1 sample fileName

myTestFileName=staticPart.dynamicPart.pdf

and then use echo combined with the variable modifiers to see the results.

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

etc.

Also notice how I combine a modified variable value with a plain string (.pdf), in unsecuredName=${file%%.*}.pdf

IHTH

0

精彩评论

暂无评论...
验证码 换一张
取 消