Scanning woes dispelled part 2

A few months ago I wrote a post about using a Pixma scanner on Linux to create PDF documents from hard copies. The script has been greatly enhanced and now has a few more features:

  • Select scanner resolution
  • Select page size
  • Option to not pause between pages
  • Option to send PDF as e-mail

The code is released under the Eclipse Public License version 1.0 which means that you can pretty much do what you want with it.

#!/bin/bash
#-----------------------------------------------------------------------------
# pixma2pdf.sh version 2.0
#
# Copyright (c) 2009 Torkild U. Resheim  - torkildr@gmail.com
# Released under the Eclipse Public License version 1.0
#-----------------------------------------------------------------------------
title="Canon Pixma - Scan to PDF"

dialog --title "$title" --checklist "Scan options" 10 50 10 
mail "Send document in e-mail" on 
pause "Confirm between pages" on 2>/tmp/dialog
if [ $? == 1 ] ; then
exit 0
fi
options=$(cat /tmp/dialog)
if [[ "$options" =~ "mail" ]] ; then
address=$(cat /tmp/pixma2pdf)
dialog --title "$title" --inputbox "The address to mail to" 8 40 $address 2>/tmp/pixma2pdf
address=$(cat /tmp/pixma2pdf)
fi
dialog --title "$title" --radiolist "Scanner resolution" 10 40 10 
75 "75dpi Screen" off 
150 "150dpi" off 
300 "300dpi Print" on 
600 "600dpi" off 2>/tmp/dialog
if [ $? == 1 ] ; then
exit 0
fi
resolution=$(cat /tmp/dialog)

dialog --title "$title" --radiolist "Page size" 10 40 10 
a4 "A4" on 
a5 "A5" off 
b5 "B5" off 
letter "Letter" off 2>/tmp/dialog
if [ $? == 1 ] ; then
exit 0
fi
paperSize=$(cat /tmp/dialog)

dialog --title "$title" --inputbox "The number of pages to scan" 8 40 1 2>/tmp/dialog
if [ $? != 1 ]
then
pages=$(cat /tmp/dialog)
for i in `seq $pages` ; do
  if [[ "$options" =~ "pause" ]] ; then
    dialog --title "$title" --msgbox "Preparing to scan page $i" 8 40
  fi
  # Perform the scan
  sudo pixma_scan -a -r $resolution -v 0 -o scan_$i.pnm
  # Exit if there was an error
  if [ $? -ne 0 ] ; then
    exit $?
  fi
  # Attempt to crop the image
  pnmcrop -left -right -top -bottom -sides scan_$i.pnm > cropped_$i.pnm
  # Remove the original image
  sudo rm scan_$i.pnm
  # Convert to PostScript
  pnmtops -equalpixels -dpi=$resolution cropped_$i.pnm > scan_$i.ps
  rm cropped_$i.pnm
done
if [ $pages -gt 1 ] ; then
  # Merge the PostScript files to one
  gs -q -sPAPERSIZE=$paperSize -dNOPAUSE -sDEVICE=pswrite -dBATCH -sOutputFile=scan.ps *.ps
  # Remove the temporary postscript files
  for i in `seq $pages` ; do
    rm scan_$i.ps
  done
else
  mv scan_1.ps scan.ps
fi
# Create the PDF file
ps2pdf -sPAPERSIZE=$paperSize scan.ps scan.pdf
# Clean up
rm scan.ps
if [[ "$options" =~ "mail" ]] ; then
  echo "Your scanned document is attached" | mutt -s "Scanned document" -a scan.pdf $address
fi
fi
rm /tmp/dialog

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.