Created
January 25, 2022 23:50
-
-
Save chamalis/9d4a60ef9a9fda6d76c5c93cc894d964 to your computer and use it in GitHub Desktop.
Remove metadata from PDF files, using pdftk, exiftool and qpdf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Ensure that required tools are installed and available in $PATH | |
command -v pdftk >/dev/null 2>&1 || { echo >&2 "pdftk is required but was not found. Aborting."; exit 1; } | |
command -v exiftool >/dev/null 2>&1 || { echo >&2 "exiftool is required but was not found. Aborting."; exit 1; } | |
command -v qpdf >/dev/null 2>&1 || { echo >&2 "qpdf is required but was not found. Aborting."; exit 1; } | |
# Ensure that 1 cmdline argument was passed | |
if [ "$#" -ne 1 ] | |
then | |
echo "Usage: cleanpdf.sh path-to-pdf-file.pdf" | |
echo "" | |
exit 1 | |
fi | |
ifile="$1" | |
# Check if the file exists | |
if ! [[ -s "$ifile" ]] | |
then | |
echo "$ifile either does not exist or is empty" | |
exit 1 | |
fi | |
# Name output file based on the input file | |
tmp=$(basename "$ifile" .pdf) | |
ofile="${tmp}_nometa.pdf" | |
# Remove metadata | |
echo "Processing $ifile ..." | |
echo "" | |
pdftk $ifile cat 1-end output /tmp/tmp1.pdf | |
exiftool -all:all= -overwrite_original /tmp/tmp1.pdf | |
qpdf --linearize /tmp/tmp1.pdf "$ofile" | |
# Change the access/modification date in the local Filesystem to a random date | |
randd=`date -d "$((RANDOM%12+2010))-$((RANDOM%12+1))-$((RANDOM%28+1)) $((RANDOM%23+1)):$((RANDOM%59+1)):$((RANDOM%59+1))" '+%Y%m%d%H%M.%S'` | |
# Note that the following command will zero the nanoseconds, an indication that the date has been tampered | |
touch -t "$randd" "$ofile" | |
echo "" | |
echo "Finished processing" | |
echo "Clean file written at $ofile" | |
echo "" | |
echo "New Meta:" | |
pdfinfo "$ofile" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment