-
Notifications
You must be signed in to change notification settings - Fork 0
/
rotpdf
executable file
·54 lines (46 loc) · 1.74 KB
/
rotpdf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/usr/bin/env bash
die() { printf %s "${@+$@$'\n'}" 1>&2 ; exit 1 ; }
see() ( { set -x; } 2>/dev/null ; "$@" ) ;
command -v qpdf > /dev/null || die "qpdf needs to be installed"
do1file() (
fnm="$1"
if [[ ! -f "$fnm" ]]; then
echo "$fnm does not name a file"
return
fi
# break down $fnm:
fpath=""
if [[ $fnm == */* ]]; then
fpath="${fnm%/*}/"
cd "$fpath" || die "cd to $fpath failed"
fi # ; echo "fpath=$fpath'"
# note that because we cd above, remaining filenames used are sanspath
sanspath="${fnm##*/}" # ; echo "sanspath=$sanspath'" # https://stackoverflow.com/a/965069
ext="${sanspath##*.}" # ; echo "ext=$ext'"
sansext="${sanspath%.*}" # ; echo "sansext=$sansext'"
# logical parameters:
bakfnm="${sansext}_orig.$ext" ; echo "bakfnm $bakfnm"
destfnm="$sanspath" ; echo "destfnm $destfnm'"
mv -n "$destfnm" "$bakfnm" || die "backup-create move failed"
dorot "$bakfnm" "$destfnm"
)
# EX: rotate *select* pages in pdf
# qpdf src.pdf --rotate=+90:19-21,24-30,43-49 -- out.pdf
# EX: merge/cat whole pdf files into one
# qpdf --empty --pages *.pdf -- out.pdf
# EX: extract page range from in.pdf into out.pdf (note the '.') https://askubuntu.com/a/672001
# qpdf in.pdf --pages . 1-16 -- out.pdf
dorot() ( qpdf --rotate="$rotdeg" "$1" "$2" )
help() { die "need rotmode file [file...] where rotmode is one of cw ccw 180" ; }
rotmode="$1" ; shift || help
case $rotmode in
cw) rotdeg=+90 ;;
ccw) rotdeg=-90 ;;
180) rotdeg=180 ;;
\?) help ;;
esac
[[ "$rotdeg" ]] || help
[[ "$1" ]] || echo "missing name of file(s) to rotate"
for fnm in "$@"; do
do1file "$fnm"
done