💾 Archived View for perso.pw › blog › articles › pdftk-guide.gmi captured on 2024-05-26 at 14:43:43. Gemini links have been rewritten to link to archived content
⬅️ Previous capture (2024-02-05)
-=-=-=-=-=-=-
I often need to work with PDF, sometimes I need to extract a single page, or add a page, too often I need to rotate pages.
Fortunately, there is a pretty awesome tool to do all of these tasks, it's called PDFtk.
Pdftk command line isn't the most obvious out there, but it's not that hard.
Extracting a page requires the `cat` sub command, and we need to give a page number or a range of pages.
For instance, extracting the pages 11, and from 16 to 18 from the file my_pdf.pdf to a new file export.pdf can be done with the following command:
pdftk my_pdf.pdf cat 11 16-18 output export.pdf
Merging multiple PDFs into a single PDF also uses the sub command `cat`. In the following example, you will concatenate the PDF first.pdf and second.pdf into a merged.pdf result:
pdftk first.pdf second.pdf cat output merged.pdf
Note that they are concatenated in their order in the command line.
Pdftk comes with a very powerful way to rotate PDFs pages. You can specify pages or ranges of pages to rotate, the whole document, or only odd/even pages etc...
If you want to rotate all the pages of a PDF clockwise (east), we need to specify a range `1-end`, which means first to last page:
pdftk input.pdf rotate 1-endeast output rotated.pdf
If you want to select even or odd pages, you can add the keyword `even` or `odd` between the range and the rotation direction: `1-10oddwest` or `2-8eveneast` are valid rotations.
If you want to reverse how pages are in your PDF, we can use the special range `end-1` which will go through pages from the last to the first one, with the sub command `cat` this will only recreate a new PDF:
pdftk input.pdf cat end-1 output reversed.pdf
Pdftk have some other commands, most people will need to extract / merge / rotate pages, but take a look at the documentation to learn about all pdftk features.
PDF are usually a pain to work with, but pdftk make it very fast and easy to apply transformation on them. What a great tool :-)