ambevar-dotfiles/.scripts/pdfcompress

69 lines
1.3 KiB
Bash
Executable File

#!/bin/sh
_printhelp ()
{
cat <<EOF
Usage: ${1##*/} [OPTIONS] PDFFILES
All PDF files will be compressed to a PDF file with "-compressed" appended in its name.
WARNING: use this function with caution. It may drastically improve compression
of some PDF files, but in some case, the output filesize will be greater! You
should not use it over PDF files embedding raster graphics.
Options:
-f: Overwrite existing files.
-i: In-place compression.
EOF
}
OPT_INPLACE=false
OPT_OVERWRITE=false
while getopts ":ifh" opt; do
case $opt in
h)
_printhelp "$0"
exit 1 ;;
i)
OPT_INPLACE=true ;;
f)
OPT_OVERWRITE=true ;;
?)
_printhelp "$0"
exit 1 ;;
:)
_printhelp "$0"
exit 1 ;;
esac
done
shift $(($OPTIND - 1))
if [ $# -eq 0 ]; then
_printhelp "$0"
exit 1
fi
for i ; do
if [ ! -f "$i" ]; then
echo "$i is not a valid file."
continue
fi
OUTPUT="${i%.*}-compressed.pdf"
if [ -e "$OUTPUT" ] && ! $OPT_OVERWRITE; then
echo "$OUTPUT alredy present. Skipping."
continue
fi
echo "$i"
gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile="$OUTPUT" "$i"
if $OPT_INPLACE; then
rm -f "$i"
mv "$OUTPUT" "$i"
fi
done