forked from rocketraman/sane-scan-pdf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscan
executable file
·305 lines (253 loc) · 8 KB
/
scan
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#!/bin/bash
DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
DEVICE=fujitsu
OUTPUT=scan.pdf
OUTPUTARR=()
USEOUTPUT=0
USEARRAY=0
APPEND=0
RESOLUTION=300
MODE=Lineart
SCRIPT="$DIR/scan_perpage"
DUPLEX=0
UNPAPER=0
SEARCHABLE=0
LANGUAGE=eng
MAXPAGE=
TRUNCPAGE=0
HELP=0
SIZE=
DEFAULTSIZE=1
PGHEIGHT=
PGHEIGHTIN=
PGWIDTH=
PGWIDTHIN=
CROP=0
DESKEW=0
DRIVER_OPTION=
VERBOSE=0
SKIP_EMPTY_PAGES=0
TMP_DIR=$(mktemp -d -p "" scan.XXXXXXXXXX)
cleanup()
{
rm -rf "$TMP_DIR"
}
trap cleanup EXIT
# Parse command-line options
while [[ $# > 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 ;;
--no-default-size) DEFAULTSIZE=0 ;;
--crop) CROP=1 ;;
--deskew) DESKEW=1 ;;
--unpaper) UNPAPER=1 ;;
--searchable|--ocr) SEARCHABLE=1 ;;
--language) shift; LANGUAGE=$1 ;;
--skip-empty-pages) SKIP_EMPTY_PAGES=1 ;;
-o|--output) shift; USEOUTPUT=1; OUTPUT="$1" ;;
-l|--outputlist) shift; USEARRAY=1; OUTPUTARR=(); OUTPUTARR+=("$1") ;;
-x|--device) shift; DEVICE=$1;;
-xo|--driver-options) shift; DRIVER_OPTION=$1;;
*) if [[ $USEARRAY == 1 ]]; then OUTPUTARR+=("$1"); else echo >&2 "Unknown argument: $1"; exit 1; fi ;;
esac
shift # next option
done
if [[ $HELP == 1 ]]; then
echo "$(basename $0) [OPTIONS]... [OUTPUT]"
echo ""
echo "OPTIONS"
echo " -v, --verbose"
echo " Verbose output (this will slow down the scan due to the need to prevent interleaved output)"
echo " -d, --duplex"
echo " Duplex scanning"
echo " -m, --mode"
echo " Mode e.g. Lineart (default), Halftone, Gray, Color, etc."
echo " -r, --resolution"
echo " Resolution e.g 300 (default)"
echo " -a, --append"
echo " Append output to existing scan"
echo " -e, --max <pages>"
echo " Max number of pages e.g. 2 (default is all pages)"
echo " -t, --truncate <pages>"
echo " Truncate number of pages from end e.g. 1 (default is none) -- truncation happens after --skip-empty-pages"
echo " -s, --size"
echo " Page Size as type e.g. Letter (default), Legal, A4, no effect if --crop is specified"
echo " -ph, --page-height"
echo " Custom Page Height in mm"
echo " -pw, --page-width"
echo " Custom Page Width in mm"
echo " -x, --device"
echo " Override scanner device name, defaulting to \"fujitsu\""
echo " -xo, --driver-options"
echo " Send additional options to the scanner driver e.g."
echo " -xo \"--whatever bar --frobnitz baz\""
echo " --no-default-size"
echo " Disable default page size, useful if driver does not support page size/location arguments"
echo " --crop"
echo " Crop to contents (driver must support this)"
echo " --deskew"
echo " Run driver deskew (driver must support this)"
echo " --unpaper"
echo " Run post-processing deskew and black edge detection (requires unpaper)"
echo " --ocr"
echo " Run OCR to make the PDF searchable (requires tesseract)"
echo " --language <lang>"
echo " which language to use for OCR"
echo " --skip-empty-pages"
echo " remove empty pages from resulting PDF document (e.g. one sided doc in duplex mode)"
echo ""
echo "OUTPUT"
echo " -o, --output <outputfile>"
echo " Output to named file default=scan.pdf"
echo " -l, --outputlist <outputfile-1...outputfile-n> Output to named files for each scanned page, can be used with append"
echo ""
exit 0
fi
if [[ $USEARRAY == 1 && $USEOUTPUT == 1 ]]; then
echo >&2 "Use one of -o or -l. Aborting."
exit 1
fi
if [[ $USEOUTPUT == 1 && "$OUTPUT" == "" ]]; then
echo >&2 "Output file must be specified. Aborting."
exit 1
fi
if [[ $USEOUTPUT == 1 && -f "$OUTPUT" && $APPEND != 1 ]]; then
echo >&2 "Output file $OUTPUT already exists. Delete or specify -a. Aborting."
exit 1
fi
if [[ $USEARRAY == 1 && ${#OUTPUTARR[@]} == 0 ]]; then
echo >&2 "At least one file must be specified with -l. Aborting."
exit 1
fi
if [[ $USEARRAY == 1 && $APPEND != 1 ]]; then
for o in "${OUTPUTARR[@]}"; do
if [[ -f "$o" ]]; then
echo >&2 "Output file $o already exists. Delete or specify -a. Aborting."
exit 1
fi
done
fi
if [[ $USEARRAY == 1 ]]; then
OUTPUT=("${OUTPUTARR[@]}")
fi
SOURCE=""
if [[ $DUPLEX == 1 ]]; then
SOURCE="--source \"ADF Duplex\""
fi
if [[ "$MAXPAGE" != "" ]]; then
MAXPAGE="-e $MAXPAGE"
fi
PS2PDF_OPTS=
if [[ $CROP != 1 && $SIZE == "" && $DEFAULTSIZE == 1 ]]; then
# Default to Letter size, but only if crop is not specified and this feature is not disabled
SIZE=Letter
fi
case "$SIZE" in
Letter) PGHEIGHT=279.4; PGWIDTH=215.9 ;;
Legal) PGHEIGHT=355.6; PGWIDTH=215.9 ;;
A4) PGHEIGHT=297; PGWIDTH=210 ;;
esac
if [[ $CROP != 1 && "$PGHEIGHT" != "" ]]; then
PGHEIGHTIN=$(units --compact -1 "$PGHEIGHT mm" 'in')
PGHEIGHT="--page-height $PGHEIGHT -y $PGHEIGHT"
PS2PDF_OPTS="-dEPSCrop"
fi
if [[ $CROP != 1 && "$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 --ald=yes"
# In duplex mode, the driver's buffer for the back side image will be larger than necessary, oh well
# http://sane.10972.n7.nabble.com/Fujitsu-backend-and-iX500-scanning-page-longer-than-14-Inches-td19303.html
PGHEIGHT="--page-height 9999 -y 9999"
PGWIDTH="--page-width 9999 -x 9999"
PS2PDF_OPTS="-dEPSCrop"
fi
if [[ $DESKEW == 1 ]]; then
DESKEW="--swdeskew=yes"
fi
export VERBOSE
export UNPAPER
export SEARCHABLE
export LANGUAGE
export RESOLUTION
export PGWIDTHIN
export PGHEIGHTIN
export PS2PDF_OPTS
export SKIP_EMPTY_PAGES
if [[ $VERBOSE == 1 ]]; then
LOCKFILE=$(mktemp)
trap "cleanup; rm -rf $LOCKFILE" EXIT
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 $DRIVER_OPTION $SOURCE -o $TMP_DIR/scan-%04d
shopt -s extglob nullglob
pdffiles=($TMP_DIR/scan-[0-9]*.pdf)
numscans=${#pdffiles[@]}
if [[ $numscans > 0 ]]; then
echo "Processing $numscans pages"
if [[ $numscans > $TRUNCPAGE && $TRUNCPAGE > 0 ]]; then
for x in ${pdffiles[@]:$numscans-$TRUNCPAGE:$TRUNCPAGE}; do rm "$x"; done;
pdffiles=(${pdffiles[@]:0:$numscans-$TRUNCPAGE})
echo "Truncated $TRUNCPAGE pages"
fi
if [[ $numscans > 1 && $USEARRAY == 1 ]]; then
echo "Naming pdfs based on output list..."
output_count=${#OUTPUT[@]}
index=0
while [[ "$index" < "$output_count" ]]; do
let "scanno = $index + 1"
if [[ -f "${OUTPUT[$index]}" ]]; then
mv "${OUTPUT[$index]}" "${OUTPUT[$index]}.orig"
if [[ $APPEND == 1 ]]; then
pdffiles=()
if [[ -f "${OUTPUT[$index]}.orig" ]]; then
pdffiles+=("${OUTPUT[$index]}.orig")
fi
pdffiles+=($TMP_DIR/scan-*(0)$scanno.pdf)
pdfunite "${pdffiles[@]}" "${OUTPUT[$index]}" && rm $TMP_DIR/scan-*(0)$scanno.pdf
else
mv $TMP_DIR/scan-*(0)$scanno.pdf "${OUTPUT[$index]}"
fi
else
mv $TMP_DIR/scan-*(0)$scanno.pdf "${OUTPUT[$index]}"
fi
let "index = $index + 1"
done
elif [[ $numscans > 1 || $APPEND == 1 ]]; then
echo "Concatenating pdfs..."
if [[ -f "$OUTPUT" ]]; then
mv "$OUTPUT" "${OUTPUT}.orig"
fi
pdffiles=()
if [[ -f "${OUTPUT}.orig" ]]; then
pdffiles+=("${OUTPUT}.orig")
fi
pdffiles+=($TMP_DIR/scan-[0-9]*.pdf)
pdfunite "${pdffiles[@]}" "$OUTPUT" && rm $TMP_DIR/scan-[0-9]*.pdf
else
if [[ $USEARRAY == 1 ]]; then
mv $TMP_DIR/scan-0*.pdf "${OUTPUT[0]}"
else
mv $TMP_DIR/scan-0*.pdf "$OUTPUT"
fi
fi
echo ""
echo "Done."
else
echo "Found no scans."
fi