forked from zhangtemplar/ricoh-sp100
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pstoricohddst-gdi
executable file
·207 lines (171 loc) · 4.58 KB
/
pstoricohddst-gdi
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
#!/bin/bash
# Debug mode: change to 'yes' to enable
DEBUG=no
function log() {
[ "${DEBUG}" = "yes" ] && echo $* | logger -t "$0[$$]"
}
function logpipe() {
[ "${DEBUG}" = "yes" ] && echo $* | logger -t "$0[$$]"
}
function trapINT() {
log "trapINT()"
[ "x$trp" = "x" ] && trp="yes" || return
sleep 30 && { [ ! "${DEBUG}" = "yes" ] && rm -rf $uid; log "Cleanup complete"; } &
exit
}
function stop() {
log "Stop stop page";
echo "stop" > $uid/999999999-page.pbm
}
log "Called with cmdline: $0 $*"
trap "stop; trapINT" SIGINT SIGTERM SIGQUIT
#trap 'echo No' SIGINT SIGTERM SIGQUIT EXIT;
# Username
user="$2"
# Page title (not used at this time, "Document" instead)
ptitle="$3"
my="$0"
options="$5"
pagesize="A4"
resolution="600"
mediasource="TRAY1"
for opt in $options; do
case "$opt" in
PageSize=*)
pagesize="$(echo "${opt#PageSize=}" | tr a-z A-Z)"
;;
Resolution=*)
resolution=${opt#Resolution=}
resolution=${resolution%dpi}
;;
InputSlot=*)
mediasource=${opt#InputSlot=}
;;
esac
done
# MS-style EOL
e=$(echo -en "\r")
# Printing date
ddate="`LC_ALL=en_US.UTF-8 date '+%Y/%m/%d %H:%M:%S'`"
# Temporary directory
uid="/tmp/pstoricohddst-gdi-`uuidgen`"
mkdir -p $uid
[ "${DEBUG}" = "yes" ] && exec >$uid/output.stream #> >(tee $uid/output.stream)
[ -x "$(which inotifywait)" ] && {
log "Asynchronous variant"
(
stage="empty"
inotifywait -q -m -r -e close_write --format '%f' $uid | grep --line-buffered 'page.pbm$' | while read page; do
log "Page submitted"
[ "$stage" = "empty" ] && {
log "1st stage. Flushing PJL header"
cat <<EOF
%-12345X@PJL$e
@PJL SET TIMESTAMP=$ddate$e
@PJL SET FILENAME=Document$e
@PJL SET COMPRESS=JBIG$e
@PJL SET USERNAME=$user$e
@PJL SET COVER=OFF$e
@PJL SET HOLD=OFF$e
EOF
stage="printing"
}
[ "$page" = "999999999-page.pbm" ] && {
log "Last stage. Flushing PJL footer"
cat <<EOF
@PJL EOJ$e
%-12345X
EOF
pid=`ps ax | grep $uid | grep -v grep | awk ' { print $1 } '`
[ ! "x$pid" = "x" ] && kill $pid
break
}
[ "$stage" = "printing" ] && {
# Converting page to JBIG format (parameters are very special for this printer!)
pbmtojbg -p 72 -o 3 -m 0 -q < $uid/$page > $uid/raster.jbig
# Taking image size
jsize=`wc -c < $uid/raster.jbig`
# Taking image dimensions
read fn ft xs ys garb < <(identify $uid/$page | tr "x" " ")
# Flushing page header
cat <<EOF
@PJL SET PAGESTATUS=START$e
@PJL SET COPIES=1$e
@PJL SET MEDIASOURCE=$mediasource$e
@PJL SET MEDIATYPE=PLAINRECYCLE$e
@PJL SET PAPER=$pagesize$e
@PJL SET PAPERWIDTH=$xs$e
@PJL SET PAPERLENGTH=$ys$e
@PJL SET RESOLUTION=${resolution%x600}$e
@PJL SET IMAGELEN=$jsize$e
EOF
log "Flushing image $page"
cat $uid/raster.jbig
# Flushing page footer
# TODO: pixelcount for toner estimate
cat <<EOF
@PJL SET DOTCOUNT=1132782$e
@PJL SET PAGESTATUS=END$e
EOF
}
done
) &
# Converting from PostScript to PostScript-monochrome, then to PBM image format (per page)
#gs -sDEVICE=ps2write -sOutputFile=- -r$resolution -dQUIET -dBATCH -dNOPAUSE - |
gs -sDEVICE=pbmraw -sOutputFile=${uid}/%03d-page.pbm -r$resolution -dQUIET -dBATCH -dNOPAUSE -
stop
wait
trapINT
} || {
log "Synchronous variant"
# Converting from PostScript to PostScript-monochrome, then to PBM image format (per page)
log "Converting document to pages"
gs -sDEVICE=ps2write -sOutputFile=- -r$resolution -dQUIET -dBATCH -dNOPAUSE - | gs -sDEVICE=pbmraw -sOutputFile=${uid}/%03d-page.pbm -r$resolution -dQUIET -dBATCH -dNOPAUSE -
log "Conversion complete"
cat <<EOF
%-12345X@PJL$e
@PJL SET TIMESTAMP=$ddate$e
@PJL SET FILENAME=Document$e
@PJL SET COMPRESS=JBIG$e
@PJL SET USERNAME=$user$e
@PJL SET COVER=OFF$e
@PJL SET HOLD=OFF$e
EOF
for page in ${uid}/*-page.pbm; do
log "Page $page"
# Converting page to JBIG format (parameters are very special for this printer!)
pbmtojbg -p 72 -o 3 -m 0 -q < $page > $uid/raster.jbig
# Taking image size
jsize=`wc -c < $uid/raster.jbig`
# Taking image dimensions
read fn ft xs ys garb < <(identify $page | tr "x" " ")
log "Identified as ${xs}x${ys}"
# Flushing page header
cat <<EOF
@PJL SET PAGESTATUS=START$e
@PJL SET COPIES=1$e
@PJL SET MEDIASOURCE=$mediasource$e
@PJL SET MEDIATYPE=PLAINRECYCLE$e
@PJL SET PAPER=$pagesize$e
@PJL SET PAPERWIDTH=$xs$e
@PJL SET PAPERLENGTH=$ys$e
@PJL SET RESOLUTION=${resolution%x600}$e
@PJL SET IMAGELEN=$jsize$e
EOF
# Flushing image
cat $uid/raster.jbig
# Flushing page footer
# TODO: pixelcount for toner estimate
cat <<EOF
@PJL SET DOTCOUNT=1132782$e
@PJL SET PAGESTATUS=END$e
EOF
done
# Flushing PJL footer
cat <<EOF
@PJL EOJ$e
%-12345X
EOF
}
#rm -rf $uid;
exit 0