Wednesday, March 28, 2012

Very high ratio and fast pdf files compression

One day, I send some scanned files in pdf format by email. Due to the large size of those files, the attached files cannot be received in the email sent. This problem disturbed me as I take multidimensional processing course and one of the chapters in the course is about image processing and file compression. After short time searching, I found the very high ratio and fast pdf files compressing from Ubuntu forum.
Here is the code:
-------------------------------------------------------------------------------------

#!/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"
---------------------------------------------------------------------------------------

Let's copy that code in /usr/local/bin and save as optimized_pdf.sh or another name. Then try to compress pdf file like in the following,
optimized_pdf.sh -s input_file.pdf -o output_file.pdf
 where input_file.pdf is your current file to be compressed (signed by -s) and output_file.pdf is the new compressed pdf files. When I use this command, I got 300 Kb from 3000 Kb of pdf file size (1/10 of compression ration). That's work for me!

*) Please note, that is not my code, but I got it from Ubuntuforum.org
Related Posts Plugin for WordPress, Blogger...