0

The following script makes use of a temporary file "data.txt" to which something is appended before reusing it. (Actually I simply add some metadata to a PDF)

#!/bin/bash

PDFTK="/app/bin/pdftk"
#PDFTK="pdftk"

$PDFTK $1.pdf dump_data output data.txt

cat  >> data.txt << EOF
InfoBegin
InfoKey: Myproperty
InfoValue: Myvalue
EOF

$PDFTK $1.pdf update_info data.txt output $1-$2.pdf

Both dump_data and update_info can write/read from stdout (see man pdftk)

bash-gurus: How can I rewrite the code such that no file is created ?

pdftk-gurus: Is there a better way to add a key/value pair?

Thanks, Bastl.

Bastl
  • 135

1 Answers1

0

Since this is using bash, you may use a process substitution:

"$PDFTK" "$1.pdf" update_info <( "$PDFTK" "$1.pdf" dump_data output; cat <<EOF
InfoBegin
InfoKey: Myproperty
InfoValue: Myvalue
EOF
) output "$1-$2.pdf"

To the pdftk utility, the <( ... ) argument will be treated as a file containing the output of the first pdftk call followed by the contents of the here-document.

Also note that you should be quoting your variables (see "Security implications of forgetting to quote a variable in bash/POSIX shells").

Kusalananda
  • 333,661
  • Thanks for that. please note that the first pdftk actually writes the data.txt in the first place. I concat to it using the HERE document. I imagine it quite ugly to have the first call to pdftk (with dump_data to stdout) and concat with "cat" inside a <(...) ?? (I also have problems running that in my windows bash, but that is probably a local problem) – Bastl Sep 30 '17 at 18:52
  • @Bastl I missed that bit. If pdftk writes to standard output by default, then my updated answer may work. It's not pretty, and honestly, I would probably go with using a temporary file. – Kusalananda Sep 30 '17 at 19:55
  • Works like a charm on my webserver. Sadly not under mingw64 on my development windows machine. The anonymous filehandler does not work ... – Bastl Oct 01 '17 at 15:52