Skip to content

Instantly share code, notes, and snippets.

@slowkow
Last active April 22, 2018 13:37
Show Gist options
  • Save slowkow/18b25f74b2773a82d7f9ed2be95e2b7c to your computer and use it in GitHub Desktop.
Save slowkow/18b25f74b2773a82d7f9ed2be95e2b7c to your computer and use it in GitHub Desktop.
Add a blank page to a pdf
#!/usr/bin/env bash
# Count pages in all pdf files in the current directory:
#
# pagecount .
#
# Count pages in this pdf:
#
# pagecount file.pdf
#
function pagecount() {
# brew install exiftool
command exiftool -T -filename -PageCount -s3 -ext pdf "$1" | cut -f2 -d$'\t'
}
# Add a blank page to an existing pdf
#
# addblank file.pdf out.pdf
#
function addblank() {
in="$1"
out="$2"
blank="blank.pdf"
if [ ! -f "$blank" ]
then
# brew install imagemagick
# Use ImageMagick to create a Letter-sized blank pdf
command convert xc:none -page Letter "$blank"
fi
# Use the built-in macOS Python script to combine pdf filesj
script="/System/Library/Automator/Combine PDF Pages.action/Contents/Resources/join.py"
"$script" -o "$out" "$in" "$blank"
}
# Example usage:
for f in *ebook.pdf
do
n=$(pagecount "$f")
if [[ $((n%2)) -eq 1 ]]
then
echo "Add blank.pdf to $f"
addblank "$f" "${f%.pdf}-even.pdf"
else
cp -f "$f" "${f%.pdf}-even.pdf"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment