Scanning to a Multi-Page PDF from Command Line

I never described how I do multi-page scanning but it is actually just a simple script. All the heavy-lifting is done by scanimage that is actually doing very well. The script is only there to pass it the necessary options…

scanimage doesn’t do PDF but it does multi-page scanning perfectly. I own an old Canon LiDE 20 and the fact that it can wait for a keypress before scanning the next page is very nice (lacking the automatic feeder).

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
#!/bin/bash

if [ -f scanned.page-*.jpg ]; then
        echo "Page exists, terminating."
        exit 1
fi
if [ -f scan.pdf ]; then
        echo "Scan exists, terminating."
        exit 1
fi

scanner=$(sane-find-scanner | grep CanoScan)
scanneraddr="genesys:libusb:"${scanner:(-7)}

scanimage -d "$scanneraddr" --contrast 27 -x 210 -y 297 --mode Color --resolution 300 --format=jpeg --batch-prompt --batch='scanned.page-%d.jpg'
convert scanned.page-*.jpg scan.pdf
rm scanned.page-*.jpg

What is going on here? First a check to see if files with names scanned.page-*.jpg and scan.pdf exist in the working directory and exit with an error if they do (else the script would overwrite them). Line 12 uses a tool from sane package to find where exactly on the USB bus the scanner is today (the scanner reports its name as “CanoScan”); the last 7 characters on that line would be the address on the bus (xxx:xxx). We know the driver and the bus (line 13) so we can run scanimage in multi-page mode with manual confirmation for each page right away. That’s actually what line 15 does. The page size is A4 (I usually scan pages of this size) and the optimal contrast has been experimentally selected for this particular scanner. Finally, convert from the ImageMagick package compiles the resulting JPEGs into a PDF.

After composing this script (quite some time ago) I promised to myself that I’d add command line options to choose file name or other parameters such as page size as soon as I need them. I haven’t yet felt such a need.