README.md 4.06 KB
Newer Older
Oliver Kennedy's avatar
Oliver Kennedy committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
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](https://github.com/virantha/scanpdf.git)

#### 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 https://github.com/virantha/scanpdf.git
$> cd scanpdf
$> pip3 install -r requirements.txt
$> pip3 install -e .
$> python setup.py install
```

You may need to apply a small fix
```
$> nano scanpdf/scanpdf.py
```
Replace the following line (near the top):
```
from version import __version__
```
with 
```
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.