Last active
February 15, 2024 05:33
-
-
Save mamu7211/03e48d9be7d74110fe24aa69793f8ea3 to your computer and use it in GitHub Desktop.
Scan script for scanning multiple documents
This file contains 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 | |
echo "" | |
set -e | |
mkdir -p ./scan_target | |
scanner_name="Canon" | |
target_paths=('/home/$USER/Tools/paperless/paperless-ngx/consume') | |
usb_address=$(lsusb | grep $scanner_name | sed -E "s/Bus ([0-9]+) Device ([0-9]+): .+/\1:\2/g") | |
# Check if there are any files in the 'scan_target' directory | |
if [ $(ls ./scan_target/* 2> /dev/null | wc -l) -gt 0 ]; then | |
while true; do | |
read -p "There are files in the 'scan_target' directory. Do you want to delete them? (y/n): " answer | |
if [[ $answer == "y" || $answer == "n" ]]; then | |
break | |
else | |
echo "Invalid input. Please enter 'y' or 'n'." | |
fi | |
done | |
if [ "$answer" == "y" ]; then | |
rm ./scan_target/*.tiff | |
rm ./scan_target/*.pdf | |
else | |
echo "Program aborted." | |
exit 1 | |
fi | |
fi | |
# Get filename from user | |
while true; do | |
read -p "Enter filename (without extension): " filename | |
if [[ $filename =~ ^[a-zA-Z0-9-]+$ ]]; then | |
break | |
else | |
echo "Invalid filename. Please use only alphanumeric characters and hyphens." | |
fi | |
done | |
timestamp=$(date +"%Y-%m-%d-%H-%M-%S") | |
filename_with_timestamp="${timestamp}-${filename}" | |
pdf_final_filename=${filename_with_timestamp}.pdf | |
# Scan function to scan multiple documents | |
scan() { | |
local number=1 | |
while true; do | |
tiff_filename="./scan_target/${filename_with_timestamp}-temp-${number}.tiff" | |
pdf_filename="./scan_target/${filename_with_timestamp}-temp-${number}.pdf" | |
# (Implementation details for scanning are not provided) | |
# Assume scanning code here, which creates a PDF in 'scan_target' | |
# For simplicity, touch command is used to create an empty PDF file | |
scanimage -d "genesys:libusb:$usb_address" --resolution 300 --brightness 25 --mode Color --format tiff > $tiff_filename | |
tiff2pdf -o $pdf_filename $tiff_filename | |
while true; do | |
read -p "Do you want to scan another document? (y/n): " answer | |
if [[ $answer == "y" || $answer == "n" ]]; then | |
break | |
else | |
echo "Invalid input. Please enter 'y' or 'n'." | |
fi | |
done | |
if [ "$answer" != "y" ]; then | |
break | |
fi | |
((number++)) | |
done | |
} | |
# Combine scanned PDFs into one document | |
combine_pdf() { | |
num_tiffs=$(ls ./scan_target/*.tiff 2> /dev/null | wc -l) | |
echo "Combining ${num_tiffs} into ${pdf_final_filename}" | |
pdfunite $(ls -tr ./scan_target/*.pdf) $pdf_final_filename | |
} | |
# Finalize by moving the PDF to the target path and remove any temp files | |
finalize() { | |
for target_path in "${target_paths[@]}"; do | |
echo "Copy ${pdf_final_filename} to ${target_path}" | |
cp "${pdf_final_filename}" "${target_path}" | |
done | |
echo "Removing temp files." | |
rm ./scan_target/*-temp-*.pdf | |
rm ./scan_target/*-temp-*.tiff | |
echo "Done." | |
} | |
# Just do it... | |
scan | |
combine_pdf | |
finalize |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment