Tech Tip: Extract Pages From a PDF

July 14th, 2009 by Kurt Pfeifle in

Your rating: None Average: 4.5 (13 votes)

There are a number of ways to extract a range of pages from a PDF file: there are PDF related toolkits for doing it, or you can use Ghostscript directly.

For example, to extract pages 22-36 from a 100-page PDF file using pdftk:

  $ pdftk A=100p-inputfile.pdf cat A22-36 output outfile_p22-p36.pdf

Or use a combination of xpdf-utils (or poppler-tools) with psutils and the ps2pdf command (which ships as part of Ghostscript):

  $ pdftops 100p-inputfile.pdf - | psselect -p22-36 | \
         ps2pdf14 - outfile_p22-p36.pdf

Or, just use Ghostscript (which, unlike pdftk, is installed nearly everywhere; and you've been using it in the last command anyway):

  $ gs -sDEVICE=pdfwrite -dNOPAUSE -dBATCH -dSAFER \
       -dFirstPage=22 -dLastPage=36 \
       -sOutputFile=outfile_p22-p36.pdf 100p-inputfile.pdf

Regarding speed and efficiency of the processing and more important the quality of the output file, the 2nd method above is for sure the worst of the 3. The conversion of the original PDF to PostScript and back to PDF (also known as "refrying" the PDF) is very unlikely to completely preserve advanced PDF features (such as transparency information, font hinting, overprinting information, color profiles, trapping instructions, etc.).

The 3rd method uses Ghostscript only (which the 2nd one uses anyway, because ps2pdf14 is nothing more than a wrapper script around a more or less complicated Ghostscript commandline. The 3rd method also preserves all the important PDF objects on your pages as they are, without any "roundtrip" conversions....

The only drawback of the 3rd method is that it's a longer, more complicated command line to type. But you can overcome that drawback if you save it as a bash function. Just put these lines in your ~/.bashrc file:

function pdfpextr()
{
    # this function uses 3 arguments:
    #     $1 is the first page of the range to extract
    #     $2 is the last page of the range to extract
    #     $3 is the input file
    #     output file will be named "inputfile_pXX-pYY.pdf"
    gs -sDEVICE=pdfwrite -dNOPAUSE -dBATCH -dSAFER \
       -dFirstPage=${1} \
       -dLastPage=${2} \
       -sOutputFile=${3%.pdf}_p${1}-p${2}.pdf \
       ${3}
}

Now you only need to type (after starting a new copy bash or sourcing .bashrc) the following:

  $ pdfpextr 22 36 inputfile.pdf

which will result in the file inputfile_p22-p36.pdf in the same directory as the input file.

__________________________


Special Magazine Offer -- Free Gift with Subscription
Receive a free digital copy of Linux Journal's System Administration Special Edition as well as instant online access to current and past issues. CLICK HERE for offer

Linux Journal: delivering readers the advice and inspiration they need to get the most out of their Linux systems since 1994.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
waparmley's picture

Or Just "Print"...

On July 18th, 2009 waparmley says:

The only "printer" I use with my laptop and my netbook is cups-pdf. If I really need a paper copy of something I "print" a pdf and send it over to the one computer that is connected to a hardware printer.

So ... if I want to extract pages from a pdf I just open it in Evince then select Print from the menu, then set the page range that I want. My pages are then "printed" to a new pdf.

Seems to work fine for me -- anyone see any downside to this?

pete12's picture

Works great, but..

On July 16th, 2009 pete12 (not verified) says:

Hi, I used this function and it seems to work really well. There is only one problem that doesn't seem to effect actual performance, but when I test ran it on the linux mint user manual I got this error:

GPL Ghostscript 8.64: ERROR: A pdfmark destination page 54 points beyond the last page 3.

At first I thought it was a link leading to page 54, but is it just an index value or something of the sort?

Not that it matters much, thanks for the function!

gourgi's picture

for a gui application -> PdfShuffler

On July 14th, 2009 gourgi (not verified) says:

take a look at "PdfShuffler" ;)
http://sourceforge.net/projects/pdfshuffler/
http://davelargo.blogspot.com/2009/05/pdfshuffler-is-wonderful.html

Post new comment

Please note that comments may not appear immediately, so there is no need to repost your comment.
The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <pre> <ul> <ol> <li> <dl> <dt> <dd> <i> <b>
  • Lines and paragraphs break automatically.

More information about formatting options

Newsletter

Each week Linux Journal editors will tell you what's hot in the world of Linux. You will receive late breaking news, technical tips and tricks, and links to in-depth stories featured on www.linuxjournal.com.
Sign up for our Email Newsletter

Tech Tip Videos

From the Magazine

December 2009, #188

If last month's Infrastrucuture issue was too "big" for you then try on this month's Embedded issue. Find out how to use Player for programming mobile robots, build a humidity controller for your root cellar, find out how to reduce the boot time of your embedded system, and if you're new to embedded systems find out the basics that go into one. You can also read about the Beagle Board, the Mesh Potato and a spate of other interestingly named items. And along with our regular columns don't miss our new monthly column: Economy Size Geek.


Read this issue