From 5aa20e38aba6485c57c016eb4964c3c748fe0025 Mon Sep 17 00:00:00 2001
From: Marty Oehme <marty.oehme@gmail.com>
Date: Sun, 16 Feb 2025 21:07:00 +0100
Subject: [PATCH] Add paperless stack

---
 paperless/docker-compose.env                |  42 +++++++
 paperless/docker-compose.simple_example.yml |  55 ++++++++++
 paperless/docker-compose.yml                | 116 ++++++++++++++++++++
 paperless/scantopaperless.sh                |  28 +++++
 4 files changed, 241 insertions(+)
 create mode 100644 paperless/docker-compose.env
 create mode 100644 paperless/docker-compose.simple_example.yml
 create mode 100644 paperless/docker-compose.yml
 create mode 100755 paperless/scantopaperless.sh

diff --git a/paperless/docker-compose.env b/paperless/docker-compose.env
new file mode 100644
index 0000000..c4dbb4c
--- /dev/null
+++ b/paperless/docker-compose.env
@@ -0,0 +1,42 @@
+# The UID and GID of the user used to run paperless in the container. Set this
+# to your UID and GID on the host so that you have write access to the
+# consumption directory.
+#USERMAP_UID=1000
+#USERMAP_GID=1000
+
+# Additional languages to install for text recognition, separated by a
+# whitespace. Note that this is
+# different from PAPERLESS_OCR_LANGUAGE (default=eng), which defines the
+# language used for OCR.
+# The container installs English, German, Italian, Spanish and French by
+# default.
+# See https://packages.debian.org/search?keywords=tesseract-ocr-&searchon=names&suite=buster
+# for available languages.
+#PAPERLESS_OCR_LANGUAGES=tur ces
+
+###############################################################################
+# Paperless-specific settings                                                 #
+###############################################################################
+
+# All settings defined in the paperless.conf.example can be used here. The
+# Docker setup does not use the configuration file.
+# A few commonly adjusted settings are provided below.
+
+# This is required if you will be exposing Paperless-ngx on a public domain
+# (if doing so please consider security measures such as reverse proxy)
+#PAPERLESS_URL=https://paperless.example.com
+
+# Adjust this key if you plan to make paperless available publicly. It should
+# be a very long sequence of random characters. You don't need to remember it.
+#PAPERLESS_SECRET_KEY=change-me
+
+# Use this variable to set a timezone for the Paperless Docker containers. If not specified, defaults to UTC.
+#PAPERLESS_TIME_ZONE=America/Los_Angeles
+
+# The default language to use for OCR. Set this to the language most of your
+# documents are written in.
+#PAPERLESS_OCR_LANGUAGE=eng
+
+# Set if accessing paperless via a domain subpath e.g. https://domain.com/PATHPREFIX and using a reverse-proxy like traefik or nginx
+#PAPERLESS_FORCE_SCRIPT_NAME=/PATHPREFIX
+#PAPERLESS_STATIC_URL=/PATHPREFIX/static/ # trailing slash required
diff --git a/paperless/docker-compose.simple_example.yml b/paperless/docker-compose.simple_example.yml
new file mode 100644
index 0000000..6b16b8b
--- /dev/null
+++ b/paperless/docker-compose.simple_example.yml
@@ -0,0 +1,55 @@
+# Docker Compose file for running paperless from the Docker Hub.
+# This file contains everything paperless needs to run.
+# Paperless supports amd64, arm and arm64 hardware.
+#
+# All compose files of paperless configure paperless in the following way:
+#
+# - Paperless is (re)started on system boot, if it was running before shutdown.
+# - Docker volumes for storing data are managed by Docker.
+# - Folders for importing and exporting files are created in the same directory
+#   as this file and mounted to the correct folders inside the container.
+# - Paperless listens on port 8000.
+#
+# SQLite is used as the database. The SQLite file is stored in the data volume.
+#
+# To install and update paperless with this file, do the following:
+#
+# - Copy this file as 'docker-compose.yml' and the files 'docker-compose.env'
+#   and '.env' into a folder.
+# - Run 'docker compose pull'.
+# - Run 'docker compose run --rm webserver createsuperuser' to create a user.
+# - Run 'docker compose up -d'.
+#
+# For more extensive installation and update instructions, refer to the
+# documentation.
+
+services:
+  broker:
+    image: docker.io/library/redis:7
+    restart: unless-stopped
+    volumes:
+      - redisdata:/data
+
+  webserver:
+    image: ghcr.io/paperless-ngx/paperless-ngx:latest
+    restart: unless-stopped
+    depends_on:
+      - broker
+    ports:
+      - "8000:8000"
+    volumes:
+      - /home/marty/documents/archive/data:/usr/src/paperless/data
+      - /home/marty/documents/archive/media:/usr/src/paperless/media
+      - /home/marty/documents/archive/export:/usr/src/paperless/export
+      - /home/marty/documents/archive/consume:/usr/src/paperless/consume
+    env_file: docker-compose.env
+    environment:
+      - "PAPERLESS_REDIS=redis://broker:6379"
+      - "PAPERLESS_OCR_LANGUAGES=eng deu frk" # ALL ocr languages to install
+      - "PAPERLESS_OCR_LANGUAGE=deu" # the default ocr language
+      - "PAPERLESS_TIME_ZONE=Europe/Berlin"
+
+volumes:
+  data:
+  media:
+  redisdata:
diff --git a/paperless/docker-compose.yml b/paperless/docker-compose.yml
new file mode 100644
index 0000000..65e3aef
--- /dev/null
+++ b/paperless/docker-compose.yml
@@ -0,0 +1,116 @@
+# Docker Compose file for running paperless from the Docker Hub.
+# This file contains everything paperless needs to run.
+# Paperless supports amd64, arm and arm64 hardware.
+#
+# All compose files of paperless configure paperless in the following way:
+#
+# - Paperless is (re)started on system boot, if it was running before shutdown.
+# - Docker volumes for storing data are managed by Docker.
+# - Folders for importing and exporting files are created in the same directory
+#   as this file and mounted to the correct folders inside the container.
+# - Paperless listens on port 8000.
+#
+# SQLite is used as the database. The SQLite file is stored in the data volume.
+#
+# To install and update paperless with this file, do the following:
+#
+# - Copy this file as 'docker-compose.yml' and the files 'docker-compose.env'
+#   and '.env' into a folder.
+# - Run 'docker compose pull'.
+# - Run 'docker compose run --rm webserver createsuperuser' to create a user.
+# - use anything (secure) for username:pw
+# - Run 'docker compose up -d'.
+#
+# For more extensive installation and update instructions, refer to the
+# documentation.
+
+services:
+  paperless:
+    container_name: paperless
+    image: ghcr.io/paperless-ngx/paperless-ngx:latest
+    restart: unless-stopped
+    networks:
+      - frontend
+      - backend
+    ports:
+      - 8000:8000
+    env_file: docker-compose.env
+    security_opt:
+      - no-new-privileges:true
+    depends_on:
+      - paperless-redis
+      - paperless-postgres
+    volumes:
+      - data:/usr/src/paperless/data # container data
+      - /home/marty/documents/archive/.media:/usr/src/paperless/media # document location
+      - export:/usr/src/paperless/export # backup location
+      - /home/marty/documents/archive/consume:/usr/src/paperless/consume # watch folder
+    environment:
+      # - "PAPERLESS_TIME_ZONE=$TZ"
+      - "PAPERLESS_TIME_ZONE=Europe/Berlin"
+      - "PAPERLESS_OCR_LANGUAGE=deu+eng" # the default ocr language
+      - "PAPERLESS_OCR_LANGUAGES=eng deu frk" # ALL ocr languages to install
+      - "PAPERLESS_OCR_SKIP_ARCHIVE_FILE=with_text"
+      - "PAPERLESS_ENABLE_UPDATE_CHECK=true"
+      - "PAPERLESS_REDIS=redis://paperless-redis:6379"
+      - "PAPERLESS_DBHOST=paperless-postgres"
+      - "PAPERLESS_DBNAME=paperlessdb"
+      - "PAPERLESS_DBUSER=paperlessdbuser"
+      - "PAPERLESS_DBPASS=paperlessdbpassword"
+      - "PAPERLESS_SECRET_KEY=Mysupersecretpaperless(ngx)key"
+      - "PAPERLESS_FILENAME_FORMAT={{created_year}}/{{correspondent}}/{{created}}_{{title}}"
+      # Set the following two for your first launch
+      # and change the admin password afterwards.
+      # Once setup, you can safely remove these variables.
+      - "PAPERLESS_ADMIN_USER=ADMINUSERNAME"
+      - "PAPERLESS_ADMIN_PASSWORD=ADMINPASSWORD"
+
+  paperless-postgres:
+    container_name: paperless-postgres
+    image: postgres:16.0-alpine #fixedVersion
+    restart: unless-stopped
+    networks:
+      - backend
+    security_opt:
+      - no-new-privileges:true
+    volumes:
+      - postgres-data:/var/lib/postgresql/data
+    environment:
+      POSTGRES_USER: paperlessdbuser
+      POSTGRES_DB: paperlessdb
+      POSTGRES_PASSWORD: paperlessdbpassword
+
+  paperless-redis:
+    container_name: paperless-redis
+    image: redis:7.2-alpine #fixedVersion
+    restart: unless-stopped
+    networks:
+      - backend
+    security_opt:
+      - no-new-privileges:true
+    volumes:
+      - redis-data:/data
+    environment:
+      REDIS_ARGS: "--save 60 10"
+
+volumes:
+  data:
+  export:
+  #media:
+  postgres-data:
+  redis-data:
+
+networks:
+  frontend:
+    name: frontend
+    driver: bridge
+  backend:
+    name: backend
+    driver: bridge
+
+# secrets:
+#   paperless_db_paperless_passwd:
+#     file: ./secrets/paperless_db_paperless_passwd
+#   paperless_secret_key:
+#     file: ./secrets/paperless_secret_key
+#
diff --git a/paperless/scantopaperless.sh b/paperless/scantopaperless.sh
new file mode 100755
index 0000000..9f417b9
--- /dev/null
+++ b/paperless/scantopaperless.sh
@@ -0,0 +1,28 @@
+#!/usr/bin/env bash
+
+# systemctl restart avahi-daemon.service
+
+# DEVICE="pixma:MG5400_BD2FD8000000"
+DEVICE="airscan:w0:CANON INC. MG5400 series"
+
+scanimage -d "$DEVICE" --mode Gray --batch --batch-prompt --format=png --resolution=300
+# scanimage -d "$DEVICE" --mode Gray --batch --format=png --button-controlled=yes --resolution=300
+
+# ensure correct order if we scan more than 9 pages
+# by adding a 0 in front of the early scans
+for i in {1..9}; do
+    if [ -f "out${i}png" ]; then
+        mv "out${i}.png" "out0${i}.png"
+        echo "Renamed out${i}.png to out0${i}.png"
+    fi
+done
+
+# Find any 'out*.png' files. If none are found, exit the program.
+if ! stat -t out*.png >/dev/null 2>&1; then
+    echo "No 'out*.png' files found. Exiting..."
+    exit 1
+fi
+
+magick out*.png out.pdf
+mv out.pdf "$HOME/documents/archive/consume/$(date +'%Y-%m-%dT%H-%M')_scan.pdf"
+rm out*.png