Last active
January 24, 2023 20:36
-
-
Save cuibonobo/b3e8e07400a90ad8de5075778dd7a809 to your computer and use it in GitHub Desktop.
Process a PDF to allow for half-size booklet printing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Get the number of pages in the PDF. (On a Mac you can do `pages=$(mdls -raw -name kMDItemNumberOfPages $PDF)`) | |
pages=$(pdftk $PDF dump_data | grep NumberOfPages | awk '{print $2}') | |
# Get the number of 16-page booklets | |
booklets=$(($pages/16)) | |
if [[ $(($pages%16)) -gt 0 ]]; then | |
booklets=$(($booklets+1)) | |
fi | |
# Determine where to split the book for the half-size | |
booksplit=$(($booklets/2)) | |
# Determine at what page to split the book | |
pagesplit=$(($booksplit*16)) | |
# Determine the page number for the extra booklet, if any | |
extrabooklet=0 | |
if [[ $(($booklets%2)) -gt 0 ]]; then | |
extrabooklet=$(($pagesplit*2+1)) | |
fi | |
# Split the PDF into 2 parts | |
pdftk $PDF cat 1-$pagesplit output split-top.pdf | |
pdftk $PDF cat $(($pagesplit+1))-$(($pagesplit*2)) output split-bottom.pdf | |
# Insert blank pages on the bottom to make sure page numbers are equal | |
echo '\shipout\hbox{}\end' | pdftex # Creates 'texput.pdf' | |
# Create a 16-page PDF of blank pages | |
pdftk texput.pdf cat 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 output blank.pdf | |
# Determine if we need an extra booklet or to pad the last booklet | |
if [[ $extrabooklet -gt 0 ]]; then | |
pdftk $PDF cat $extrabooklet-$pages output extra.pdf | |
extrapages=$(($pages-$extrabooklet)) | |
pdftk blank.pdf cat 1-$extrapages output blank-cut.pdf | |
pdftk A=extra.pdf B=blank-cut.pdf shuffle A B output extra-shuffle.pdf | |
pdftk A=extra-shuffle.pdf B=blank.pdf cat A B1 B1 B1 B1 B1 B1 B1 B1 B1 B1 B1 B1 B1 B1 B1 B1 output extra-extended.pdf | |
pdftk extra-extended.pdf cat 1-32 output extra-booklet.pdf | |
rm extra.pdf blank-cut.pdf extra-shuffle.pdf extra-extended.pdf | |
else | |
# Add 15 blank pages to the end, then cut the pages back to the right number | |
pdftk A=split-bottom.pdf B=texput.pdf cat A B B B B B B B B B B B B B B B B output split-bottom-extended.pdf | |
pdftk split-bottom-extended.pdf cat 1-$pagesplit output split-bottom.pdf | |
rm split-bottom-extended.pdf | |
fi | |
# Collate the two parts | |
pdftk A=split-top.pdf B=split-bottom.pdf shuffle A B output collated.pdf | |
# Add the extra booklet to the end if necessary | |
if [[ $extrabooklet -gt 0 ]]; then | |
pdftk A=collated.pdf B=extra-booklet.pdf cat A B output collated-extra.pdf | |
mv collated-extra.pdf collated.pdf | |
rm extra-booklet.pdf | |
fi | |
# Arrange 2 logical pages vertically on a single page | |
pdfjam collated.pdf --nup 1x2 --no-landscape --papersize '{8.5in,22in}' --outfile output.pdf | |
# Delete our temporary files | |
rm split-top.pdf split-bottom.pdf texput.pdf texput.log blank.pdf collated.pdf |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment