Removes all metadata from one or more PDF files
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

61 lines
1.9 KiB

# A very simple script by Max Mehl:
# Removes all PDF metadata from
# a) all PDF files in a directory
# b) or one file given as parameter
# This happens by the usage of pdftk, a powerful command line tool on GNU/Linux
# change to directory where the script resides if tool is called by cron
if [ "$#" == "0" ]; then
echo "Searching for PDF files in this directory..."
ls *.pdf > /dev/null 2>&1
if [ "$?" != "0" ]; then
echo "No files found. Maybe change directory or give one file as parameter."
exit 1
echo "Cleaning all files in the current directory..."
read -p "Should I proceed? Press Enter to continue." END
TMPDIR=$(mktemp -d) # create temporary directory
cp ./*.pdf $TMPDIR
elif [ "$#" == "1" ]; then
if [ -e "$1" -a -f "$1" ]; then
echo "Cleaning $1..."
TMPDIR=$(mktemp -d) # create temporary directory
cp "$1" $TMPDIR
echo "$1 does not exist or is no file."
exit 1
echo "Too many parameters. Please give no file (cleans all files in current directory) or one file as parameter."
exit 1
for FILE in $TMPDIR/*.pdf; # filename with path in $i
FILENAME=$(basename "$FILE") # filename without path, but possible spaces
TMPFILENAME=$(echo $FILENAME | sed "s/ /_/g") # replace spaces by underscores
EXTLESS=$(echo $FILENAME | sed "s/\.pdf$//")
cp "$FILE" "$BAKFILENAME" # Generating backup
mv "$FILE" "$TMPFILE" # rename it as long as we work with it
# Dump data in temporary file
pdftk "$TMPFILE" dump_data output "$TMPFILE".meta
sed 's/InfoValue:.*/InfoValue:/g' "$TMPFILE".meta > "$TMPFILE".meta.clean
pdftk "$TMPFILE" update_info "$TMPFILE".meta.clean output "$FILENAME" # Update Metadata from cleaned metafile and write to original file
echo "Cleaned $FILENAME from metadata. Saved backup to $BAKFILENAME."
rm -r $TMPDIR