Skip to content

Instantly share code, notes, and snippets.

@ZeppLu
Created September 27, 2017 14:55
Show Gist options
  • Save ZeppLu/6d392892af015a81ad5615bb4b080c35 to your computer and use it in GitHub Desktop.
Save ZeppLu/6d392892af015a81ad5615bb4b080c35 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Usage:
# ./this_script.bash -i [input file] -o [output file]
# generate a PDF that can be directly printed from an E-book
pdf_file=""
temp_dir="temp"
output_file=""
while getopts 'i:o:' opt; do
case $opt in
i)
pdf_file="$OPTARG"
;;
o)
output_file="$OPTARG"
;;
*)
exit 1
;;
esac
done
# both arguments are required
[ -z "$pdf_file" -o -z "$output_file" ] && exit 1
pdf_page_count="$(pdfinfo "$pdf_file" | grep '^Pages:' | awk '{print $2}')"
# including empty pages
book_page_count="$(( ($pdf_page_count + 3) / 4 * 4 ))"
pdfnup_arg=""
for i in $(seq $(( $book_page_count / 2 ))); do
neighbor_page=$(( $book_page_count + 1 - $i ))
if [[ "$neighbor_page" -gt "$pdf_page_count" ]]; then
neighbor_page="{}"
fi
if [[ "$(( $i % 2 ))" == 0 ]]; then
left_page="$i"
right_page="$neighbor_page"
else
left_page="$neighbor_page"
right_page="$i"
fi
if [[ -z "$pdfnup_arg" ]]; then
pdfnup_arg="$left_page,$right_page"
else
pdfnup_arg+=",$left_page,$right_page"
fi
done
# TODO: automatically set paper size according to original pdf file
pdfnup "$pdf_file" "$pdfnup_arg" -q --outfile "$output_file"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment