#!/bin/bash # adjust to paths for pdfinfo (from the xpdf package, which I got from # fink.sourceforge.net) and pdflatex (from the teTeX package, which I # got from ii2.sourceforge.net, though fink has it as well). Uses the # pdfpages TeX package, but that should be included with teTeX. If # not, ctan.org has it. pdfinfo="/sw/bin/pdfinfo" pdflatex="/usr/local/teTeX/bin/powerpc-apple-darwin-current/pdflatex" # 3RD argument when PDF Services calls this script is the full path of # the PDF file to process infile="$3" # temporary directory tied to the script's unique process ID tmpdir="/tmp/$$" # remove temporary files and directories when we exit for any reason. # As per Apple's sample at www.apple.com/applescript/print, the # *parent* directory of the input file is deleted on completion. onexit() { rm -rf "$tmpdir" # Deletes temp directory and anything in it rmdir "${infile%/*}" # Parent directory is only removed if empty } trap onexit EXIT # put input file into new working directory -- renamed without spaces, # as files and paths with spaces in them make TeX unhappy mkdir "$tmpdir" mv "$infile" "$tmpdir/in.pdf" cd "$tmpdir" # get page count, rounded up to the nearest 4 pages=$("$pdfinfo" "in.pdf" | awk '/^Pages/{print $2+3 - ($2+3)%4;}') # create single fold booklet form in the working directory "$pdflatex" -interaction=batchmode \ '\documentclass{book}\ \usepackage{pdfpages}\ \begin{document}\ \includepdf[pages=-,signature='$pages',landscape]{in.pdf}\ \end{document}' 2>&1 >/dev/null # reopen in Preview. Can save, print duplex or print odd then even # (manual duplex) from there open -a Preview book.pdf