Last active
January 3, 2024 21:58
-
-
Save Jabb0/3179d79a939988c26e6a36c9015c5dcb to your computer and use it in GitHub Desktop.
Automatic Scanning for paperless-ngx with ASN barcode
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -e | |
# Enable ASN barcode detection: https://docs.paperless-ngx.com/configuration/#barcodes | |
# https://margau.net/posts/2023-04-16-paperless-ngx-asn/ | |
if [ "$#" -ne 1 ]; then | |
echo "Usage: $0 <initial_counter>" | |
exit 1 | |
fi | |
initial_counter=$1 | |
output_dir="." | |
base_name="ASN" | |
# Ensure the output directory exists | |
mkdir -p "$output_dir" | |
# Initialize the document counter | |
counter="$initial_counter" | |
while true; do | |
# Create a temporary directory for scanned pages | |
temp_dir=$(mktemp -d) | |
# Scan using the ADF at 300 DPI | |
# scanimage --help --device-name 'brother3:net1;dev0' will get you options | |
scanimage --device-name 'brother3:net1;dev0' --mode "True Gray" --source "Automatic Document Feeder(centrally aligned)" -x 210 -y 297 --resolution 300 --batch="$temp_dir/page_%04d.pnm" | |
# Convert each scanned page to PDF | |
for page_file in "$temp_dir/page_"*.pnm; do | |
page_number=$(basename "$page_file" | sed 's/page_\([0-9]\+\)\.pnm/\1/') | |
convert "$page_file" -page a4 "$temp_dir/${base_name}${counter}_page${page_number}.pdf" | |
done | |
# Generate barcode image (QR code) | |
barcode_text="${base_name}$(printf "%05d" "$counter")" | |
barcode_image="$temp_dir/barcode_${counter}.png" | |
qrencode -o "$barcode_image" "$barcode_text" | |
# Convert QR code to PDF | |
convert "$barcode_image" -page a4 "$temp_dir/${base_name}${counter}_barcode.pdf" | |
# Combine all pages into a single PDF | |
pdf_files=("$temp_dir/${base_name}${counter}_page"*.pdf "$temp_dir/${base_name}${counter}_barcode.pdf" ) | |
pdfunite "${pdf_files[@]}" "$output_dir/${base_name}${counter}.pdf" | |
# Clean up temporary directory | |
rm -r "$temp_dir" | |
# Increment the document counter | |
((counter++)) | |
# Prompt user to add new pages | |
read -p "Place the next set of pages in the ADF and press Enter to continue. If you are done, type 'exit' and press Enter: " userInput | |
# Check if the user wants to exit | |
if [[ "$userInput" == "exit" ]]; then | |
echo "Scanning process completed." | |
exit 0 | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment