init commit
This commit is contained in:
@@ -0,0 +1,4 @@
|
|||||||
|
env
|
||||||
|
*.pdf
|
||||||
|
*.png
|
||||||
|
*.jpg
|
||||||
@@ -0,0 +1,87 @@
|
|||||||
|
# scan-station
|
||||||
|
|
||||||
|
Scan-to-Paperless for a Fujitsu ScanSnap iX500 attached to a Linux VM (Proxmox USB passthrough).
|
||||||
|
Load paper (or press the scan button) and the pages are scanned duplex, compressed, stripped of blank pages, and uploaded to Paperless-ngx.
|
||||||
|
|
||||||
|
## How it works
|
||||||
|
|
||||||
|
```
|
||||||
|
iX500 (USB) -> scanbd (polls button + page-loaded sensor)
|
||||||
|
-> scan.sh: scanimage -> JPEG -> blank filter -> img2pdf -> Paperless API
|
||||||
|
```
|
||||||
|
|
||||||
|
- **Triggers:** scan button (`scan` action) and paper loaded in the feeder (`pageloaded` action, 3 s settle delay).
|
||||||
|
- **Blank pages:** pages below `BLANK_THRESHOLD` ink are left out of the upload. A full copy of every scan (blanks included) is kept in `/var/lib/scan-station/archive`.
|
||||||
|
- **Barcode splitting:** patch sheets work with Paperless barcode separators. The blank back of a patch sheet is dropped by the blank filter, the barcode front is consumed by Paperless.
|
||||||
|
- **Failures:** if an upload fails, the PDF is saved to `/var/lib/scan-station/failed`.
|
||||||
|
- **Cleanup:** a daily cron job deletes archive files older than 60 days.
|
||||||
|
|
||||||
|
## Layout
|
||||||
|
|
||||||
|
| Path | Installed to |
|
||||||
|
|---|---|
|
||||||
|
| `scripts/scan.sh` | `/etc/scanbd/scripts/scan.sh` |
|
||||||
|
| `config/env.example` | `/etc/scan-station/env` (copy, then edit) |
|
||||||
|
| `config/scanbd-actions.conf` | merge into `/etc/scanbd/scanbd.conf` |
|
||||||
|
| `config/scan-station-cleanup.cron` | `/etc/cron.d/scan-station-cleanup` |
|
||||||
|
| `install.sh` | helper that does the above (except the scanbd.conf merge) |
|
||||||
|
|
||||||
|
## Setup
|
||||||
|
|
||||||
|
1. **Proxmox:** pass the scanner through to the VM (`qm set <vmid> -usb0 host=<vendor:product>`; get IDs from `lsusb`).
|
||||||
|
2. **Packages (VM):**
|
||||||
|
```bash
|
||||||
|
sudo apt install sane-utils scanbd img2pdf imagemagick curl
|
||||||
|
```
|
||||||
|
3. **SANE config.** On this Ubuntu install scanbd uses `/etc/scanbd` as its SANE config dir (`SANE_CONFIG_DIR` in `scanbd.conf`).
|
||||||
|
```bash
|
||||||
|
echo net | sudo tee /etc/sane.d/dll.conf
|
||||||
|
echo localhost | sudo tee /etc/sane.d/net.conf
|
||||||
|
echo fujitsu | sudo tee /etc/scanbd/dll.conf
|
||||||
|
```
|
||||||
|
4. **Permissions:** `saned` must be in the `scanner` group (`sudo usermod -aG scanner saned`).
|
||||||
|
5. **Install the files:**
|
||||||
|
```bash
|
||||||
|
sudo ./install.sh
|
||||||
|
sudo nano /etc/scan-station/env # set PAPERLESS_TOKEN
|
||||||
|
```
|
||||||
|
6. **scanbd actions:** replace the `action scan { ... }` block in `/etc/scanbd/scanbd.conf` with the blocks in `config/scanbd-actions.conf`. Comment out the other default actions (copy, email, file).
|
||||||
|
7. **Start:**
|
||||||
|
```bash
|
||||||
|
sudo systemctl enable --now scanbd
|
||||||
|
sudo systemctl restart scanbd
|
||||||
|
```
|
||||||
|
|
||||||
|
## Config (`/etc/scan-station/env`)
|
||||||
|
|
||||||
|
| Variable | Meaning |
|
||||||
|
|---|---|
|
||||||
|
| `PAPERLESS_URL` | Base URL of Paperless-ngx |
|
||||||
|
| `PAPERLESS_TOKEN` | API token (dedicated user recommended) |
|
||||||
|
| `RESOLUTION` | Scan DPI (200 is plenty for OCR) |
|
||||||
|
| `ROTATE` | `0` or `180` if scans come out upside down |
|
||||||
|
| `BLANK_THRESHOLD` | Ink fraction below which a page counts as blank (default `0.003`) |
|
||||||
|
|
||||||
|
## Operations
|
||||||
|
|
||||||
|
```bash
|
||||||
|
journalctl -t scan-station -n 30 --no-pager # script log (shows skipped blanks + ink values)
|
||||||
|
journalctl -u scanbd -n 50 --no-pager # scanbd log
|
||||||
|
scanimage -L # lists device via scanbd
|
||||||
|
```
|
||||||
|
|
||||||
|
## Notes and gotchas
|
||||||
|
|
||||||
|
- scanbd owns the USB device. Run `sudo systemctl stop scanbd` before using `scanimage -d fujitsu` directly.
|
||||||
|
- Large uploads through a reverse proxy need a raised body limit. For Nginx Proxy Manager, add in the proxy host's Advanced tab:
|
||||||
|
```nginx
|
||||||
|
client_max_body_size 500m;
|
||||||
|
proxy_request_buffering off;
|
||||||
|
proxy_read_timeout 300s;
|
||||||
|
proxy_send_timeout 300s;
|
||||||
|
client_body_timeout 300s;
|
||||||
|
```
|
||||||
|
Cloudflare's free plan caps uploads at 100 MB (413); JPEG compression keeps files well under that.
|
||||||
|
- The paper-loaded trigger needs a 0 to 1 transition. Remove and reload paper if scanbd started with paper already in the feeder.
|
||||||
|
- Duplex backs flipped but fronts fine? Rotate even-numbered pages with `mogrify -rotate 180 "$WORK"/p_*[02468].*` before the PDF step.
|
||||||
|
- Never commit the real `env` file (it is in `.gitignore`).
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
PAPERLESS_URL="https://paperless.example.com"
|
||||||
|
PAPERLESS_TOKEN="PASTE_TOKEN_HERE"
|
||||||
|
RESOLUTION=200
|
||||||
|
ROTATE=0
|
||||||
|
BLANK_THRESHOLD=0.003
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
30 3 * * * root find /var/lib/scan-station/archive -name "*_full.pdf" -mtime +60 -delete
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
action scan {
|
||||||
|
filter = "^scan.*"
|
||||||
|
numerical-trigger {
|
||||||
|
from-value = 1
|
||||||
|
to-value = 0
|
||||||
|
}
|
||||||
|
desc = "Scan to Paperless (button)"
|
||||||
|
script = "scan.sh"
|
||||||
|
}
|
||||||
|
|
||||||
|
action pageloaded {
|
||||||
|
filter = "^page-loaded.*"
|
||||||
|
numerical-trigger {
|
||||||
|
from-value = 0
|
||||||
|
to-value = 1
|
||||||
|
}
|
||||||
|
desc = "Scan to Paperless (paper loaded)"
|
||||||
|
script = "scan.sh"
|
||||||
|
}
|
||||||
Executable
+29
@@ -0,0 +1,29 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
# Installs scan.sh, env template, and cleanup cron. Run as root from the repo root.
|
||||||
|
# Does NOT edit /etc/scanbd/scanbd.conf: merge config/scanbd-actions.conf by hand.
|
||||||
|
set -euo pipefail
|
||||||
|
|
||||||
|
if [ "$(id -u)" -ne 0 ]; then
|
||||||
|
echo "run as root: sudo ./install.sh" >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
HERE="$(cd "$(dirname "$0")" && pwd)"
|
||||||
|
|
||||||
|
mkdir -p /etc/scanbd/scripts /etc/scan-station \
|
||||||
|
/var/lib/scan-station/failed /var/lib/scan-station/archive
|
||||||
|
|
||||||
|
install -m 755 "$HERE/scripts/scan.sh" /etc/scanbd/scripts/scan.sh
|
||||||
|
|
||||||
|
if [ ! -f /etc/scan-station/env ]; then
|
||||||
|
install -m 640 -o root -g scanner "$HERE/config/env.example" /etc/scan-station/env
|
||||||
|
echo "created /etc/scan-station/env: edit it and set PAPERLESS_TOKEN"
|
||||||
|
else
|
||||||
|
echo "keeping existing /etc/scan-station/env"
|
||||||
|
fi
|
||||||
|
|
||||||
|
install -m 644 "$HERE/config/scan-station-cleanup.cron" /etc/cron.d/scan-station-cleanup
|
||||||
|
|
||||||
|
chown saned:scanner /var/lib/scan-station/failed /var/lib/scan-station/archive
|
||||||
|
|
||||||
|
echo "done. next: merge config/scanbd-actions.conf into /etc/scanbd/scanbd.conf, then: systemctl restart scanbd"
|
||||||
Executable
+82
@@ -0,0 +1,82 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
set -uo pipefail
|
||||||
|
shopt -s nullglob
|
||||||
|
|
||||||
|
source /etc/scan-station/env
|
||||||
|
LOG="logger -t scan-station"
|
||||||
|
ARCHIVE=/var/lib/scan-station/archive
|
||||||
|
BLANK_THRESHOLD="${BLANK_THRESHOLD:-0.003}"
|
||||||
|
|
||||||
|
# One scan at a time (ignores a second trigger mid-scan)
|
||||||
|
exec 9>/tmp/scan-station.lock
|
||||||
|
flock -n 9 || { $LOG "scan already running"; exit 0; }
|
||||||
|
|
||||||
|
# When triggered by paper being loaded, let the stack finish seating
|
||||||
|
if [ "${SCANBD_ACTION:-}" = "pageloaded" ]; then
|
||||||
|
sleep 3
|
||||||
|
fi
|
||||||
|
|
||||||
|
WORK=$(mktemp -d /tmp/scan.XXXXXX)
|
||||||
|
trap 'rm -rf "$WORK"' EXIT
|
||||||
|
STAMP=$(date +%Y-%m-%d_%H%M%S)
|
||||||
|
PDF="$WORK/scan_$STAMP.pdf"
|
||||||
|
FULL="$ARCHIVE/scan_${STAMP}_full.pdf"
|
||||||
|
DEVICE="${SCANBD_DEVICE:-fujitsu}"
|
||||||
|
mkdir -p "$ARCHIVE"
|
||||||
|
|
||||||
|
$LOG "scan started (trigger: ${SCANBD_ACTION:-manual}, device: $DEVICE)"
|
||||||
|
|
||||||
|
# scanimage exits non-zero when the feeder runs empty, which is normal
|
||||||
|
scanimage -d "$DEVICE" \
|
||||||
|
--source "ADF Duplex" --mode Color --resolution "$RESOLUTION" \
|
||||||
|
--format=png --batch="$WORK/p_%03d.png" >/dev/null 2>>"$WORK/scan.err"
|
||||||
|
|
||||||
|
# Compress PNG -> JPEG so PDFs are a few MB instead of hundreds
|
||||||
|
PNGS=("$WORK"/p_*.png)
|
||||||
|
if [ ${#PNGS[@]} -gt 0 ]; then
|
||||||
|
mogrify -format jpg -quality 70 "${PNGS[@]}" 2>>"$WORK/scan.err"
|
||||||
|
rm -f "${PNGS[@]}"
|
||||||
|
fi
|
||||||
|
|
||||||
|
PAGES=("$WORK"/p_*.jpg)
|
||||||
|
if [ ${#PAGES[@]} -eq 0 ]; then
|
||||||
|
$LOG "no pages scanned: $(tail -n1 "$WORK/scan.err")"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Full archive copy: every page, blanks included
|
||||||
|
img2pdf --rotation="${ROTATE:-0}" "${PAGES[@]}" -o "$FULL" 2>>"$WORK/scan.err" \
|
||||||
|
|| $LOG "archive copy failed: $(tail -n1 "$WORK/scan.err")"
|
||||||
|
|
||||||
|
# Blank-page filter: fraction of dark pixels on a downscaled grayscale copy
|
||||||
|
KEEP=()
|
||||||
|
for f in "${PAGES[@]}"; do
|
||||||
|
ink=$(magick "$f" -resize 25% -colorspace Gray -threshold 80% \
|
||||||
|
-format '%[fx:1-mean]' info: 2>/dev/null)
|
||||||
|
if awk -v v="${ink:-1}" -v t="$BLANK_THRESHOLD" 'BEGIN{exit !(v<t)}'; then
|
||||||
|
$LOG "skipped blank $(basename "$f") (ink=$ink)"
|
||||||
|
else
|
||||||
|
KEEP+=("$f")
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
|
||||||
|
if [ ${#KEEP[@]} -eq 0 ]; then
|
||||||
|
$LOG "all pages blank, nothing uploaded (archive: $FULL)"
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
if ! img2pdf --rotation="${ROTATE:-0}" "${KEEP[@]}" -o "$PDF" 2>>"$WORK/scan.err"; then
|
||||||
|
$LOG "img2pdf failed: $(tail -n1 "$WORK/scan.err")"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
if curl -sS --fail --retry 3 --max-time 120 \
|
||||||
|
-H "Authorization: Token $PAPERLESS_TOKEN" \
|
||||||
|
-F "document=@$PDF;type=application/pdf" \
|
||||||
|
"$PAPERLESS_URL/api/documents/post_document/" >/dev/null 2>>"$WORK/scan.err"; then
|
||||||
|
$LOG "uploaded ${#KEEP[@]} of ${#PAGES[@]} pages"
|
||||||
|
else
|
||||||
|
cp "$PDF" /var/lib/scan-station/failed/
|
||||||
|
$LOG "upload failed, saved to /var/lib/scan-station/failed: $(tail -n1 "$WORK/scan.err")"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
Reference in New Issue
Block a user