blob: 6abf39ce2a0226a085c4031f4145e375a680e2e1 (
plain)
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
|
#!/usr/bin/env bash
: <<COMMENT
Copyright (C) 2014 Tri Le <trile7 at gmail dot com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation version 3.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
COMMENT
version="pdftools v0.2.3"
blue='\e[0;34m'; green='\e[0;32m'; red='\e[0;31m'; yellow='\e[0;33m'; bold='\e[1m';none='\e[0m'
function menu {
n=0
choices=("$@")
echo -e "$blue$bold$menutitle"
for i in "${choices[@]}"; do
echo -e "$none$((n++))) $yellow$i"
done
echo -en "${none}c|q) ${yellow}Cancel"
echo -e $green
read -p "Enter a choice from above menu: " i
echo -e $none
[[ $i =~ c|q ]] && exit 1
if test $i -lt $n 2>/dev/null; then
choice=${choices[i]}; return $i
else
echo -e "$red$i is an invalid entry...please try again!"
menu "${choices[@]}"
fi
}
function mainmenu {
if [[ -z $1 ]]; then
menutitle="---PDF Tools---"
menu "PDF Merge" "PDF Split" "PDF Extract" "PDF to JPG" "PDF to Text"
case $? in
0) merge ;;
1) split ;;
2) extract ;;
3) 2jpg ;;
4) 2txt ;;
esac
else
$cmd
fi
mainmenu
}
function merge {
if [[ ${#files[@]} -lt 2 ]]; then
echo -e "${red}Select at least 2 or more PDF files to merge"
exit 1
fi
echo -ne $green
read -p "Enter merged pdf name (default merged.pdf): " f
echo -e $none
if [[ $f ]]; then
[[ $f != *.pdf ]] && f=$f.pdf
else f=merged.pdf
fi
[[ -e $f ]] && f=${f%.*}-`date +%s`.pdf
mkdir -p `dirname "$f"`
gs -o -q -sDEVICE=pdfwrite -sOutputFile="$f" "${files[@]}"
if [[ $? -eq 0 ]]; then
echo -e $green
read -p "Remove `echo ${files[@]}` (Y/n)? " i
[[ $i = n ]] || rm "${files[@]}"
fi
}
function split {
for i in "${files[@]}"; do
totalp=`gs -q -dNODISPLAY -c "($i) (r) file runpdfbegin pdfpagecount = quit"`
for (( p=1; p<=$totalp; p++ )); do
gs -o -q -sDEVICE=pdfwrite -dFirstPage=$p -dLastPage=$p -sOutputFile="${i%.*}-p$p.pdf" "$i"
done
done
}
function extract {
echo -e "Use ${green}space$none for separating pages, ${green}dash$none for range of pages, and ${green}end$none for the last page. For example, to extract pages 1, 5, and 8 to the last page, enter: ${green}1 5 8-end"
read -p "Enter page: " -a p
echo -e $none
if [[ -z $p ]]; then
echo -e "${red}A value is required for page $none"
extract "$@"; return 1
fi
for i in ${p[@]}; do
[[ $i = end ]] && continue
test ${i%*-end} -gt 0 2>/dev/null && continue
test $((i)) -lt 0 2>/dev/null && continue
echo -e "${red}$i is invalid $none"
extract "$@"; return 1
done
tmpdir=/tmp/pdfextract
mkdir -p $tmpdir
for i in "${files[@]}"; do
pages=`gs -q -dNODISPLAY -c "($i) (r) file runpdfbegin pdfpagecount = quit"`
for j in ${p[@]}; do
if [[ $j = *-* ]]; then
p1=`echo $j | cut -d'-' -f1`
[[ $p1 -gt $pages ]] && continue
p2=`echo $j | cut -d'-' -f2`
[[ $p2 = end ]] && p2=$pages
[[ $p2 -gt $pages ]] && p2=$pages
elif [[ $j = end ]]; then
p1=$pages; p2=$pages
elif [[ $j -le $pages ]]; then
p1=$j; p2=$j
fi
gs -o -q -sDEVICE=pdfwrite -dFirstPage=$p1 -dLastPage=$p2 -sOutputFile=$tmpdir/$j.pdf "$i"
done
ext=${p[@]}
ext=p${ext// /,}.pdf
gs -o -q -sDEVICE=pdfwrite -sOutputFile="${i%.*}-$ext" $tmpdir/*.pdf
rm -rf $tmpdir
done
}
function 2jpg {
for i in "${files[@]}"; do
gs -o -q -sDEVICE=jpeg -sOutputFile="${i%.*}-p%d.jpg" "$i"
done
}
function 2txt {
for i in "${files[@]}"; do
gs -o -q -sDEVICE=txtwrite -sOutputFile="${i%.*}.txt" "$i"
done
}
function usage {
echo -e "${bold}$version"
echo -e "Usage: ${0##*/} [option] pdf1 pdf2 ..... $none"
echo "Options: menu is shown if option is not specified"
echo " --merge combine 2 or more pdf files to one"
echo " --split split pdf file to individual page"
echo " --extract extract page and/or range of pages from pdf file"
echo " --2jpg convert pdf page to jpg"
echo " --2txt convert text portion of pdf file to text"
exit
}
case $1 in
-h|--h*) usage ;;
--*) cmd=${1#--}; shift; files=("$@") ;;
*) files=("$@") ;;
esac
[[ $# -eq 0 ]] && usage
mainmenu $cmd
|