2013-09-06 17:05:03 +02:00
|
|
|
|
#!/bin/bash
|
|
|
|
|
|
2020-08-30 21:50:45 +02:00
|
|
|
|
#Next four lines must be uncommented in MacOS
|
|
|
|
|
#GS=/opt/local/bin/gs
|
|
|
|
|
#PBMTOJBG=/opt/local/bin/pbmtojbg
|
|
|
|
|
#IDENTIFY=/opt/local/bin/identify
|
|
|
|
|
#uid="/var/spool/cups/tmp/pstoricohddst-gdi-`uuidgen`"
|
|
|
|
|
|
|
|
|
|
# Next four lines must be uncommented in Linux
|
|
|
|
|
GS=gs
|
|
|
|
|
PBMTOJBG=pbmtojbg
|
|
|
|
|
IDENTIFY=identify
|
|
|
|
|
uid="/tmp/pstoricohddst-gdi-`uuidgen`"
|
|
|
|
|
|
2014-04-27 11:25:34 +02:00
|
|
|
|
# Debug mode: change to 'yes' to enable
|
2020-08-30 21:50:45 +02:00
|
|
|
|
DEBUG=${DEBUG+no}
|
2014-04-27 11:25:34 +02:00
|
|
|
|
|
|
|
|
|
function log() {
|
|
|
|
|
[ "${DEBUG}" = "yes" ] && echo $* | logger -t "$0[$$]"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function logpipe() {
|
|
|
|
|
[ "${DEBUG}" = "yes" ] && echo $* | logger -t "$0[$$]"
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-08 11:39:31 +02:00
|
|
|
|
function trapINT() {
|
2014-04-27 11:25:34 +02:00
|
|
|
|
log "trapINT()"
|
2013-09-08 12:18:22 +02:00
|
|
|
|
[ "x$trp" = "x" ] && trp="yes" || return
|
2014-04-27 11:25:34 +02:00
|
|
|
|
sleep 30 && { [ ! "${DEBUG}" = "yes" ] && rm -rf $uid; log "Cleanup complete"; } &
|
2013-09-08 11:39:31 +02:00
|
|
|
|
exit
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function stop() {
|
2014-04-27 11:25:34 +02:00
|
|
|
|
log "Stop stop page";
|
2020-08-30 21:50:45 +02:00
|
|
|
|
echo "stop" > $uid/999999999.page.pbm
|
2013-09-08 11:39:31 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-30 21:50:45 +02:00
|
|
|
|
function pageOutput() {
|
|
|
|
|
|
|
|
|
|
# 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 | awk ' { printf($1); }' `
|
|
|
|
|
|
|
|
|
|
# Taking image dimensions
|
|
|
|
|
#read fn ft xs ys garb < <($IDENTIFY $uid/$page | tr "x" " ") #Not work under MacOS
|
|
|
|
|
ident=`$IDENTIFY $uid/$page | tr "x" " "`
|
|
|
|
|
xs=`echo $ident | awk ' { print $3 } '`
|
|
|
|
|
ys=`echo $ident | awk ' { print $4 } '`
|
|
|
|
|
log "Identified as ${xs}x${ys}"
|
|
|
|
|
|
|
|
|
|
# Flushing page header
|
|
|
|
|
cat <<EOF
|
2018-09-19 23:11:02 +02:00
|
|
|
|
@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
|
2020-08-30 21:50:45 +02:00
|
|
|
|
@PJL SET PAPERWIDTH=$xs$e
|
|
|
|
|
@PJL SET PAPERLENGTH=$ys$e
|
2018-09-19 23:11:02 +02:00
|
|
|
|
@PJL SET RESOLUTION=${resolution%x600}$e
|
|
|
|
|
@PJL SET IMAGELEN=$jsize$e
|
|
|
|
|
EOF
|
|
|
|
|
|
2020-08-30 21:50:45 +02:00
|
|
|
|
# Flushing image
|
|
|
|
|
cat $uid/raster.jbig
|
2018-09-19 23:11:02 +02:00
|
|
|
|
|
2020-08-30 21:50:45 +02:00
|
|
|
|
# Flushing page footer
|
|
|
|
|
# TODO: pixelcount for toner estimate
|
|
|
|
|
cat <<EOF
|
|
|
|
|
@PJL SET DOTCOUNT=1132782$e
|
2018-09-19 23:11:02 +02:00
|
|
|
|
@PJL SET PAGESTATUS=END$e
|
|
|
|
|
EOF
|
2020-08-30 21:50:45 +02:00
|
|
|
|
|
|
|
|
|
#end of pageOutput
|
2018-09-19 23:11:02 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-30 21:50:45 +02:00
|
|
|
|
function pjlHeader() {
|
|
|
|
|
cat <<EOF
|
2018-09-19 23:11:02 +02:00
|
|
|
|
%-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
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-30 21:50:45 +02:00
|
|
|
|
function pjlFooter() {
|
|
|
|
|
cat <<EOF
|
2018-09-19 23:11:02 +02:00
|
|
|
|
@PJL EOJ$e
|
2020-08-30 21:50:45 +02:00
|
|
|
|
%-12345X$e
|
2018-09-19 23:11:02 +02:00
|
|
|
|
EOF
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-27 11:25:34 +02:00
|
|
|
|
log "Called with cmdline: $0 $*"
|
2013-09-08 12:18:22 +02:00
|
|
|
|
|
2013-09-08 11:39:31 +02:00
|
|
|
|
trap "stop; trapINT" SIGINT SIGTERM SIGQUIT
|
|
|
|
|
#trap 'echo No' SIGINT SIGTERM SIGQUIT EXIT;
|
2013-09-06 17:29:49 +02:00
|
|
|
|
# Username
|
2013-09-06 17:05:03 +02:00
|
|
|
|
user="$2"
|
2013-09-06 17:29:49 +02:00
|
|
|
|
|
|
|
|
|
# Page title (not used at this time, "Document" instead)
|
2013-09-06 17:05:03 +02:00
|
|
|
|
ptitle="$3"
|
2013-09-08 11:39:31 +02:00
|
|
|
|
my="$0"
|
2014-07-01 05:37:48 +02:00
|
|
|
|
options="$5"
|
|
|
|
|
|
|
|
|
|
pagesize="A4"
|
2014-07-23 19:43:52 +02:00
|
|
|
|
resolution="600"
|
2014-07-23 21:11:35 +02:00
|
|
|
|
mediasource="TRAY1"
|
2014-07-01 05:37:48 +02:00
|
|
|
|
for opt in $options; do
|
|
|
|
|
case "$opt" in
|
|
|
|
|
PageSize=*)
|
|
|
|
|
pagesize="$(echo "${opt#PageSize=}" | tr a-z A-Z)"
|
|
|
|
|
;;
|
2014-07-23 19:43:52 +02:00
|
|
|
|
Resolution=*)
|
|
|
|
|
resolution=${opt#Resolution=}
|
|
|
|
|
resolution=${resolution%dpi}
|
2014-07-23 21:11:35 +02:00
|
|
|
|
;;
|
|
|
|
|
InputSlot=*)
|
|
|
|
|
mediasource=${opt#InputSlot=}
|
|
|
|
|
;;
|
2014-07-01 05:37:48 +02:00
|
|
|
|
esac
|
|
|
|
|
done
|
2013-09-06 17:29:49 +02:00
|
|
|
|
|
2014-05-07 07:16:14 +02:00
|
|
|
|
# MS-style EOL
|
|
|
|
|
e=$(echo -en "\r")
|
|
|
|
|
|
2013-09-06 17:29:49 +02:00
|
|
|
|
# Printing date
|
2013-09-06 17:05:03 +02:00
|
|
|
|
ddate="`LC_ALL=en_US.UTF-8 date '+%Y/%m/%d %H:%M:%S'`"
|
|
|
|
|
|
2013-09-06 17:29:49 +02:00
|
|
|
|
# Temporary directory
|
|
|
|
|
uid="/tmp/pstoricohddst-gdi-`uuidgen`"
|
2013-09-06 17:05:03 +02:00
|
|
|
|
mkdir -p $uid
|
2020-08-30 21:50:45 +02:00
|
|
|
|
[ "${DEBUG}" = "yes" ] && exec >$uid/output.stream #> >(tee $uid/output.stream) #does not work under MacOS
|
2013-09-06 17:05:03 +02:00
|
|
|
|
|
2020-08-30 21:50:45 +02:00
|
|
|
|
if [ -x "$(which inotifywait)" ]; then {
|
2014-04-27 11:25:34 +02:00
|
|
|
|
log "Asynchronous variant"
|
2014-03-21 10:05:09 +01:00
|
|
|
|
|
|
|
|
|
(
|
|
|
|
|
stage="empty"
|
|
|
|
|
inotifywait -q -m -r -e close_write --format '%f' $uid | grep --line-buffered 'page.pbm$' | while read page; do
|
2014-04-27 11:25:34 +02:00
|
|
|
|
log "Page submitted"
|
2014-03-21 10:05:09 +01:00
|
|
|
|
[ "$stage" = "empty" ] && {
|
2014-04-27 11:25:34 +02:00
|
|
|
|
log "1st stage. Flushing PJL header"
|
2020-08-30 21:50:45 +02:00
|
|
|
|
pjlHeader;
|
|
|
|
|
|
2014-03-21 10:05:09 +01:00
|
|
|
|
stage="printing"
|
|
|
|
|
}
|
2020-08-30 21:50:45 +02:00
|
|
|
|
[ "$page" = "999999999.page.pbm" ] && {
|
2014-04-27 11:25:34 +02:00
|
|
|
|
log "Last stage. Flushing PJL footer"
|
2020-08-30 21:50:45 +02:00
|
|
|
|
pjlFooter;
|
|
|
|
|
|
2014-03-21 10:05:09 +01:00
|
|
|
|
pid=`ps ax | grep $uid | grep -v grep | awk ' { print $1 } '`
|
|
|
|
|
[ ! "x$pid" = "x" ] && kill $pid
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
[ "$stage" = "printing" ] && {
|
2020-08-30 21:50:45 +02:00
|
|
|
|
pageOutput;
|
2014-03-21 10:05:09 +01:00
|
|
|
|
}
|
|
|
|
|
done
|
|
|
|
|
) &
|
|
|
|
|
|
|
|
|
|
# Converting from PostScript to PostScript-monochrome, then to PBM image format (per page)
|
2020-08-30 21:50:45 +02:00
|
|
|
|
#$GS -sDEVICE=ps2write -sOutputFile=- -r$resolution -dQUIET -dBATCH -dNOPAUSE - |
|
|
|
|
|
$GS -sDEVICE=pbmraw -sOutputFile=${uid}/%03d.page.pbm -r$resolution -dQUIET -dBATCH -dNOPAUSE -
|
2014-03-21 10:05:09 +01:00
|
|
|
|
|
|
|
|
|
stop
|
|
|
|
|
wait
|
|
|
|
|
|
|
|
|
|
trapINT
|
2020-08-30 21:50:45 +02:00
|
|
|
|
}
|
|
|
|
|
else {
|
2014-04-27 11:25:34 +02:00
|
|
|
|
log "Synchronous variant"
|
2020-08-30 21:50:45 +02:00
|
|
|
|
mkdir ${uid}/gs-temp
|
|
|
|
|
export TEMP=${uid}/gs-temp
|
2014-03-21 10:05:09 +01:00
|
|
|
|
|
|
|
|
|
# Converting from PostScript to PostScript-monochrome, then to PBM image format (per page)
|
2014-04-27 11:25:34 +02:00
|
|
|
|
log "Converting document to pages"
|
2020-08-30 21:50:45 +02:00
|
|
|
|
$GS -sDEVICE=ps2write -sOutputFile=- -r$resolution -dQUIET -dBATCH -dNOPAUSE - | $GS -sDEVICE=pbmraw -sOutputFile=${uid}/%03d.page.pbm -r$resolution -dQUIET -dBATCH -dNOPAUSE -
|
2014-04-27 11:25:34 +02:00
|
|
|
|
log "Conversion complete"
|
2014-03-21 10:05:09 +01:00
|
|
|
|
|
2020-08-30 21:50:45 +02:00
|
|
|
|
pjlHeader;
|
|
|
|
|
|
|
|
|
|
ls -1 $uid | grep "page.pbm" | while read page; do
|
|
|
|
|
#for page in ${uid}/*.page.pbm; do #looks untested - does not work
|
|
|
|
|
log "Page $page"
|
|
|
|
|
|
|
|
|
|
pageOutput;
|
2014-03-21 10:05:09 +01:00
|
|
|
|
|
|
|
|
|
done
|
2013-09-06 17:05:03 +02:00
|
|
|
|
|
2014-03-21 10:05:09 +01:00
|
|
|
|
# Flushing PJL footer
|
2020-08-30 21:50:45 +02:00
|
|
|
|
pjlFooter;
|
|
|
|
|
|
|
|
|
|
if [ ! "${DEBUG}" = "yes" ]; then
|
|
|
|
|
rm -rf $uid
|
|
|
|
|
fi
|
|
|
|
|
}
|
|
|
|
|
fi
|
2014-07-23 19:24:28 +02:00
|
|
|
|
|
|
|
|
|
exit 0
|