#!/bin/bash
# RIP a PDF into 1 bit screened TIFFs, convert to PS, and then back into PDF
# Set filename to first command line arguement
# dpi and lpi to 2nd and 3rd if they are present
# otherwise default to 600DPI 106LPI if not specified
if [ $1 ]
then file=$1
else echo "No input file specified." && exit
fi
if [ $2 ]
then dpi=$2
else dpi=600
fi
if [ $3 ]
then lpi=$3
else lpi=106
di
# Is this a PDF?
echo -e "Is this file a PDF?"
if [ "${1##*.}" = "pdf" ]
then echo -e "\nYes."
else echo "Not a PDF." && exit
fi
# Create the directory structure ./$name/ and ./$name/TIFF/,
# move the file into it, and cd
name=${file%.pdf}
dir=$(dirname $file)
echo -e "\nMaking directory $dir/$name"
mkdir $dir/$name
echo -e "\nMaking directory $dir/$name/TIFF"
mkdir $dir/$name/TIFF
mv $file $dir/$name
cd $dir/$name
# Get PDF dimensions with the Imagemajick utility "identify"
echo -e "\nCalculating PDF dimensions ..."
wh=$(identify -format "%w %h" $file[1])
w=$(echo $wh | awk '{print $1}')
h=$(echo $wh | awk '{print $2}')
echo -e "\nRIPing $(($w / 72))\" x $(($h / 72))\" TIFF's @ $dpi dpi / $lpi lpi ..."
gs -dBATCH -dNOPAUSE -sDEVICE=tiffg4 \
-sOutputFile=TIFF/${name}_p%03d.tif \
-dDEVICEWIDTHPOINTS=$w -dDEVICEHEIGHTPOINTS=$h -r$dpi \
-dDITHERPPI=$lpi -dCOLORSCREEN=false -dDOINTERPOLATE $file
echo -e "\nConverting the TIFFs into Postscript ..."
tiff2ps TIFF/*.tif -O $name.ps
echo -e "\nConverting the Postcript back into PDF ..."
ps2pdf14 -dDEVICEWIDTHPOINTS=$w -dDEVICEHEIGHTPOINTS=$h $name.ps ${name}_GS_${dpi}DPI_${lpi}LPI.pdf
echo -e "\nCleaning up ..."
rm $name.ps
echo -e "\nSuccess!\n"
fi