Skip to content

Instantly share code, notes, and snippets.

@bagustris
Created July 2, 2015 18:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bagustris/12c63f4cd7ea9526a512 to your computer and use it in GitHub Desktop.
Save bagustris/12c63f4cd7ea9526a512 to your computer and use it in GitHub Desktop.
#!/bin/bash
DPI=150
PDF_DESTINATION=""
help() {
echo "optimize_pdf help"
echo "-h : show this help"
echo "-d : (optional) output pdf document resolution, by default : 150"
echo "-s : pdf source file, this file must exist"
echo "-o : pdf output file"
}
full_path() {
if [ -z $1 ]; then
exit;
else
if [ `expr substr ${1:-a} 1 2` != "/" ]; then
FULL_FILE=`pwd`"/"$1
fi
fi
echo $FULL_FILE
}
isNumeric(){ echo "$@" | grep -q -v "[^0-9]" ;}
while getopts "s:o:d:h" flag
do
case $flag in
#Source : source file
"s")
PDF_FILE=`full_path $OPTARG`
if [ ! -e $PDF_FILE ]; then
echo "Please provide a valid source file"
exit=1
fi
;;
#Output : output file
"o")
PDF_DESTINATION=$OPTARG
;;
#Dpi : desired resolution
"d")
if [ -z `isNumeric $OPTARG` ]; then
DPI=$OPTARG
else
echo "Please provide a numeric value for your DPI"
exit=1
fi
;;
"h")
exit=1
;;
esac
done
#Is there a target file?
if [ -z $PDF_DESTINATION ]; then
echo "Please provide a file name for output"
exit=1
fi
#At least one error, we’re not going any further
if [ $exit ]; then
help
exit
fi
pdftops \
-paper match \
-nocrop \
-noshrink \
-nocenter \
-level3 \
-q \
"$PDF_FILE" – \
| ps2pdf14 \
-dEmbedAllFonts=true \
-dUseFlateCompression=true \
-dOptimize=true \
-dProcessColorModel=/DeviceRGB \
-dUseCIEColor=true \
-r72 \
-dDownsampleGrayImages=true \
-dGrayImageResolution=$DPI \
-dAutoFilterGrayImages=false \
-dGrayImageDownsampleType=/Bicubic \
-dDownsampleMonoImages=true \
-dMonoImageResolution=$DPI \
-dMonoImageDownsampleType=/Bicubic \
-dDownsampleColorImages=true \
-dColorImageResolution=$DPI \
-dAutoFilterColorImages=false \
-dColorImageDownsampleType=/Bicubic \
-dPDFSETTINGS=/prepress \
– "$PDF_DESTINATION"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment