ambevar-dotfiles/.scripts/pdfcompress

95 lines
2.0 KiB
Bash
Executable File

#!/bin/sh
## Some JPEG options
# -sDEVICE=jpeg
# -dUseCIEColor
# -dJPEGQ=100
# -r300
# -dTextAlphaBits=4
# -dGraphicsAlphaBits=4
# -dMaxStripSize=8192
_usage () {
cat <<EOF
Usage: ${1##*/} [OPTIONS] PDFFILES
All PDF files will be compressed to a PDF file with "-compressed" appended in its name.
Options:
-c: Compress raster graphics at the expense of a quality loss.
-f: Overwrite existing files.
-h: Show this help.
-i: In-place compression.
-n: Do not compress raster graphics at all. Output size is usually greater.
For more details:
$ gs -sDEVICE=pdfwrite -o /dev/null \\
-c "currentpagedevice { exch ==only ( ) print == } forall"
http://partners.adobe.com/public/developer/en/acrobat/sdk/pdf/pdf_creation_apis_and_specs/DistillerParameters.pdf
EOF
}
OPT_INPLACE=false
OPT_OVERWRITE=false
OPT_COMPRESSION="-dColorConversionStrategy=/LeaveColorUnchanged -dDownsampleMonoImages=false -dDownsampleGrayImages=false -dDownsampleColorImages=false -dAutoFilterColorImages=false -dAutoFilterGrayImages=false -dColorImageFilter=/FlateEncode -dGrayImageFilter=/FlateEncode"
while getopts ":cfhin" opt; do
case $opt in
c)
OPT_COMPRESSION="" ;;
f)
OPT_OVERWRITE=true ;;
h)
_usage "$0"
exit 1 ;;
i)
OPT_INPLACE=true ;;
n)
OPT_COMPRESSION="-dColorConversionStrategy=/LeaveColorUnchanged -dEncodeColorImages=false -dEncodeGrayImages=false -dEncodeMonoImages=false" ;;
\?)
_usage "$0"
exit 1 ;;
esac
done
shift $(($OPTIND - 1))
if [ $# -eq 0 ]; then
_usage "$0"
exit 1
fi
for i ; do
if [ ! -f "$i" ]; then
echo "$i is not a valid file."
continue
fi
case "$i" in
*/*) ;;
*) i="./$i" ;;
esac
DIRNAME="${i%/*}"
BASENAME="${i##*/}"
EXTNAME="${BASENAME##*.}"
BASENAME="${BASENAME%.*}"
OUTPUT="$DIRNAME/$BASENAME-compressed.pdf"
if [ -e "$OUTPUT" ] && ! $OPT_OVERWRITE; then
echo "$OUTPUT alredy present. Skipping."
continue
fi
echo "$i"
gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite $OPT_COMPRESSION -sOutputFile="$OUTPUT" "$i"
if $OPT_INPLACE; then
rm -f "$i"
mv "$OUTPUT" "$i"
fi
done