Skip to content

Instantly share code, notes, and snippets.

@commenthol
Last active March 27, 2019 20:08
Show Gist options
  • Save commenthol/bafa45de829c9d804e92073f07ccb324 to your computer and use it in GitHub Desktop.
Save commenthol/bafa45de829c9d804e92073f07ccb324 to your computer and use it in GitHub Desktop.
#!/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