Create a PDF with specific page ranges of multiple PDF files?

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?


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

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

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