Skip to content

Instantly share code, notes, and snippets.

@diego021
Last active April 9, 2019 04:58
Show Gist options
  • Save diego021/260df2ade47721771eb503939b620f1f to your computer and use it in GitHub Desktop.
Save diego021/260df2ade47721771eb503939b620f1f to your computer and use it in GitHub Desktop.
Add/update metadata to an existing PDF file and encrypt it with random password.
#!/bin/bash
#
# Add/update metadata to an existing PDF file.
# Encrypt it with random password.
#
CREATOR="Diego Arias"
TITLE="Document title"
PRODUCER="GNU/Linux PDF"
KEYWORDS="keyword1, keyword2"
SUBJECT="Document subject"
AUTHOR="Diego Arias"
if [ $# -ne 1 ]; then
echo "You must enter PDF filename as a parameter."
exit 1
else
if [ ! -f "$1" ]; then
echo "The file \"$1\" doesn't exist."
exit 1
fi
cp "$1" "$1".backup
fi
echo InfoKey: Creator > report.cfg
echo InfoValue: $CREATOR >> report.cfg
echo InfoKey: Title >> report.cfg
echo InfoValue: $TITLE >> report.cfg
echo InfoKey: Producer >> report.cfg
echo InfoValue: $PRODUCER >> report.cfg
echo InfoKey: Keywords >> report.cfg
echo InfoValue: $KEYWORDS >> report.cfg
echo InfoKey: Subject >> report.cfg
echo InfoValue: $SUBJECT >> report.cfg
echo InfoKey: Author >> report.cfg
echo InfoValue: $AUTHOR >> report.cfg
if [ ! -f report.cfg ]; then
echo "The file \"report.cfg\" cannot be readed. Check if you have write permissions in the current directory."
exit 1
fi
clear
echo "Make sure you have configured metadata variables to define into the PDF file. If you have done it and everything is okay, press any key to continue..."
read _
pdftk "$1" update_info report.cfg output "$1".1 && echo "Loading metadata" || echo "Failed to insert metadata"
pdftk "$1".1 output "$1".2 encrypt_128bit owner_pw $(apg -a 1 -n 1 -m 50) allow printing && echo "File created successfuly" || echo "Failed to encript"
mv "$1".2 "$1"
rm -f "$1".1 report.cfg
echo ""
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment