# # Declare variables # DEVICE="${SANE_DEFAULT_DEVICE:-}" OUTPUT_FILE= while [[ $# -gt 0 ]]; do case $1 in -d | --device) # Allows user to override the device used, since scanimage is # slow and the printer might not even change between runs DEVICE="$2" shift shift ;; -*) echo "Unknown option $1" exit 1 ;; *) OUTPUT_FILE="$1" shift ;; esac done # # Handle defaults # if [ -z "$OUTPUT_FILE" ]; then OUTPUT_FILE=./scan_"$(date)".pdf fi if [ -z "$DEVICE" ]; then DEVICE=$(scanimage --formatted-device-list="%d (%v %m)" --list-devices | fzf --header "Pick a device:" | cut -d ' ' -f 1) fi tempdir="$(mktemp -d)" do_scan() { filename="$tempdir/$(date).pdf" while ! scanimage --format=pdf --device="$DEVICE" --resolution 300 >"$filename"; do : done echo "$filename" } do_fix() { read -r filename fixed_name="${filename%.pdf}_fixed.pdf" gs -o "$fixed_name" -sDEVICE=pdfwrite -dPDFSETTINGS=/prepress "$filename" rm "$filename" mv "$fixed_name" "$filename" echo "$filename" } while :; do do_scan | do_fix IFS= read -r -p "Continue scanning ? [Y/n] " cont case "$cont" in [nN]) echo "Exiting..." break ;; [yY] | *) ;; esac done # Multiple files are scanned, join them merged_filename="$tempdir/$(date)_final.pdf" pdfunite "$tempdir"/*.pdf "$merged_filename" echo "$merged_filename" | do_fix # Copy scan to current directory cp "$merged_filename" "$OUTPUT_FILE" # Make sure I don't remove things other than pdf rm "$tempdir/"*.pdf rm -d "$tempdir"