The commandline for qpdfpdftk (suggested by iolsmit) to remove the first 2 and the last two pages from a 30-page PDF named input1.pdf
is this:
pdftk \
input1.pdf \
cat 3-28 \
output input1_p3-28.pdf
The very latest, still unreleased version of Ghostscript (which will become v. 9.06 in a few weeks) can also do it:
gs \
-o input1_p3-28.pdf \
-dFirstPage=3 \
-dLastPage=28 \
-sDEVICE=pdfwrite \
input1.pdf
To automatically determine the number of pages, you could use pdfinfo
(also available from MacPorts):
pdfinfo input1.pdf
will show the collection of embedded metadata about the PDF. Hence you could script the whole process of removing the pages. First, put the number of pages into a variable:
_endpage=$(( $(pdfinfo input1.pdf | grep Pages: | awk '{print $2}' sed 's# ##g') - 2 ))
Then, run one of the above commandlines using this variable:
pdftk input1.pdf cat 3-${_endpage} output input1_p3-${_endpage}.pdf
Just put these two lines into a Bash script called remove-my-4-pages.sh
:
#!/bin/bash
_endpage=$(( $(pdfinfo ${1}.pdf | grep Pages: | awk '{print $2}' sed 's# ##g') - 2 ))
pdftk ${1}.pdf cat 3-${_endpage} output ${2}.pdf
and run it like this:
bash remove-my-4-pages.pdf myinput.pdf myoutput.pdf
Just make sure that the pdfinfo
and pdftk
utilities are in your $PATH environment variable. Otherwise the script will not run. If you installed these utilities from MacPorts, you can put the followng line into your ~/.bash_profile
:
export PATH=/opt/local/bin:$PATH