3 ## pdfselect: A shell program to pages subset of a PDF file
5 ## Author ezzetabi, based on the work of
6 ## Author David Firth (http://www.warwick.ac.uk/go/dfirth)
9 echo "This is pdfselect version ""$version"
11 ## Relies on pdflatex and the 'pdfpages' package (version 0.2e
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
23 ## (which are read in that order)
25 ## First say where your "pdflatex" program lives:
28 #pdflatex="pdflatex.exe" ## this for Windows computers
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
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
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"
73 where args must include source pdf filename(s) and optionally also
74 * a list or range of pages to be included, for example
78 * a LaTeX papersize, for example --paper a4paper
80 * one of --evenonly true
82 if true the outfile will contains only even pages.
83 * one of --oddonly true
85 if true the outfile will contains only odd pages.
86 * one of --reversed true
88 if true the outfile will contains only odd pages.
89 * the output page orientation, one of --orient landscape
91 * a specific name for the output file, e.g. --outfile printme.pdf
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
106 case `kpsewhich pdfpages.sty` in
107 "") echo "pdfselect: pdfpages.sty not installed"; exit 1;;
109 case `kpsewhich eso-pic.sty` in
111 "pdfselect: eso-pic.sty not installed (see the pdfpages manual)"
114 case `kpsewhich everyshi.sty` in
116 "pdfselect: everyshi.sty not installed (see the pdfpages manual)"
120 ## Now do the argument loop...
127 while test -n "${1}"; do
129 *.pdf
) inFiles
=`expr $inFiles + 1`;
130 sourcePath
="$sourcePath$newline${1}";;
131 --help) echo "$helptext";
133 --pages) pages
="${2}"
135 --paper) paper
="${2}"
137 --outfile) outFile
="${2}"
140 *) echo "pdfselect: outfile name must end in .pdf";
146 --evenonly) evenonly
="${2}"
147 if [ $oddonly == true
] ; then
148 echo "pdfselect: oddonly and evenonly must not appear together."
153 --oddonly) oddonly
="${2}"
154 if [ $evenonly == true
] ; then
155 echo "pdfselect: oddonly and evenonly must not appear together."
160 --reversed) reversed
="${2}"
162 --orient) orient
="${2}"
164 *) echo "pdfselect: unrecognised argument ${1}"; exit 1;;
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"
175 case "$sourcePath" in
176 "") echo "pdfselect: no pdf source file specified
177 For information on usage try \"pdfselect --help\""; exit 1;;
183 if test -f "$k"; then :;
184 else echo "pdfselect: ""$k"" does not exist, no files were processed";
189 if [ $subset == false
] ; then
190 if [ $pages == all
] ; then
192 if [ $reversed == true
] ; then
199 if [ $oddonly == true
] ; then
204 if [ $evenonly == true
] ; then
209 if [ $reversed == true
] ; then
215 ## That's the arguments done.
217 pwd=`pwd | sed 's/ /\\ /'`
220 ## Now work on the input file (or files in turn)
224 for inFile
in $sourcePath
226 echo "Processing $inFile..."
227 counter
=`expr $counter + 1`
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
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
246 if [ $pages == '{}' ] ; then
247 echo Selected an empty
/invalid range
for $pdfName. Skipping...
251 if [ x
$pages != x
] ; then
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"` ;
265 case "$outfileDir"/"$outfileNameRoot"".pdf" in
266 $sourceFullPath) echo "pdfselect: outfile and source cannot be the same";
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}
281 \\includepdf[fitpaper=true,pages=]{pdfname}
285 |
sed s
/pages
=/pages
="$pages"/ \
286 |
sed s
/papersize
/"$paper"/ \
287 |
sed s
/orientation
/"$orient"/ \
289 echo " Temporary LaTeX file for this job is ""$texFile"
291 ## Now run pdflatex and tidy up
293 echo " Calling pdflatex..."
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"
304 true
) rm "$tempfileDir"/"$uniqueName"* ;;
307 else echo " Failed: output file not written"