Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/bin/zsh
zmodload zsh/mathfunc
local usage="Usage: ${0##*/} [-p lastpage] PDF"
local -a help pages
zparseopts -D h=help -help=help p:=pages
if (( ! $# )); then
echo $usage >&2
exit 1
fi
if [[ -n $help ]]; then
echo $usage
exit
fi
f="$1"; shift
if [[ ! -f "$f" ]]; then
echo "Not a file: “$f" >&2
exit 1
fi
if (( ${#pages} == 0 )); then
local total_pages="$(pdfinfo "$f" | grep '^Pages:' | grep -Po '\d+$')"
pages="$total_pages"
else
pages=${pages[2]}
fi
local sig="$(( int(ceil(pages / 4.) * 4) ))"
local name="$(basename "$f" .pdf)"
local title=$(pdfinfo "$f" | grep '^Title:' | sed -E 's/^Title:\s+//')
[[ -z "$title" ]] && title=$name
local pdf="$(mktemp --tmpdir "$name-booklet-XXX.pdf")"
pdfbook --a3paper --signature "$sig" -o "$pdf" "$f" "1-$pages" || {
unlink "$pdf"
exit 1
}
echo "$title"
# two-sided-long-edge apparently only means *long edge* when in portrait mode
lpr \
-J "$title" \
-o media=A3 \
-o sides=two-sided-long-edge \
-o landscape=true \
"$pdf"
unlink "$pdf"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment