Commit cc090c18 authored by Oliver Kennedy's avatar Oliver Kennedy

Initial commit

One Touch Scanning on a Pi
I have a ScanSnap ix500.
I need to be able to push the button on the scanner and get scans uploaded to my NextCloud.
This repository documents how to achieve this.
#### Equipment/Software Overview
* Raspberry Pi 2B+/Raspbian (should work on any debian-flavored distro)
* ScanSnap ix500 (should work with a wide range of other scanners)
* SANE/SANE-utils (apt)
* scanbd (apt)
* imagemagick (apt)
* [scanpdf](
#### SANE
SANE is a standard interface/API for scanners.
$> sudo apt install sane sane-utils
These packages provide a range of tools for scanning on the command line. To see whether your scanner is recognized:
$> scanimage -L
You should see it list the ix500. If it doesn't, try to run the above command with `sudo`. If so, you may need to add a udev rule to allow access to the scanner (this one is specific to the ix500)
$> echo -e "\n#ScanSnap ix500\nATTRS{idVendor}=="04c5", ATTRS{idProduct}=="132b", ENV{libsane_matched}="yes"" >> /etc/udev/rules.d/40-fujitsu.rules
The following command should trigger the scanner to scan a page
$> scanimage --format=tiff --mode Color > image.tiff
Scanbd is enough for simple scanning, but we're going to add two additional features:
* `scanbd` is a daemon that triggers scripts when a scanner's button(s) is(are) pushed.
* `scanpdf` is a simple python app that automates scanning and common post-processing tasks.
#### scanbd
`scanbd` is a daemon that monitors the button(s) and sensors on a wide range of scanners, allowing
scripts to be triggered when the button is pressed (or paper is fed into the hopper, etc...)
$> sudo apt install scanbd
$> sudo systemctl enable scanbd
$> sudo systemctl start scanbd
The configuration lives in `/etc/scanbd/scanbd.conf`
The default configuation points to `/etc/scanbd/scripts/test.script` for the "scan" button, and this file must be executable.
#### ImageMagick
ImageMagick is a general-purpose tool for transforming images. It's needed by scanpdf.
$> sudo apt install imagemagick
One of ImageMagick's main use cases is to provide image transformation functionality for PHP. Because of this, it has a fairly comprehensive permissions scheme that defaults to a really locked down state. These defaults are too locked down for `scanpdf`.
**Warning:** Do not do this on a box that is also acting as a public-facing webserver.
$> sudo nano /etc/ImageMagick-6/policy.xml
Comment out the restriction on creating PDFs by replacing the following line (near the bottom):
<policy domain="coder" rights="none" pattern="PDF" />
with the following:
<!-- policy domain="coder" rights="none" pattern="PDF" -->
#### scanpdf
`scanpdf` is a wrapper script around some of the sane utils and imagemagick that automates common scanning and post-processing tasks:
* Sequential **two-sided** scanning as long as there is paper in the feeder.
* Deleting blank pages (e.g., if your documents are only one-sided)
* Converting images to B/W or Greyscale if there is no color
* Cropping margins off of images
* Concatenating scans into a single PDF.
You'll need python3 installed. I'm going to assume that's already the case.
ScanPDF does exist in Pypi, but the version there is a bit out-of-date.
$> git clone
$> cd scanpdf
$> pip3 install -r requirements.txt
$> pip3 install -e .
$> python install
You may need to apply a small fix
$> nano scanpdf/
Replace the following line (near the top):
from version import __version__
from .version import __version__
#### Setting up the scanner
From the root of **this** repository
$> sudo mkdir /etc/scanbd/scripts
$> sudo cp scan.script /etc/scanbd/scripts/test.script
$> sudo cp postprocess.script /etc/scanbd/scripts/postprocess.script
$> sudo chmod +x /etc/scanbd/scripts/test.script
$> sudo chmod +x /etc/scanbd/scripts/postprocess.script
$> sudo nano /etc/scanbd/scripts/postprocess.script
Edit the postprocessing script to meet your needs.
\ No newline at end of file
scanfile=/tmp/$(basename $tempdir).pdf
# Assuming passwords are in
# - ~pi/.hass_bearer
# - ~pi/.cloud_bot
# Change these below if needed
curl -X POST \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $(cat ~pi/.hass_bearer | tr -d '\n')" \
-d '{"attributes": {"friendly_name": "ix500 Scanner"}, "state": "processing"}' \
logger -t "scanbd" "Postprocessing ${tempdir} to ${scanfile}"
scanpdf --dpi=300 --tmpdir="${tempdir}" pdf "${scanfile}" 2>&1
logger -t "scanbd" "Uploading ${tempdir} to ${scanfile}"
curl -X POST \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $(cat ~pi/.hass_bearer | tr -d '\n')" \
-d '{"attributes": {"friendly_name": "ix500 Scanner"}, "state": "uploading"}' \
curl -T ${scanfile} \
-u "bot:$(cat ~pi/.cloud_bot|tr -d '\n')" \
"${NEXTCLOUD_HOST}/remote.php/dav/files/${NEXTCLOUD_USER}/Scans/$(basename $scanfile)"
rm -f $scanfile
rm -rf $tempdir
curl -X POST \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $(cat ~pi/.hass_bearer | tr -d '\n')" \
-d '{"attributes": {"friendly_name": "ix500 Scanner"}, "state": "idle"}' \
\ No newline at end of file
logger -t "scanbd" "Begin of $SCANBD_ACTION for device $SCANBD_DEVICE"
current_date=$(date +"%Y_%m_%d_%H%M%S"| tr -d '\n')
logger -t "scanbd" "Scanning to ${tempdir}"
scanpdf --dpi=300 --tmpdir="${tempdir}" scan --keep-tmpdir 2>&1
/etc/scanbd/scripts/postprocess.script $tempdir &
logger -t "scanbd" "End of $SCANBD_ACTION for device $SCANBD_DEVICE"
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment