23

I have found many related questions in this forum. But, none addresses my issue. Please double-check before marking it as duplicate.

Suppose I have two PDF files.

  1. first.pdf having 10 pages.

  2. second.pdf having 20 pages.

I want to create a new PDF file, where I need pages - 2,5,6,9 from first.pdf and pages 6,7,15,19 from second.pdf.

How to do it from command line?

ddas
  • 436
  • 1
  • 5
  • 20
  • And what would be the sequence of the pages? I mean you want first all the pages extracted from first.pdf, and consequently the pages extracted from second.pdf? Or maybe you want from both pdfs pages 2,5,6,6,7,9,15,19 – lese Jun 19 '17 at 08:03
  • @lese I don't want to extract all the pages from `first.pdf`. I want a new PDF file, which would have total eight pages, having four pages (2,5,6,9) of `first.pdf` and four pages (6,7,15,19) from `second.pdf`. – ddas Jun 19 '17 at 08:45

4 Answers4

21

You can use pdfseparate command to split all pages of pdfs into single page pdf. The following command will create last_page-first_page pdfs where their name will be out_<pageNumber>:

pdfseparate -f <first_page> -l <last_page> <file_name>.pdf out_%d.pdf

Apply the process to both pdf using different output name for each input pdf, so you don't overwrite previously created single page pdfs. Then you can use pdfunite for merging selected pages into a single one pdf:

pdfunite <ordered list of pdf> <output_filename>.pdf
Danibix
  • 2,047
  • 2
  • 18
  • 27
16

You can use pdftk

in just ONE step:

pdftk A=first.pdf B=second.pdf cat A2 A5 A6 A9 B6 B7 B15 B19 output final.pdf

PS. If you need to install, you should:

sudo apt-get install pdftk
Rogelio Prieto
  • 301
  • 2
  • 6
2

As pdftk is no longer available in the repositories you could use mutool from mupdf-tools with a command like:

mutool merge -o output.pdf first.pdf 2,5,6,9 second.pdf 6,7,15,19
Stephen
  • 68
  • 4
2

Install pdftk:

sudo apt install pdftk

then, to extract "2 5 6 9" from first one in a file named "1.pdf":

pdftk first.pdf cat 2 5 6 9 output 1.pdf

and for the second.pdf:

pdftk second.pdf cat  6 7 15 19 output 2.pdf

Then merge them:

pdftk 1.pdf 2.pdf output final.pdf

And remove unnecessary ones:

rm 1.pdf 2.pdf
Ravexina
  • 54,268
  • 25
  • 157
  • 179
  • 1
    Unfortunately it seems that pdftk is not included in bionic (Ubuntu 18.04). Is there an alternative to pdftk available? I have not found it yet. https://packages.ubuntu.com/search?keywords=pdftk – holmb May 08 '18 at 09:27
  • 1
    Update: it seems that pdfseparate and pdfunite is just as good, it is found in the `poppler-utils` package. Upvoting the answer by @Danibix – holmb May 08 '18 at 09:59
  • Why is pdftk not included? – bomben Jul 18 '18 at 10:07