Mac Jones from New Zealand, created this great bash script that scans pages from your scanner, then turns them into a PDF. Well, I took that script, and improved upon it. Check it out.
– Producer option for your name
– Output folder, with temp files in /tmp
– Device selection (since scanimage picks up my TV tuner as the default scanner), if more than one devices exists
– Resolution options
– Page size options (since my default was like post card size on my scanner), including a “custom size”
– Progress bar for scanning (however, does not show “progress” at the moment)
– And an option to restart with a new document
I am not a bash script expert, and I just whipped this together. There may be better ways to do things… just let me know
#!/bin/sh
#scan to pdf with metadata, by Mac Jones, New Zealand
#http://maconstuff.blogspot.com/producer=”Your Name” # If you’d like it inputed into the PDF, otherwise, rem the line
outputfolder=”/home/user/ScannedDocuments/” # Pre-fix for last outputted file#scan a batch
#select a scanner if more than one exists, or just uncomment the next line, and comment the rest
#scannerdevice=”-d DEVICE”devices=`scanimage -L | awk ‘{print $2}’ | cut -c 2-50 | tr -d =\’=`
devicenum=`echo $devices | wc -w`
if [ “$devicenum” -gt 1 ]; then
devices=`echo $devices | sed -e ‘s/ / FALSE /g’`
scannerdevice=`zenity –list –title “Device?” –radiolist –column “-” –column “Device” TRUE $devices`
else
scannerdevice=”$devices”
firestart=1
until [ “$restart” -eq 0 ]; do#ask for the resolution
resolution=`zenity –list –title “Resolution?” –radiolist –column “-” –column “Resolution” FALSE 100 TRUE 150 FALSE 300 FALSE 350 FALSE 600`#ask for the page size, mm on most scanners, yours may differ, run scanimage –help -d DEVICE
qpagesize=`zenity –list –title “Page Size?” –radiolist –column “-” –column “Page Size” TRUE Letter FALSE Legal FALSE A4 FALSE Custom`
if [ “$qpagesize” == “Letter” ]; then
pagesize=”-x 215 -y 280″
elif [ “$qpagesize” == “Legal” ]; then
pagesize=”-x 215 -y 355″
elif [ “$qpagesize” == “A4″ ]; then
pagesize=”-x 210 -y 297″
elif [ “$qpagesize” == “Custom” ]; then
pagex=`zenity –entry –text “Enter the page width (mm)” –title “Width”`
pagey=`zenity –entry –text “Enter the page height (mm)” –title “Height”`
pagesize=”-x $pagex -y $pagey”
fi#decide grey or colour (gray or color for the Americans!)
colour=`zenity –list –title “Color or Gray?” –radiolist –column “-” –column “Scan Mode” TRUE Gray FALSE Color`a=0 #page counter
cont=1 #should we continue?until [ $cont -eq “0” ] #keep doing it until cont variable is not a zero.
do
echo -n “$a ”
let “a+=1”
if zenity –question –text “OK to scan a page, Cancel to finish, Page=$a” –title “Scanning pages”
then
cont=1
`scanimage -d $scannerdevice –format pnm –resolution $resolution $pagesize –mode $colour > “/tmp/$a.pnm”` | zenity –progress –auto-close –title “Scanning page…”
else
cont=0
fidone # No surprises, so far.
#convert the raw file to postscript
convert -density $resolution /tmp/*.pnm /tmp/out.ps | zenity –progress –auto-close –title “Converting to Postscript…”#convert the postscript to pdf
ps2pdf /tmp/out.ps /tmp/out.pdf | zenity –progress –auto-close –title “Converting Postscript to PDF…”#remove raw scan files
rm /tmp/*.pnm#remove old ps files
rm /tmp/out.ps#beep to get attention after processing
echo -e “\a”#add the metadata and file name.
#this meta data can be searched from Beagle in Ubuntu.#echo “Please enter a name for the PDF file (** no .pdf on end)”
nm=`zenity –entry –text “Enter file name, (no .pdf on the end)” –title “File Name?”`#echo “Please enter Metadata for searching”
meta=`zenity –entry –text “Meta data for searching” –entry-text=$nm –title “Meta Data for Searching”`echo “InfoKey: Producer” > /tmp/pdfdata.tmp
if [ “$producer” ]; then
echo “InfoValue: $producer” >> /tmp/pdfdata.tmp
else
echo “InfoValue: $meta” >> /tmp/pdfdata.tmp
fi
echo “InfoKey: Keywords” >> /tmp/pdfdata.tmp
echo “InfoValue: $meta” >> /tmp/pdfdata.tmp
echo “InfoKey: Title” >> /tmp/pdfdata.tmp
echo “InfoValue: $nm” >> /tmp/pdfdata.tmp#update the metadata
pdftk /tmp/out.pdf update_info /tmp/pdfdata.tmp output “$outputfolder$nm.pdf”#rm metadata file and pdf
rm /tmp/pdfdata.tmp
rm /tmp/out.pdfif zenity –question –text “$nm.pdf has been created! Would you like to scan another document?” –title “Thanks!”; then
restart=1
else
restart=0
fidone
exit 0