PDF Manipulation with pdftk
Posted on Fri 24 January 2025 in Developer Tools • 2 min read
pdftk (PDF Toolkit) is a command-line tool for manipulating PDF files. Here are the most common operations.
Installation
# Ubuntu/Debian
sudo apt install pdftk
# Or pdftk-java on newer systems
sudo apt install pdftk-java
Merge PDFs
Combine multiple PDFs into one:
pdftk file1.pdf file2.pdf cat output merged.pdf
Merge all PDFs in a directory:
pdftk *.pdf cat output combined.pdf
Extract Pages
Extract specific pages from a PDF:
# Extract pages 12 through 15
pdftk input.pdf cat 12-15 output pages_12-15.pdf
# Extract single page
pdftk input.pdf cat 5 output page_5.pdf
# Extract first 10 pages
pdftk input.pdf cat 1-10 output first_10.pdf
# Extract last page (use 'end')
pdftk input.pdf cat end output last_page.pdf
Rotate Pages
Rotate pages using cardinal directions:
| Direction | Rotation |
|---|---|
north |
0 (no rotation) |
east |
90 clockwise |
south |
180 |
west |
270 (90 counter-clockwise) |
Relative rotations:
| Direction | Rotation |
|---|---|
left |
-90 (counter-clockwise) |
right |
+90 (clockwise) |
down |
+180 |
Examples:
# Rotate all pages 90 degrees clockwise
pdftk input.pdf cat 1-endeast output rotated.pdf
# Rotate all pages 90 degrees counter-clockwise
pdftk input.pdf cat 1-endwest output rotated.pdf
# Rotate only page 3 clockwise
pdftk input.pdf cat 1-2 3east 4-end output rotated.pdf
Split PDF into Single Pages
pdftk input.pdf burst output page_%02d.pdf
Creates page_01.pdf, page_02.pdf, etc.
Combine Operations
Merge specific pages from multiple files:
# Pages 1-5 from A, pages 10-15 from B
pdftk A=first.pdf B=second.pdf cat A1-5 B10-15 output combined.pdf
View PDF Metadata
pdftk input.pdf dump_data
Quick Reference
| Task | Command |
|---|---|
| Merge files | pdftk a.pdf b.pdf cat output merged.pdf |
| Extract pages 5-10 | pdftk in.pdf cat 5-10 output out.pdf |
| Rotate clockwise | pdftk in.pdf cat 1-endeast output out.pdf |
| Rotate counter-clockwise | pdftk in.pdf cat 1-endwest output out.pdf |
| Split into pages | pdftk in.pdf burst |
pdftk handles most PDF manipulation tasks without needing heavyweight applications or online services.