Skip to content

Instantly share code, notes, and snippets.

@meonkeys
Created February 6, 2012 22:53
Show Gist options
  • Select an option

  • Save meonkeys/1755669 to your computer and use it in GitHub Desktop.

Select an option

Save meonkeys/1755669 to your computer and use it in GitHub Desktop.
Bash script to reduce PDF filesize.
#!/bin/bash
# shrinkpdf - Reduce PDF filesize.
# Copyright (C) 2012 Adam Monsen <haircut@gmail.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
if [ $# -lt 2 ] || [ $# -gt 3 ]; then
echo usage: shrinkpdf \<filename\> \<resolution\> \[\<output\>\]
echo
echo For example: shrinkpdf input.pdf 72
exit
fi
if [ ! -e "$1" ]; then
echo "$1" does not exist. Exiting.
exit
fi
if [ $# = 3 ]; then
NEWNAME=$3
else
NEWNAME=`basename "$1" .pdf`_shrinked.pdf
fi
if [ "$1 " = "$NEWNAME " ]; then
echo Input and output are identical. Won\'t overwrite---exiting.
exit
fi
if [ -e "$NEWNAME" ]; then
echo "$NEWNAME" exists. Delete? \(y/n\)
read ANS
if [ "$ANS " = "y " ]; then
rm "$NEWNAME"
else
exit
fi
fi
gs -q -dNOPAUSE -dBATCH -dSAFER -dQUIET \
-sDEVICE=pdfwrite \
-dCompatibilityLevel=1.4 \
-dPDFSETTINGS=/screen \
-dEmbedAllFonts=true \
-dSubsetFonts=true \
-dColorImageDownsampleType=/Bicubic \
-dColorImageResolution=$2 \
-dGrayImageDownsampleType=/Bicubic \
-dGrayImageResolution=$2 \
-dMonoImageDownsampleType=/Bicubic \
-dMonoImageResolution=$2 \
-sOutputFile="$NEWNAME" \
"$1"
echo $1: $((`wc -c "$1" | cut -d \ -f 1` / 1024 )) kb
echo $NEWNAME: $((`wc -c "$NEWNAME" | cut -d \ -f 1` / 1024 )) kb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment