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.

82 lines
2.8 KiB

  1. #!/bin/bash
  2. ########################################################################
  3. # Copyright (C) 2015 Max Mehl <mail@mehl.mx>
  4. ########################################################################
  5. #
  6. # This program is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. #
  19. ########################################################################
  20. #
  21. # Removes all PDF metadata from
  22. # a) all PDF files in a directory
  23. # b) or one file given as parameter
  24. # This happens by the usage of pdftk, a powerful command line tool on GNU/Linux
  25. #
  26. ########################################################################
  27. # change to directory where the script resides if tool is called by cron
  28. if [ "$#" == "0" ]; then
  29. echo "Searching for PDF files in this directory..."
  30. ls *.pdf > /dev/null 2>&1
  31. if [ "$?" != "0" ]; then
  32. echo "No files found. Maybe change directory or give one file as parameter."
  33. exit 1
  34. fi
  35. echo "Cleaning all files in the current directory..."
  36. read -p "Should I proceed? Press Enter to continue." END
  37. TMPDIR=$(mktemp -d) # create temporary directory
  38. cp ./*.pdf $TMPDIR
  39. elif [ "$#" == "1" ]; then
  40. if [ -e "$1" -a -f "$1" ]; then
  41. echo "Cleaning $1..."
  42. TMPDIR=$(mktemp -d) # create temporary directory
  43. cp "$1" $TMPDIR
  44. else
  45. echo "$1 does not exist or is no file."
  46. exit 1
  47. fi
  48. else
  49. echo "Too many parameters. Please give no file (cleans all files in current directory) or one file as parameter."
  50. exit 1
  51. fi
  52. for FILE in $TMPDIR/*.pdf; # filename with path in $i
  53. do
  54. FILENAME=$(basename "$FILE") # filename without path, but possible spaces
  55. TMPFILENAME=$(echo $FILENAME | sed "s/ /_/g") # replace spaces by underscores
  56. TMPFILE="$TMPDIR/$TMPFILENAME"
  57. EXTLESS=$(echo $FILENAME | sed "s/\.pdf$//")
  58. BAKFILENAME="$EXTLESS.pdf.bak"
  59. cp "$FILE" "$BAKFILENAME" # Generating backup
  60. mv "$FILE" "$TMPFILE" # rename it as long as we work with it
  61. # Dump data in temporary file
  62. pdftk "$TMPFILE" dump_data output "$TMPFILE".meta
  63. sed 's/InfoValue:.*/InfoValue:/g' "$TMPFILE".meta > "$TMPFILE".meta.clean
  64. pdftk "$TMPFILE" update_info "$TMPFILE".meta.clean output "$FILENAME" # Update Metadata from cleaned metafile and write to original file
  65. echo "Cleaned $FILENAME from metadata. Saved backup to $BAKFILENAME."
  66. done
  67. rm -r $TMPDIR