Tech Tip: Extract Pages From a PDF
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.
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Sponsored by AMD
Built-in forensics, incident response, and security with Red Hat Enterprise Linux 6
Every security policy provides guidance and requirements for ensuring adequate protection of information and data, as well as high-level technical and administrative security requirements for a system in a given environment. Traditionally, providing security for a system focuses on the confidentiality of the information on it. However, protecting the data integrity and system and data availability is just as important. For example, when processing United States intelligence information, there are three attributes that require protection: confidentiality, integrity, and availability.
Learn more about catching the bad guy in this free white paper.
Sponsored by DLT Solutions
| Designing Electronics with Linux | May 22, 2013 |
| Dynamic DNS—an Object Lesson in Problem Solving | May 21, 2013 |
| Using Salt Stack and Vagrant for Drupal Development | May 20, 2013 |
| Making Linux and Android Get Along (It's Not as Hard as It Sounds) | May 16, 2013 |
| Drupal Is a Framework: Why Everyone Needs to Understand This | May 15, 2013 |
| Home, My Backup Data Center | May 13, 2013 |
- RSS Feeds
- Dynamic DNS—an Object Lesson in Problem Solving
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- Designing Electronics with Linux
- Using Salt Stack and Vagrant for Drupal Development
- New Products
- A Topic for Discussion - Open Source Feature-Richness?
- Drupal Is a Framework: Why Everyone Needs to Understand This
- Validate an E-Mail Address with PHP, the Right Way
- What's the tweeting protocol?
- Kernel Problem
7 hours 17 min ago - BASH script to log IPs on public web server
11 hours 44 min ago - DynDNS
15 hours 20 min ago - Reply to comment | Linux Journal
15 hours 52 min ago - All the articles you talked
18 hours 16 min ago - All the articles you talked
18 hours 19 min ago - All the articles you talked
18 hours 20 min ago - myip
22 hours 45 min ago - Keeping track of IP address
1 day 36 min ago - Roll your own dynamic dns
1 day 5 hours ago
Enter to Win an Adafruit Pi Cobbler Breakout Kit for Raspberry Pi

It's Raspberry Pi month at Linux Journal. Each week in May, Adafruit will be giving away a Pi-related prize to a lucky, randomly drawn LJ reader. Winners will be announced weekly.
Fill out the fields below to enter to win this week's prize-- a Pi Cobbler Breakout Kit for Raspberry Pi.
Congratulations to our winners so far:
- 5-8-13, Pi Starter Pack: Jack Davis
- 5-15-13, Pi Model B 512MB RAM: Patrick Dunn
- 5-21-13, Prototyping Pi Plate Kit: Philip Kirby
- Next winner announced on 5-27-13!
Free Webinar: Hadoop
How to Build an Optimal Hadoop Cluster to Store and Maintain Unlimited Amounts of Data Using Microservers
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Some of key questions to be discussed are:
- What is the “typical” Hadoop cluster and what should be installed on the different machine types?
- Why should you consider the typical workload patterns when making your hardware decisions?
- Are all microservers created equal for Hadoop deployments?
- How do I plan for expansion if I require more compute, memory, storage or networking?



Comments
You should do:
You should do:
-sOutputFile="${3%.pdf}_p${1}-p${2}.pdf" \
"${3}"
or it won't work with filenames with spaces
Hi, i tried the first one,
Hi, i tried the first one, and didn't work, only work if you write it like this:
$ pdftk book.pdf cat 22-26 output book2.pdf
i'm using Ubuntu karmic by
i'm using Ubuntu karmic by the way
and sorry my bad english, i'm spanish
pdftk is fantastic
Hello,
Thank you for the great article, it exactly answered to the question I was having for some time now.
I tried solution n°1 and it works perfectly (on Windows 7). My problem was to split into single pages a large PDF document and a
pdftk a.pdf burst output "hello world %d.pdfmade my day.
Thanks!
Wojtek
Or Just "Print"...
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?
Works great, but..
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!
Probably a bogus error message
This is probably a bogus error message (should rather be a warning). If you check your output, most likely everything is there.
for a gui application -> PdfShuffler
take a look at "PdfShuffler" ;)
http://sourceforge.net/projects/pdfshuffler/
http://davelargo.blogspot.com/2009/05/pdfshuffler-is-wonderful.html