I want to exctact pages from a pdf document such that all pages are extracted except the first one and the last one. Code but (end-1) does not work, nor 2-end-1

pdftk 1.pdf cat 2-(end-1) output output.pdf
pdftk yourpdfile.pdf cat 2-r2 output out.pdf
  • Confirmed! This one works! – Léo Léopold Hertz 준영 Mar 31 '17 at 9:18
  • 3
    Docs: You can reference page numbers in reverse order by prefix‐ ing them with the letter r. For example, page r1 is the last page of the document, r2 is the next-to-last page of the doc‐ ument, and rend is the first page of the document. You can use this prefix in ranges, too, for example r3-r1 is the last three pages of a PDF. – Léo Léopold Hertz 준영 Mar 31 '17 at 9:20

You can define a range and remove pages from it (using ~) to achieve this:

pdftk 1.pdf cat 2-end~end output output.pdf

This tells pdftk cat to take the pages from the second page to the last page included (2-end), and remove the last page from the range (~end). The result is a PDF containing everything apart from the first and last pages of the original.

  • @Assume you need to remove last page and one before last page i.e. two pages at the end. Is that possible with your approach? – Léo Léopold Hertz 준영 Mar 31 '17 at 9:06
  • No, not with this approach; you need Rakesh's reverse approach for that. – Stephen Kitt Mar 31 '17 at 9:20

You can use the following format:

pdftk A=1.pdf cat A2-(n-1) output 'outputfile_p2-p(n-1).pdf'

You ca determine n through :pdftk 1.pdf dump_data | grep NumberOfPages

e,g: n=8

pdftk A=1.pdf cat A2-7 output 'outputfile_p2-p7.pdf'
  • 1
    How do you determine n? – Stephen Kitt Mar 31 '17 at 9:00
  • 1
    A pdf document with n pages – GAD3R Mar 31 '17 at 9:01
  • I get bash: syntax error near unexpected token ('` with your syntax. What is your shell? – Léo Léopold Hertz 준영 Mar 31 '17 at 9:04
  • @LéoLéopoldHertz준영 the /bin/bash – GAD3R Mar 31 '17 at 9:06
  • 1
    I meant programmatically... – Stephen Kitt Mar 31 '17 at 9:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.