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

13,214

Solution 1

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

Solution 2

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

Solution 3

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

Solution 4

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
Share:
13,214

Related videos on Youtube

ddas
Author by

ddas

Updated on September 18, 2022

Comments

  • ddas
    ddas over 1 year

    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?

    • lese
      lese almost 7 years
      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
    • ddas
      ddas almost 7 years
      @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.
  • holmb
    holmb about 6 years
    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. packages.ubuntu.com/search?keywords=pdftk
  • holmb
    holmb about 6 years
    Update: it seems that pdfseparate and pdfunite is just as good, it is found in the poppler-utils package. Upvoting the answer by @Danibix
  • bomben
    bomben almost 6 years
    Why is pdftk not included?
  • Juan Ignacio Barisich
    Juan Ignacio Barisich over 3 years
    I had this error: Unimplemented Feature: Could not merge encrypted files. Was solved using an intermediate command: qpdf --decrypt <source.pdf> <unencrypted.pdf> Thanks @Danibix