Skip to content

Instantly share code, notes, and snippets.

@turesheim
Created May 5, 2011 06:51
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 turesheim/956652 to your computer and use it in GitHub Desktop.
Save turesheim/956652 to your computer and use it in GitHub Desktop.
Bash script to create PDF files using Canon Pixma scanner on Linux
#!/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 >/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
@turesheim
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment