[office] scantopdf revisited

At the airport. Bored. Started hacking up my
scantopdf script
to cater for multiple page documents and a simple scanner.


#!/bin/sh

# scans an A4 image and saves it as a pdf file with given filename
# supports multiple page documents

colourmode="Gray"
depth=8
resolution=300

while getopts cd:r: option
do
case $option in
c) colourmode="Color";;
d) depth=${OPTARG};;
r) resolution=${OPTARG};;
?) printf "Usage: %s [-c] [-d depth] [-r resolution] filename.pdf [number of pages]\n" $0
exit 2;;
esac
done

shift $(($OPTIND - 1))

filename=$1

numpages=$2
if [ -z ${numpages} ]
then
numpages=1
fi

getpdf() {
/usr/bin/scanimage --mode=${colourmode} --depth=${depth} \
--resolution ${resolution} -x 215 -y 297 \
| /usr/bin/pnmtops -noshowpage -equalpixels -dpi=${resolution} \
| /usr/bin/ps2pdf -sPAPERSIZE=a4 - "$*"
}

TMPDIR=$(/usr/bin/mktemp -d)

currentpage=0
while [ ${currentpage} -lt ${numpages} ]
do
currentpage=$((currentpage+1))
echo "Please insert page ${currentpage}/${numpages}."
read -p "Press enter to continue..."
echo "Scanning..."
getpdf ${TMPDIR}/page${currentpage}.pdf
echo "Ready!"
done

/usr/bin/pdftk ${TMPDIR}/*pdf cat output ${filename}

echo "Result is ${filename}. Individual pages can be found in ${TMPDIR}."

It depends on some external tools though and I don’t really like the way their
paths are hard-coded now, but it’s useful as is, still (and very easy to
change). Make sure you have pdftk, scanimage, netpnm and ghostscript installed or it won’t work for
you.

Curious if it will work when I return to the office. :-)

Add a Comment   Trackback  

Add a Comment