75

I would like to extract page ranges from a PDF document into a new PDF document using the command line in Linux. Note that:

$ pdftk input.pdf cat 1 verbose output output.pdf
Error: Failed to open PDF file: 
   input.pdf
Errors encountered.  No output created.
Done.  Input errors, so no output created.

From here:

You (should) know that Pdftk is nothing more than a very old version of iText (a Java-PDF library) compiled with GCJ and extended with some command line functionality.

The keywords in the above statement are "VERY OLD".

$ java -classpath /path/to/Multivalent20091027.jar tool.pdf.Split -page 1 input.pdf
Exception in thread "main" java.lang.NoClassDefFoundError: tool/pdf/Split
Caused by: java.lang.ClassNotFoundException: tool.pdf.Split
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Could not find the main class: tool.pdf.Split.  Program will exit.

Turns out, this is a bit of a tricky software: even if it's on SourceForge, and says here that

Practical Thought generously provides these tools for free use on the command line

However, here it says:

The browser is open source. The document tools are a free bonus and not open source.

Which finally clarifies the comment from conversion - Gluing (Imposition) PDF documents - Stack Overflow:

All releases of Multivalent linked from the official sourceforge site are missing the tools package.

(edit: there seems to be an old Multivalent version with the tools included, see the SO link; but as it looks somewhat like abandonware, I'd rather not use it)

Finally, I'd like to avoid tools that are essentially front ends for LaTeX like pdfjam.

Are there any options for such a PDF splitting command line tool under Linux?

2
  • 4
    Qpdf can split PDFs. For example, to split a PDF into groups of two pages, do: qpdf --split-pages=2 in.pdf out-%d.pdf, see this answer for more. To extract a range of pages, 2 to 5 in this example: qpdf --empty --pages in.pdf 2-5 -- out.pdf, see also this. Sep 13, 2021 at 11:12
  • 1
    @MatthiasBraun Why is this a comment and not a top-rated answer? Mar 1, 2023 at 18:58

5 Answers 5

100

I find pdfseparate very convenient to split ranges into individual pages. You can extract all pages into files named output-page1.pdf, output-page2.pdf, like this,

pdfseparate input.pdf output-page%d.pdf

And you can extract pages 1 - 5 of input.pdf by using the first-page and last-page flags, -f 1 -l 5,

pdfseparate -f 1 -l 5 input.pdf output-page%d.pdf

If you want to recombine them into page ranges, for example pages 1-3 in one document and pages 4-5 in another, you can use the companion program, pdfunite, as follows:

pdfunite output-page1.pdf output-page2.pdf output-page3.pdf final-pages1-3.pdf
pdfunite output-page4.pdf output-page5.pdf final-pages4-5.pdf

I believe theese tools are part of poppler and may already be installed on your system.

4
  • Great solution, but: The resulting file size of the split results can sometimes be identical to the whole file. A solution to this is found in this comment. In my experience, a pdf file created with pdflatex that contained many images using the \includepdf{} command have caused this problem. The solution in the linked comment works great.
    – Martin
    Sep 30, 2020 at 9:31
  • Quote from the comment linked by @Ok_Letsdothis. << [F]irst, "optimiz[e]" the PDF with Ghostscript: gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dNOPAUSE -dQUIET -dBATCH -sOutputFile=doc-compressed.pdf doc.pdf, after which pdfseparate lead [sic] to the expected size reduction. >> May 10, 2021 at 18:50
  • Probably not too useful to write your sample command such that it extracts 1-5. That's going to be arbitrary to users that find this page, which are far more likely to want all the pages. Jun 29, 2023 at 1:26
  • +1 because pdfseparate comes preinstalled with TeX Live for me
    – Jake Levi
    Oct 10, 2023 at 0:52
23

Using pdftk 2.02 worked for me on debian, but I think it should work for you too.

pdftk input.pdf cat 2-4 output out1.pdf

For a general case where you have to split a single pdf to multiple files I could not find a way with pdftk, so I'm using a Bash script.

8

You can use the pdfjam tool with the syntax

pdfjam <input-file> <page-ranges> -o <output-file>

and an example of page ranges would be

3,67-70,80

Source: https://tex.stackexchange.com/questions/79623 by Vincent Nivoliers

4

I'll put this as an answer, so as not to clog the question: here is a related link on unix.se:

... and the accepted answer uses a Python script with PyPDF (but that answer implements a split of one page into two - and that script thus needs to be modified for page ranges, for it to work as asked in OP).

 

EDIT: I just found this: Stapler - A python utility for manipulating PDF docs based on pypdf (Page 3) / Community Contributions / Arch Linux Forums; which is, apparently "A small utility making use of the pypdf library to provide a (somewhat) lighter alternative to pdftk" (note that the mailing list notes some problems with it, however)...

0

You can use pdfly:

  • Installation: pip install pdfly and more
  • Usage: pdfly cat in.pdf 2:4 -o out.pdf and more

Some details:

 Page ranges refer to the previously-named file. A file not followed by a page
 range means all the pages of the file.
 PAGE RANGES are like Python slices.
 Remember, page indices start with zero.
 Page range expression examples:

    :     all pages.
    -1    last page.
    22    just the  23rd page.
    :-1   all but the last page.
    0:3   the first   three pages.
    -2    second-to-last page.
    :3    the first      three pages.
    -2:   last two pages.
    5:    from the sixth page onward.
    -3:-1 third & second to last.

 The third, "stride" or "step" number is also recognized.

    ::2       0 2 4 ... to the end.
    3:0:-1    3 2 1 but not 0.
    1:10:2    1 3 5 7 9
    2::-1     2 1 0.
    ::-1      all  pages in reverse order.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .