mirror of
https://github.com/rocketraman/sane-scan-pdf.git
synced 2025-05-16 23:50:39 -07:00
233 lines
5.8 KiB
Bash
Executable File
233 lines
5.8 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
|
|
|
|
DEVICE=fujitsu
|
|
OUTPUT=scan.pdf
|
|
USEARRAY=0
|
|
APPEND=0
|
|
RESOLUTION=300
|
|
MODE=Lineart
|
|
SCRIPT="$DIR/scan_perpage"
|
|
DUPLEX=0
|
|
UNPAPER=0
|
|
SEARCHABLE=0
|
|
MAXPAGE=
|
|
TRUNCPAGE=0
|
|
HELP=0
|
|
SIZE=Letter
|
|
PGHEIGHT=
|
|
PGHEIGHTIN=
|
|
PGWIDTH=
|
|
PGWIDTHIN=
|
|
CROP=0
|
|
DESKEW=0
|
|
VERBOSE=0
|
|
|
|
# Parse command-line options
|
|
while [ $# -gt 0 ]; do
|
|
case "$1" in
|
|
|
|
-v|--verbose) VERBOSE=1 ;;
|
|
|
|
-d|--duplex) DUPLEX=1 ;;
|
|
|
|
-m|--mode) shift; MODE=$1 ;;
|
|
|
|
-r|--resolution) shift; RESOLUTION=$1 ;;
|
|
|
|
-a|--append) APPEND=1 ;;
|
|
|
|
-e|--max) shift; MAXPAGE=$1 ;;
|
|
|
|
-t|--truncate) shift; TRUNCPAGE=$1 ;;
|
|
|
|
-h|--help) HELP=1 ;;
|
|
|
|
-s|--size) shift; SIZE=$1 ;;
|
|
|
|
-ph|--page-height) shift; PGHEIGHT=$1 ;;
|
|
|
|
-pw|--page-width) shift; PGWIDTH=$1 ;;
|
|
|
|
--crop) CROP=1 ;;
|
|
|
|
--deskew) DESKEW=1 ;;
|
|
|
|
--unpaper) UNPAPER=1 ;;
|
|
|
|
--searchable|--ocr) SEARCHABLE=1 ;;
|
|
|
|
-o|--output) shift; OUTPUT="$1" ;;
|
|
|
|
-l|--outputlist) shift; USEARRAY=1; OUTPUT=(); OUTPUT+=("$1") ;;
|
|
|
|
*) if [ $USEARRAY = 1 ]; then OUTPUT+=("$1"); else echo >&2 "Unknown argument: $1"; exit 1; fi ;;
|
|
|
|
esac
|
|
shift # next option
|
|
done
|
|
|
|
|
|
if [ $HELP -eq 1 ]; then
|
|
echo "$(basename $0) [-v|--verbose] [-d|--duplex] [-m|--mode] [-r|--resolution] [-a|--append] [-e|--max <pages>] [-t|--truncate <pages>] [-s|--size | [-ph|--page-height] [-pw|--page-width]] [--crop] [--deskew] [--unpaper] [--ocr] [-o|--output <outputfile>]"
|
|
echo " -v Verbose output (this will slow down the scan due to the need to prevent interleaved output)"
|
|
echo " -d Duplex scanning"
|
|
echo " -m Mode e.g. Lineart (default), Halftone, Gray, Color, etc."
|
|
echo " -r Resolution e.g 300 (default)"
|
|
echo " -a Append output to existing scan"
|
|
echo " -e Max number of pages e.g. 2 (default is all pages)"
|
|
echo " -t Truncate number of pages from end e.g. 1 (default is none)"
|
|
echo " -s Page Size as type e.g. Letter (default), Legal, A4"
|
|
echo " -ph Custom Page Height in mm"
|
|
echo " -pw Custom Page Width in mm"
|
|
echo " --crop Run driver deskew (driver must support this)"
|
|
echo " --deskew Run driver deskew (driver must support this)"
|
|
echo " --unpaper Run deskew and black edge detection (requires unpaper)"
|
|
echo " --ocr Run OCR to make the PDF searchable (requires tesseract)"
|
|
echo " -o Output to named file default=scan.pdf"
|
|
echo ""
|
|
exit 0
|
|
fi
|
|
|
|
if [ $USEARRAY = 0 -a -f "$OUTPUT" -a ! $APPEND = 1 ]; then
|
|
echo >&2 "Output file $OUTPUT already exists. Delete or specify -a. Aborting."
|
|
exit 1
|
|
fi
|
|
|
|
if [ $USEARRAY = 1 -a ! $APPEND = 1 ]; then
|
|
for o in "${OUTPUT[@]}"; do
|
|
if [ -f "$o" ]; then
|
|
echo >&2 "Output file $o already exists. Delete or specify -a. Aborting."
|
|
exit 1
|
|
fi
|
|
done
|
|
fi
|
|
|
|
SOURCE=""
|
|
if [ $DUPLEX -eq 1 ]; then
|
|
SOURCE="--source \"ADF Duplex\""
|
|
fi
|
|
|
|
if [ "$MAXPAGE" != "" ]; then
|
|
MAXPAGE="-e $MAXPAGE"
|
|
fi
|
|
|
|
PS2PDF_OPTS=
|
|
|
|
# Specify non-letter sizes in mm
|
|
case "$SIZE" in
|
|
|
|
Letter) PGHEIGHTIN=11; PGWIDTHIN=8.5 ;;
|
|
|
|
Legal) PGHEIGHT=355.6; PGWIDTH=215.9 ;;
|
|
|
|
A4) PGHEIGHT=297; PGWIDTH=210 ;;
|
|
|
|
esac
|
|
|
|
if [ $CROP != 1 -a "$PGHEIGHT" != "" ]; then
|
|
PGHEIGHTIN=$(units --compact -1 "$PGHEIGHT mm" 'in')
|
|
PGHEIGHT="--page-height $PGHEIGHT -y $PGHEIGHT"
|
|
PS2PDF_OPTS="-dEPSCrop"
|
|
fi
|
|
|
|
if [ $CROP != 1 -a "$PGWIDTH" != "" ]; then
|
|
PGWIDTHIN=$(units --compact -1 "$PGWIDTH mm" 'in')
|
|
PGWIDTH="--page-width $PGWIDTH -x $PGWIDTH"
|
|
PS2PDF_OPTS="-dEPSCrop"
|
|
fi
|
|
|
|
if [ $CROP = 1 ]; then
|
|
CROP="--swcrop=yes"
|
|
PGHEIGHT=
|
|
PGWIDTH=
|
|
PGHEIGHTIN=
|
|
PGWIDTHIN=
|
|
PS2PDF_OPTS="-dEPSCrop"
|
|
fi
|
|
|
|
if [ $DESKEW = 1 ]; then
|
|
DESKEW="--swdeskew=yes"
|
|
fi
|
|
|
|
export VERBOSE
|
|
export UNPAPER
|
|
export SEARCHABLE
|
|
export RESOLUTION
|
|
export PGWIDTHIN
|
|
export PGHEIGHTIN
|
|
export PS2PDF_OPTS
|
|
|
|
if [ $VERBOSE = 1 ]; then
|
|
LOCKFILE=$(mktemp)
|
|
trap "rm -rf $LOCKFILE" 0
|
|
export LOCKFILE
|
|
fi;
|
|
|
|
echo >&2 "Scanning..."
|
|
#eval strace -f -o /tmp/scan-trace.txt scanadf -d $DEVICE $MAXPAGE $PGHEIGHT $PGWIDTH -S $SCRIPT --script-wait --resolution $RESOLUTION --mode $MODE $DESKEW $CROP $SOURCE -o scan-%04d
|
|
eval scanadf -d $DEVICE $MAXPAGE $PGHEIGHT $PGWIDTH -S $SCRIPT --script-wait --resolution $RESOLUTION --mode $MODE $DESKEW $CROP $SOURCE -o scan-%04d
|
|
|
|
shopt -s extglob nullglob
|
|
numscans=$(ls scan-[0-9]*.pdf | wc -w)
|
|
if [ $numscans -gt 0 ]; then
|
|
echo ""
|
|
echo "Processing $numscans pages"
|
|
if [ $TRUNCPAGE -gt 0 ]; then
|
|
source /usr/local/bin/stack.sh
|
|
for x in scan-[0-9]*; do push $x; done;
|
|
for x in $(seq $TRUNCPAGE); do rm $(pop); done;
|
|
echo "Truncated $TRUNCPAGE pages"
|
|
fi
|
|
if [ $numscans -gt 1 -a $USEARRAY = 1 ]; then
|
|
echo "Naming pdfs based on output list..."
|
|
output_count=${#OUTPUT[@]}
|
|
index=0
|
|
while [ "$index" -lt "$output_count" ]; do
|
|
let "scanno = $index + 1"
|
|
echo "scanno = $scanno"
|
|
if [ -f "${OUTPUT[$index]}" ]; then
|
|
mv "${OUTPUT[$index]}" "${OUTPUT[$index]}.orig"
|
|
if [ $APPEND -eq 1 ]; then
|
|
pdffiles=()
|
|
if [ -f "${OUTPUT[$index]}.orig" ]; then
|
|
pdffiles+=("${OUTPUT[$index]}.orig")
|
|
fi
|
|
echo "index = $index"
|
|
echo "scanno = $scanno"
|
|
pdffiles+=(scan-*(0)$scanno.pdf)
|
|
pdfunite "${pdffiles[@]}" "${OUTPUT[$index]}" && rm scan-*(0)$scanno.pdf
|
|
else
|
|
mv scan-*(0)$scanno.pdf "${OUTPUT[$index]}"
|
|
fi
|
|
else
|
|
mv scan-*(0)$scanno.pdf "${OUTPUT[$index]}"
|
|
fi
|
|
let "index = $index + 1"
|
|
done
|
|
elif [ $numscans -gt 1 -o $APPEND -eq 1 ]; then
|
|
echo "Concatenating pdfs..."
|
|
if [ -f "$OUTPUT" ]; then
|
|
mv "$OUTPUT" "${OUTPUT}.orig"
|
|
fi
|
|
pdffiles=()
|
|
if [ -f "${OUTPUT}.orig" ]; then
|
|
pdffiles+=("${OUTPUT}.orig")
|
|
fi
|
|
shopt -s nullglob
|
|
pdffiles+=(scan-[0-9]*.pdf)
|
|
pdfunite "${pdffiles[@]}" "$OUTPUT" && rm scan-[0-9]*.pdf
|
|
else
|
|
if [ $USEARRAY = 1 ]; then
|
|
mv scan-0*.pdf "${OUTPUT[0]}"
|
|
else
|
|
mv scan-0*.pdf "$OUTPUT"
|
|
fi
|
|
fi
|
|
echo ""
|
|
echo "Done."
|
|
else
|
|
echo "Found no scans."
|
|
fi
|