2014-11-27 17:21:37 +01:00
|
|
|
#!/bin/bash
|
2015-07-07 15:08:52 +02:00
|
|
|
########################################################################
|
|
|
|
# Copyright (C) 2015 Max Mehl <mail@mehl.mx>
|
|
|
|
########################################################################
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
#
|
|
|
|
########################################################################
|
|
|
|
#
|
|
|
|
# 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
|
|
|
|
#
|
|
|
|
########################################################################
|
2014-11-27 17:21:37 +01:00
|
|
|
|
|
|
|
|
|
|
|
# change to directory where the script resides if tool is called by cron
|
|
|
|
|
2015-03-01 00:10:05 +01:00
|
|
|
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
|
|
|
|
fi
|
|
|
|
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
|
|
|
|
else
|
|
|
|
echo "$1 does not exist or is no file."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2014-11-27 17:21:37 +01:00
|
|
|
else
|
2015-03-01 00:10:05 +01:00
|
|
|
echo "Too many parameters. Please give no file (cleans all files in current directory) or one file as parameter."
|
2014-11-27 17:21:37 +01:00
|
|
|
exit 1
|
2015-03-01 00:10:05 +01:00
|
|
|
|
2014-11-27 17:21:37 +01:00
|
|
|
fi
|
|
|
|
|
|
|
|
|
2015-03-01 00:10:05 +01:00
|
|
|
for FILE in $TMPDIR/*.pdf; # filename with path in $i
|
2014-11-27 17:21:37 +01:00
|
|
|
do
|
2015-03-01 00:10:05 +01:00
|
|
|
FILENAME=$(basename "$FILE") # filename without path, but possible spaces
|
|
|
|
TMPFILENAME=$(echo $FILENAME | sed "s/ /_/g") # replace spaces by underscores
|
|
|
|
TMPFILE="$TMPDIR/$TMPFILENAME"
|
|
|
|
EXTLESS=$(echo $FILENAME | sed "s/\.pdf$//")
|
|
|
|
BAKFILENAME="$EXTLESS.pdf.bak"
|
|
|
|
|
|
|
|
cp "$FILE" "$BAKFILENAME" # Generating backup
|
|
|
|
|
|
|
|
mv "$FILE" "$TMPFILE" # rename it as long as we work with it
|
2014-11-27 17:21:37 +01:00
|
|
|
|
|
|
|
# Dump data in temporary file
|
2015-03-01 00:10:05 +01:00
|
|
|
pdftk "$TMPFILE" dump_data output "$TMPFILE".meta
|
2014-11-27 17:21:37 +01:00
|
|
|
|
2015-03-01 00:10:05 +01:00
|
|
|
sed 's/InfoValue:.*/InfoValue:/g' "$TMPFILE".meta > "$TMPFILE".meta.clean
|
2014-11-27 17:21:37 +01:00
|
|
|
|
2015-03-01 00:10:05 +01:00
|
|
|
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."
|
2014-11-27 17:21:37 +01:00
|
|
|
done
|
|
|
|
|
2015-03-01 00:10:05 +01:00
|
|
|
rm -r $TMPDIR
|