Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Convert a/any (ps, pdf, dvi, and plain text) file to a booklet for printing on an A4 duplex printer to save paper. By default the output is sent to the systems standard printer.
#!/bin/bash
#
# DESCRIPTION
#
# Convert a(ny) file to a booklet for printing
# on an A4 duplex printer to save paper.
# By default the output is sent to the systems
# standard printer.
#
# Supports ps, pdf, dvi, and plain text.
#
# OPTIONS:
# -h Print message
# -p Sent to named printer
# -o Store output in file
#
printHelp() {
cat << EOF
a2book - convert file to booklet
DESCRIPTION
Convert a(ny) file to a booklet for printing
on an A4 duplex printer to save paper.
By default the output is sent to the systems
standard printer.
Supports ps, pdf, dvi, and plain text.
OPTIONS:
-h This help message
-p Sent to named printer
-o Store output in file
EOF
}
#check iff input
if [ -z $1 ];then
printHelp
exit 1
fi
#get params
PRINTER=""
OUTFILE=""
while getopts ":p:o:h" OPTION;do
case "$OPTION" in
"h")
printHelp
exit 1
;;
"o")
OUTFILE=$OPTARG
;;
"p")
PRINTER=$OPTARG
;;
*)
printHelp
exit 1
;;
esac
done
#so we can access $1
shift $((OPTIND-1))
#create name for a temporary file
TEMPFILE="/tmp/$(basename $1).$RANDOM.ps"
# make $1 to ps
case `file -b $1` in
*PDF* ) pdftops $1 $TEMPFILE;;
*PostScript* ) cp $1 $TEMPFILE;;
*DVI* ) dvips -o $TEMPFILE $1 ;;
# *LaTeX* ) a2ps -B1q --border=no -o temp.ps $1;;
* ) a2ps -B1q --border=no --delegate=no -o $TEMPFILE $1;;
esac
#make it a booklet
psbook -q $TEMPFILE | pstops -q -pa4 '2:0L@0.7(21cm,0)+1L@0.7(21cm,14.85cm)' |
pstops -q -pa4 '2:0,1U(21cm,29.7cm)' |
sed '/^%!PS-Adobe./a\%%BeginFeature *Duplex True\n << \/Duplex true >> setpagedevice \n%%EndFeature' >$1.book
# store it
if [ -z $OUTFILE ];then
if [ -z $PRINTER ];then
lpr $1.book
else
lpr -P$PRINTER $1.book
fi
rm $1.book
else
mv $1.book $OUTFILE
echo "Booklet saved to $OUTFILE"
fi
# del temp file
rm $TEMPFILE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment