-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscan2pdf.sh
More file actions
executable file
·143 lines (129 loc) · 4.81 KB
/
Copy pathscan2pdf.sh
File metadata and controls
executable file
·143 lines (129 loc) · 4.81 KB
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
#!/bin/bash
# scan2pdf.sh: Scan Key Daemon scan-to-PDF script example
# Copyright (C) 2016 Frank Abelbeck <frank.abelbeck@googlemail.com>
#
# This file is part of the brscankeyd program "brscankeyd.py".
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>."""
#
# usage: scan2pdf.sh devicename [odd|even] [reversed]
#
# perform various sanity checks
#
if [ $# -eq 0 ]; then
cat <<ENDOFMSG
Need at least one argument (SANE device address)!
usage: scan2pdf.sh SANEADDRESS [odd|even] [reversed]
If "odd" or "even" is given, this script assumes it will scan odd or even
pages; odd pages are saved as xyz-odd.pdf, even pages are merged with
previously scanned odd pages.
If "reversed" is given, this script assumes the scanned pages to be in
reversed order (paper stack scanned back to front); this argument is
only processd when both even and odd pages were scanned (prior to
merging both) and can be used to simplify double-sided scanning (just
flip stack and scan again).
ENDOFMSG
exit 1
fi
if [ ! -e "/usr/bin/convert" ]; then
echo "Could not find convert (part of imagemagick)!"
exit 1
fi
if [ ! -e "/usr/bin/scanimage" ]; then
echo "Could not find scanimage (part of sane-backends)!"
exit 1
fi
if [ ! -e "/usr/bin/pdftk" ]; then
echo "Could not find pdftk!"
exit 1
fi
#
# security precaution: change into tmp
#
pushd /tmp
#
# create unique filename from date and if necessary from an index
#
if [[ "$2" == "odd" || "$2" == "even" ]]; then
SUFFIX="-$2" # insert "odd" or "even" into the filename
else
SUFFIX=""
fi
# target directory for scans (created automatically if it does not exist):
# - if SCANDIR is set in the environment (e.g. from "scan dir" in the .ini, or
# exported manually), that wins;
# - otherwise default to a "Scans" folder inside the user's XDG Pictures dir
# (localised, e.g. ~/Afbeeldingen on a Dutch system), falling back to
# ~/Pictures if the xdg-user-dir helper is unavailable.
if [ -z "$SCANDIR" ]; then
if command -v xdg-user-dir >/dev/null 2>&1; then
SCANDIR="$(xdg-user-dir PICTURES)/Scans"
else
SCANDIR="$HOME/Pictures/Scans"
fi
fi
/bin/mkdir -p "$SCANDIR"
BASEDIR="$SCANDIR/"
BASENAME="$BASEDIR$(/bin/date +%Y-%m-%d)_scan"
FILENAME="$BASENAME$SUFFIX.pdf"
N=-1
while [ -e "$FILENAME" ]; do
# filename already exists: increment counter, try again
let "N++"
FILENAME="$BASENAME$SUFFIX"_"$N".pdf
done
# perform the scan:
# (1) create a temporary filename
# (2) batch-scan all pages from the ADF using scanimage's batch mode
# (replaces the no-longer-packaged scanadf; export SCANSOURCE to use a
# different source, e.g. "FlatBed" for single-page glass scans)
# (3) convert all scanned images to one PDF
# (4) remove images
IMGNAME="$(/usr/bin/mktemp -u)"
SCANSOURCE="${SCANSOURCE:-Automatic Document Feeder(left aligned)}"
/usr/bin/scanimage --device-name="$1" --batch="$IMGNAME-%02d.pnm" --mode="24bit Color[Fast]" --resolution="300" --source="$SCANSOURCE" 2>&1
/usr/bin/convert "$IMGNAME"*.pnm -trim -compress jpeg -quality 92 -page A4 "$FILENAME"
# remove all scanned images
/bin/rm "$IMGNAME"*
# (5) if either odd or even is set: merge with newest even or odd file
if [ -n "$SUFFIX" ]; then
# either even or odd set: merge the latest odd/even files into a normal pdf;
# if one of these files does not exist, this might be the first half of
# duplex scanning, so do nothing.
ODDFILE="$(/bin/ls -t1 "$BASEDIR"*-odd.pdf 2>/dev/null | head -1)" # get latest odd file, ignore errors
EVENFILE="$(/bin/ls -t1 "$BASEDIR"*-even.pdf 2>/dev/null | head -1)" # get latest even file, ignore errors
if [ -n "$ODDFILE" -a -n "$EVENFILE" ]; then
# odd and even file found
# create unique target name
FILENAME="$BASENAME.pdf"
N=-1
while [ -e "$FILENAME" ]; do
# filename already exists: increment counter, try again
let "N++"
FILENAME="$BASENAME"_$N.pdf
done
# shuffle odd and even into one document
# take into account reversing the staple?
if [[ "$3" == "reversed" ]]; then
/usr/bin/pdftk A="$ODDFILE" B="$EVENFILE" shuffle A Bend-1 output "$FILENAME"
else
/usr/bin/pdftk A="$ODDFILE" B="$EVENFILE" shuffle A B output "$FILENAME"
fi
# remove odd and even file: now replaced by merged file
/bin/rm "$EVENFILE"
/bin/rm "$ODDFILE"
fi
fi
# return from tmp
popd