rotate pdf pages of bitmap scan pdfs any degrees
#!/bin/bash
# usage: pdfrotate.sh <angle> <file.pdf>
# Rotates all pages of a PDF using pdftk (for 90° increments) or Ghostscript otherwise.
angle=$1
file=$2
if [ -z "$angle" ] || [ -z "$file" ]; then
echo "Usage: $0 <angle> <file.pdf>"
exit 1
fi
base="${file%.pdf}"
out="${base}_rotated_${angle}.pdf"
# Round angle if needed
norm_angle=$((angle % 360))
# Handle pdftk for standard rotations
if [[ "$norm_angle" == "90" || "$norm_angle" == "180" || "$norm_angle" == "270" || "$norm_angle" == "0" ]]; then
case $norm_angle in
0) dir="north" ;; # no rotation
90) dir="right" ;;
180) dir="down" ;;
270) dir="left" ;;
esac
echo "Rotating with pdftk ($norm_angle°)..."
pdftk "$file" cat 1-end${dir} output "$out"
else
# Use Ghostscript for non-90° angles
echo "Rotating with Ghostscript ($angle°)..."
gs -o "$out" -sDEVICE=pdfwrite -dAutoRotatePages=/None \
-c "<< /Orientation 0 /Rotate $angle >> setpagedevice" \
-f "$file"
fi
if [ $? -eq 0 ]; then
echo "Done. Output: $out"
else
echo "Error: rotation failed." >&2
fi
# usage: pdfrotate.sh <angle> <file.pdf>
# Rotates all pages of a PDF using pdftk (for 90° increments) or Ghostscript otherwise.
angle=$1
file=$2
if [ -z "$angle" ] || [ -z "$file" ]; then
echo "Usage: $0 <angle> <file.pdf>"
exit 1
fi
base="${file%.pdf}"
out="${base}_rotated_${angle}.pdf"
# Round angle if needed
norm_angle=$((angle % 360))
# Handle pdftk for standard rotations
if [[ "$norm_angle" == "90" || "$norm_angle" == "180" || "$norm_angle" == "270" || "$norm_angle" == "0" ]]; then
case $norm_angle in
0) dir="north" ;; # no rotation
90) dir="right" ;;
180) dir="down" ;;
270) dir="left" ;;
esac
echo "Rotating with pdftk ($norm_angle°)..."
pdftk "$file" cat 1-end${dir} output "$out"
else
# Use Ghostscript for non-90° angles
echo "Rotating with Ghostscript ($angle°)..."
gs -o "$out" -sDEVICE=pdfwrite -dAutoRotatePages=/None \
-c "<< /Orientation 0 /Rotate $angle >> setpagedevice" \
-f "$file"
fi
if [ $? -eq 0 ]; then
echo "Done. Output: $out"
else
echo "Error: rotation failed." >&2
fi