Last active
March 27, 2019 20:08
-
-
Save commenthol/bafa45de829c9d804e92073f07ccb324 to your computer and use it in GitHub Desktop.
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
#!/bin/bash | |
# rotate scanned pdf pages by 180° | |
# name all pages | |
help=$(cat <<EOS | |
Usage: rotate-pdf.sh IN.pdf OUT.pdf [PAGES] | |
Read from IN.pdf, rotate PAGES by 180° and write out to OUT.pdf | |
Requires `pdftk` to work. | |
Example: rotate-pdf.sh in.pdf out.pdf 2 4 7 | |
EOS | |
) | |
if [ -z "$1" ] || [ "$1" = "-h" ] || [ "$1" = "--help" ]; then | |
echo -e "$help" | |
exit 1 | |
fi | |
#----------------------------------------------------------------------- | |
in=$1 | |
shift | |
out=$1 | |
shift | |
south=($@) | |
#----------------------------------------------------------------------- | |
pageCount=$(pdftk $in dump_data 2> /dev/null | grep "NumberOfPages" | cut -d":" -f2 | xargs expr) | |
pages=() | |
for ((i=1; i<=$pageCount; i++)); do | |
ok=0 | |
for p in ${south[@]}; do | |
if [ $i -lt $p ]; then | |
break | |
elif [ $i -eq $p ]; then | |
pages+=(${i}south) | |
ok=1 | |
fi | |
done | |
if [ $ok -eq 0 ]; then | |
pages+=(${i}) | |
fi | |
done | |
echo ${pages[*]} | |
pdftk $in cat ${pages[*]} output $out |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment