QPDF is a command-line tool that excels in PDF structure manipulation. It is not a rendering or viewing tool like pdftk, but a powerful utility for tasks such as page extraction, rearrangement, and merging—ideal for scripting and automation.

In this post, I demonstrate common QPDF use cases using file1.pdf and file2.pdf.


1. Extract a Range of Pages from file1.pdf

To extract pages 3 to 7 from file1.pdf:

qpdf file1.pdf --pages . 3-7 -- output.pdf

Here, . refers to the current input file (file1.pdf). The result is saved in output.pdf.

To extract pages 3 to 7, 9 and 10 from file1.pdf:

qpdf file1.pdf --pages . 3-7,9,10 -- output.pdf

2. Rearrange Pages in file1.pdf

Suppose you want to reorder the pages of file1.pdf to appear in the order 5, 1, 3:

qpdf file1.pdf --pages . 5 1 3 -- reordered.pdf

This command creates reordered.pdf with pages in the specified order.


3. Combine file1.pdf and file2.pdf into a New File

To merge all pages of file1.pdf and file2.pdf sequentially:

qpdf --empty --pages file1.pdf file2.pdf -- combined.pdf

The --empty option starts with a blank PDF, into which all pages are inserted.


4. Combine a Range of Pages from Each PDF

Extract pages 2–4 from file1.pdf and pages 1–3 from file2.pdf, and merge them:

qpdf --empty --pages file1.pdf 2-4 file2.pdf 1-3 -- subset_combined.pdf

This creates subset_combined.pdf with selected page ranges.


5. Remove Specific Pages

To remove page 2 (keep pages 1 and 3 to the end):

qpdf file1.pdf --pages . 1,3-r1 -- output.pdf

To remove pages 4 to 7 (keep pages 1–3 and 8 to the end):

qpdf file1.pdf --pages . 1-3,8-r1 -- output.pdf

In both cases, r1 represents the last page of the file.


Additional Use Cases

Other useful QPDF scenarios include:

  • Rotate pages (e.g., rotate page 1 by 90°):

    qpdf file1.pdf --rotate=+90:1 -- rotated.pdf
    
  • Split all pages into individual files (requires scripting):

    qpdf file1.pdf --split-pages
    
  • Encrypt/Decrypt PDFs:

    qpdf --encrypt user-password owner-password 256 -- file1.pdf encrypted.pdf
    

QPDF is scriptable, fast, and well-suited for batch processing. Whether you’re automating PDF workflows or performing one-off edits, it’s a reliable addition to your toolbox.