updated on Thu Jan 12 04:00:44 UTC 2012
[aur-mirror.git] / pdfselect / pdfselect
blobeca6eae545bf3dfc2caa82e05b7d3c1ec19fd587
1 #! /bin/sh
2 ##
3 ## pdfselect: A shell program to pages subset of a PDF file
4 ##
5 ## Author ezzetabi, based on the work of
6 ## Author David Firth (http://www.warwick.ac.uk/go/dfirth)
7 ##
8 version=1.1
9 echo "This is pdfselect version ""$version"
11 ## Relies on pdflatex and the 'pdfpages' package (version 0.2e
12 ## or later).
14 #######################################################################
15 ## CONFIGURATION: change this block as necessary
17 ## THESE SETTINGS WILL BE OVER-RIDDEN by any found at
18 ## /etc/pdfselect.conf
19 ## /usr/share/etc/pdfselect.conf
20 ## /usr/local/share/pdfselect.conf
21 ## /usr/local/etc/pdfselect.conf
22 ## ~/.pdfselect.conf
23 ## (which are read in that order)
25 ## First say where your "pdflatex" program lives:
27 pdflatex=pdflatex
28 #pdflatex="pdflatex.exe" ## this for Windows computers
30 ## Next pdfinfo name:
32 pdfinfo=pdfinfo
34 ## Next a permitted location for temporary files on your system:
36 tempfileDir="/var/tmp" ## /var/tmp is standard on many unix systems
37 #tempfileDir="C:/tmp" ## use something like this under Windows
39 ## Now specify the default settings for pdfselect:
41 paper=a4paper ## alternatives are other LaTeX paper sizes
42 pages=all
43 evenonly=false
44 oddonly=false
45 reversed=false
46 orient=portrait ## alternatives are landscape and portrait
47 tidy=true ## delete all temporary files immediately
48 subset=false ## internal, true if evenonly or oddonly is true
49 pagesknown=false ## internal, if true the page range is the same for
50 ## every file. (e.i. pages=- or pages=last,1)
52 ## END OF CONFIGURATION
53 #######################################################################
55 ## Read the configuration file(s) if such exist:
57 for d in /etc /usr/share/etc /usr/local/share /usr/local/etc
58 do if test -f $d/pdfselect.conf; then
59 echo "Reading site configuration from $d/pdfselect.conf"
60 source $d/pdfselect.conf
62 done
63 if test -f ~/.pdfselect.conf; then
64 echo "Reading user defaults from ~/.pdfselect.conf";
65 source ~/.pdfselect.conf;
67 #######################################################################
69 ## Define the output of "pdfselect --help"
71 helptext="
72 Usage: pdfselect args
73 where args must include source pdf filename(s) and optionally also
74 * a list or range of pages to be included, for example
75 --pages 3-6
76 --pages 2,8,4,5
77 --pages all
78 * a LaTeX papersize, for example --paper a4paper
79 --paper letterpaper
80 * one of --evenonly true
81 --evenonly false
82 if true the outfile will contains only even pages.
83 * one of --oddonly true
84 --oddnonly false
85 if true the outfile will contains only odd pages.
86 * one of --reversed true
87 --reversed false
88 if true the outfile will contains only odd pages.
89 * the output page orientation, one of --orient landscape
90 --orient portrait
91 * a specific name for the output file, e.g. --outfile printme.pdf
92 * one of --tidy true
93 --tidy false
94 according to whether or not temporary files should be deleted immediately.
95 If --tidy false is used, such files are left in $tempfileDir.
97 Default arguments for you at this site are
98 --paper $paper --pages $pages --tidy $tidy --evenonly $evenonly --oddonly
99 $oddonly --reversed $reversed --orient $orient
102 ## Check that necessary LaTeX packages are installed
104 PATH=`dirname "$pdflatex"`:$PATH
105 export PATH
106 case `kpsewhich pdfpages.sty` in
107 "") echo "pdfselect: pdfpages.sty not installed"; exit 1;;
108 esac
109 case `kpsewhich eso-pic.sty` in
110 "") echo \
111 "pdfselect: eso-pic.sty not installed (see the pdfpages manual)"
112 exit 1;;
113 esac
114 case `kpsewhich everyshi.sty` in
115 "") echo \
116 "pdfselect: everyshi.sty not installed (see the pdfpages manual)"
117 exit 1;;
118 esac
120 ## Now do the argument loop...
122 sourcePath=
123 outFile=
124 inFiles=0
125 newline='
127 while test -n "${1}"; do
128 case "${1}" in
129 *.pdf) inFiles=`expr $inFiles + 1`;
130 sourcePath="$sourcePath$newline${1}";;
131 --help) echo "$helptext";
132 exit 0;;
133 --pages) pages="${2}"
134 shift;;
135 --paper) paper="${2}"
136 shift;;
137 --outfile) outFile="${2}"
138 case "$outFile" in
139 *".pdf");;
140 *) echo "pdfselect: outfile name must end in .pdf";
141 exit 1;;
142 esac
143 shift;;
144 --tidy) tidy="${2}"
145 shift;;
146 --evenonly) evenonly="${2}"
147 if [ $oddonly == true ] ; then
148 echo "pdfselect: oddonly and evenonly must not appear together."
149 exit 1
151 subset=true;
152 shift;;
153 --oddonly) oddonly="${2}"
154 if [ $evenonly == true ] ; then
155 echo "pdfselect: oddonly and evenonly must not appear together."
156 exit 1
158 subset=true;
159 shift;;
160 --reversed) reversed="${2}"
161 shift;;
162 --orient) orient="${2}"
163 shift;;
164 *) echo "pdfselect: unrecognised argument ${1}"; exit 1;;
165 esac
166 shift
167 done
168 if test $inFiles -gt 1 ; then
169 if test "$outFile" != "" ; then
170 echo "pdfselect: --outfile cannot be used with multiple input files";
171 echo "pdfselect: no files processed"
172 exit 1;
175 case "$sourcePath" in
176 "") echo "pdfselect: no pdf source file specified
177 For information on usage try \"pdfselect --help\""; exit 1;;
178 esac
179 OIFS=IFS
180 IFS="$newline"
181 for k in $sourcePath
183 if test -f "$k"; then :;
184 else echo "pdfselect: ""$k"" does not exist, no files were processed";
185 exit 1;
187 done
188 IFS=OIFS
189 if [ $subset == false ] ; then
190 if [ $pages == all ] ; then
191 pagesknown=true
192 if [ $reversed == true ] ; then
193 pages='last-1'
194 else
195 pages=-
199 if [ $oddonly == true ] ; then
200 oddonly=o
201 else
202 oddonly=''
204 if [ $evenonly == true ] ; then
205 evenonly=e
206 else
207 evenonly=''
209 if [ $reversed == true ] ; then
210 reversed=r
211 else
212 reversed=''
215 ## That's the arguments done.
217 pwd=`pwd | sed 's/ /\\ /'`
218 IFS="$newline"
220 ## Now work on the input file (or files in turn)
222 counter=0
223 opages=$pages
224 for inFile in $sourcePath
226 echo "Processing $inFile..."
227 counter=`expr $counter + 1`
228 cd "$pwd"
229 pdfName=`basename "$inFile"`
230 sourceDir=`dirname "$inFile"` ; cd "$sourceDir" ; sourceDir=`pwd`
231 sourceFullPath="$sourceDir"/"$pdfName"
232 #if needed generate the pages range
233 if [ $pagesknown == false ] ; then
234 pages=$opages
235 # detect the number of pages
236 lastpage=`$pdfinfo "$sourceFullPath" | sed -n -e '/^Pages: / s_^Pages: *__ p'`
237 # replaces 'all' and 'last' with explicit ranges.
238 pages=`echo -n $pages | \
239 sed -e 's_^ *\| *$__' -e 's_last_'$lastpage'_g' -e 's_all_1-'$lastpage'_g'`
240 # enumerate the page list explicitly
241 pages=`pdfselect_i $lastpage $pages $reversed $oddonly $evenonly`
242 # split the page numbers with commas
243 pages=`echo -n $pages | sed -e 's_^ *\| *$__' -e 's_ _,_g'`
244 # add surrounding brackets
245 pages={$pages}
246 if [ $pages == '{}' ] ; then
247 echo Selected an empty/invalid range for $pdfName. Skipping...
248 pages=''
251 if [ x$pages != x ] ; then
252 cd "$pwd"
253 case "$outFile" in
254 "") ## no --outfile argument supplied
255 outfileNameRoot=`echo "$pdfName" | \
256 sed 's/\.pdf$//'`-$evenonly$oddonly$reversed;
257 outfileDir="$sourceDir";
258 outFile="$outfileNameRoot"".pdf";;
259 *) ## --outfile argument was supplied
260 outfileNameRoot=`basename "$outFile" | sed 's/\.pdf$//';`
261 outfileDir=`dirname "$outFile"` ;
262 cd "$outfileDir" ;
263 outfileDir=`pwd` ;;
264 esac
265 case "$outfileDir"/"$outfileNameRoot"".pdf" in
266 $sourceFullPath) echo "pdfselect: outfile and source cannot be the same";
267 exit 1;;
268 //"$outfileNameRoot"".pdf") outfileDir="";; ## in case of output
269 esac ## to the root directory!
271 ## Now edit the temporary LaTeX input file
273 uniqueName="$$"-"$counter"
274 ln -s "$sourceFullPath" "$tempfileDir"/"$uniqueName"source.pdf
275 texFile="$tempfileDir"/"$uniqueName".tex
276 msgFile="$tempfileDir"/"$uniqueName".msgs
277 (sed s*pdfname*"$tempfileDir"/"$uniqueName"source.pdf* <<EndTemplate
278 \\documentclass[papersize,orientation]{article}
279 \\usepackage{pdfpages}
280 \\begin{document}
281 \\includepdf[fitpaper=true,pages=]{pdfname}
282 \\end{document}
283 EndTemplate
285 | sed s/pages=/pages="$pages"/ \
286 | sed s/papersize/"$paper"/ \
287 | sed s/orientation/"$orient"/ \
288 > "$texFile"
289 echo " Temporary LaTeX file for this job is ""$texFile"
291 ## Now run pdflatex and tidy up
293 echo " Calling pdflatex..."
294 cd "$tempfileDir"
295 "$pdflatex" --interaction batchmode "$texFile" > "$msgFile"
296 if test -f "$tempfileDir"/"$uniqueName"".aux";
297 ## ie if LaTeX didn't choke
298 then if cp "$tempfileDir"/"$uniqueName".pdf \
299 "$outfileDir"/"$outfileNameRoot"".pdf"
300 then echo " Finished: output is"\
301 "$outfileDir"/"$outfileNameRoot"".pdf"
303 case "$tidy" in
304 true) rm "$tempfileDir"/"$uniqueName"* ;;
305 esac
306 outFile=""
307 else echo " Failed: output file not written"
310 done
311 IFS=OIFS