paperless: init scan2paperless script

This commit is contained in:
teutat3s 2024-04-18 12:51:43 +02:00
parent 298b480680
commit f45fa35e90
Signed by: teutat3s
GPG key ID: 4FA1D3FA524F22C1
4 changed files with 61 additions and 32 deletions

View file

@ -13,33 +13,6 @@ with lib; let
backupDir = "${xdg.dataHome}/PaperlessBackup"; backupDir = "${xdg.dataHome}/PaperlessBackup";
consumptionDir = "/home/${psCfg.user.name}/.local/share/scandir"; consumptionDir = "/home/${psCfg.user.name}/.local/share/scandir";
scan2paperless = with pkgs; writeShellScriptBin "scan2paperless" ''
DEVICE=$1
NUM_PAGES=$2
NAME=$3
if [ -z "''${DEVICE}" ] || [ -z "''${NUM_PAGES}" ] || [ -z "''${NAME}" ]; then
echo "Usage: scan2paperless <device> <num_pages> <name>"
exit 1
fi
tmpDir=$(${coreutils}/bin/mktemp -d)
files=()
for i in $(seq 1 $NUM_PAGES); do
fileName=$(${openssl}/bin/openssl rand -hex 12)
file="$tmpDir/$fileName.jpg"
echo "Start scanning page $i/$NUM_PAGES";
${sane-backends}/bin/scanimage -d $DEVICE --format=jpeg --resolution 300 --progress -o $file
echo "Finished scanning page $i";
files+=($file)
done
pdf="${consumptionDir}/$NAME.pdf"
${python3Packages.img2pdf}/bin/img2pdf --output $pdf ''${files[@]}
echo "PDF written to $pdf"
'';
in { in {
services.paperless = { services.paperless = {
enable = true; enable = true;
@ -60,11 +33,6 @@ in {
#brscan5.enable = true; #brscan5.enable = true;
}; };
users.users."${psCfg.user.name}".packages = with pkgs; [
scan2paperless
python3Packages.img2pdf
];
home-manager.users."${psCfg.user.name}" = { home-manager.users."${psCfg.user.name}" = {
home.sessionVariables = { home.sessionVariables = {
SCANNER_OUTPUT_DIR = consumptionDir; SCANNER_OUTPUT_DIR = consumptionDir;

View file

@ -13,6 +13,7 @@ with prev; {
psos = writeShellScriptBin "psos" (import ./psos.nix final); psos = writeShellScriptBin "psos" (import ./psos.nix final);
psos-docs = import ./psos-docs.nix final; psos-docs = import ./psos-docs.nix final;
s = writeShellScriptBin "s" (import ./s.nix final); s = writeShellScriptBin "s" (import ./s.nix final);
scan2paperless = writeShellScriptBin "scan2paperless" (import ./scan2paperless.nix final);
sway-launcher = writeScriptBin "sway-launcher" (import ./sway-launcher.nix final); sway-launcher = writeScriptBin "sway-launcher" (import ./sway-launcher.nix final);
swaylock-bg = writeShellScriptBin "swaylock-bg" (import ./swaylock-bg.nix final); swaylock-bg = writeShellScriptBin "swaylock-bg" (import ./swaylock-bg.nix final);
toggle-kbd-layout = writeShellScriptBin "toggle-kbd-layout" (import ./toggle-kbd-layout.nix final); toggle-kbd-layout = writeShellScriptBin "toggle-kbd-layout" (import ./toggle-kbd-layout.nix final);

59
pkgs/scan2paperless.nix Normal file
View file

@ -0,0 +1,59 @@
self:
with self; ''
echo "$@"
DUPLEX=,Duplex
removeempty="true"
[[ $1 == "-s" ]] && {
DUPLEX=""
removeempty=false
shift
}
[[ $1 == "-a" ]] && {
DUPLEX=,Duplex
removeempty="true"
shift
}
[[ $1 == "-d" ]] && {
DUPLEX=,Duplex
shift
}
cleanup() {
echo finalizing pdf file.
${img2pdf}/bin/img2pdf --pdfa --output /tmp/scan2paperless_$$.pdf /tmp/scan2paperless_$$_*.png && \
rm -f /tmp/scan2paperless_$$_*.png
scp /tmp/scan2paperless_$$.pdf pub-solar@paperless.faenix.eu:~/.local/share/scandir \
&& rm -f /tmp/scan2paperless_$$* \
|| echo upload failed, retaining file /tmp/scan2paperless_$$.pdf >&2
}
trap 'cleanup; exit 1' EXIT
${sane-backends}/bin/scanimage --device 'brother5:bus1;dev5' --format=png --resolution 300 --batch=/tmp/scan2paperless_$$_%d.png --AutoDeskew=yes --batch-start=10 --source="Automatic Document Feeder(left aligned''${DUPLEX})" -x 210 -y 297
if $removeempty
then
threshold=99
images=( )
values=( )
for f in /tmp/scan2paperless_$$_*.png
do
images[''${#images[@]}]=$f
values[''${#values[@]}]=$(${imagemagick}/bin/convert "$f" -fuzz 02% -fill black +opaque white -fill white -opaque white -format "%[fx:100*mean]" info:)
done
for ((i=0;i<''${#images[@]};i++))
do
if [[ $(echo "''${values[i]} > $threshold" | ${bc}/bin/bc -l) == "1" ]]
then
# bc will output 1 if the comparison is true, 0 otherwise
echo image "''${images[i]}" was found to be mostly white, removing.
rm "''${images[i]}"
fi
done
fi
''

View file

@ -80,6 +80,7 @@ in {
age-plugin-yubikey age-plugin-yubikey
#cockroach-bin #cockroach-bin
obs-studio obs-studio
scan2paperless
wlvncc wlvncc
]; ];
programs.bash.initExtra = '' programs.bash.initExtra = ''