How to use the command pdftk (with examples)
The pdftk
command is a PDF toolkit that allows you to perform various operations on PDF files. It is a versatile tool that can be used for tasks such as extracting pages, merging multiple PDF files, splitting pages into separate files, and rotating pages.
Use case 1: Extracting specific pages from a PDF file
Code:
pdftk input.pdf cat 1-3 5 6-10 output output.pdf
Motivation: You may want to extract specific pages from a PDF file and save them as a separate PDF file. This can be useful if you only need certain pages from a larger document.
Explanation:
input.pdf
: Specifies the input PDF file from which the pages will be extracted.cat
: Short for “catenate”, this command instructspdftk
to concatenate the specified pages.1-3 5 6-10
: Specifies the range of page numbers to be extracted. In this example, pages 1 to 3, page 5, and pages 6 to 10 will be included.output output.pdf
: Specifies the output file name for the extracted pages. In this case, the extracted pages will be saved asoutput.pdf
.
Example output:
The specified pages (1-3, 5, and 6-10) from the input.pdf
file will be extracted and saved as output.pdf
.
Use case 2: Merging multiple PDF files
Code:
pdftk file1.pdf file2.pdf ... cat output output.pdf
Motivation: You may have multiple PDF files that you want to combine into a single PDF file. This can be useful for organizing related documents or creating a comprehensive report.
Explanation:
file1.pdf file2.pdf ...
: Specifies the input PDF files that will be merged together. You can specify as many files as needed, separated by a space.cat
: Short for “catenate”, this command instructspdftk
to concatenate the specified files.output output.pdf
: Specifies the output file name for the merged PDF. In this case, the merged PDF will be saved asoutput.pdf
.
Example output:
The contents of file1.pdf
, file2.pdf
, and any additional PDF files specified will be merged together and saved as output.pdf
.
Use case 3: Splitting pages of a PDF file into separate files
Code:
pdftk input.pdf burst output out_%d.pdf
Motivation: You may want to split each page of a PDF file into individual files. This can be useful for further processing or organizing the pages individually.
Explanation:
input.pdf
: Specifies the input PDF file that will be split.burst
: This command instructspdftk
to burst the PDF file into separate pages.output out_%d.pdf
: Specifies the output pattern for the separate pages. In this case, the pages will be saved asout_1.pdf
,out_2.pdf
,out_3.pdf
, and so on, with%d
representing the page number.
Example output: The input PDF file will be split into separate pages, and each page will be saved as a separate PDF file following the specified output pattern.
Use case 4: Rotating all pages of a PDF file
Code:
pdftk input.pdf cat 1-endsouth output output.pdf
Motivation: You may need to rotate all pages of a PDF file by a certain degree. This can be useful if the pages are oriented incorrectly or if you want to change the orientation of the entire document.
Explanation:
input.pdf
: Specifies the input PDF file that will be rotated.cat
: This command instructspdftk
to concatenate the pages.1-endsouth
: Specifies the page range to rotate. In this example, all pages from 1 to the end of the document will be rotated by 180 degrees clockwise.output output.pdf
: Specifies the output file name for the rotated PDF. In this case, the rotated PDF will be saved asoutput.pdf
.
Example output:
All pages of the input PDF file will be rotated by 180 degrees clockwise, and the resulting document will be saved as output.pdf
.
Use case 5: Rotating specific pages of a PDF file
Code:
pdftk input.pdf cat 1-2 3east 4-end output output.pdf
Motivation: You may want to rotate specific pages of a PDF file while leaving others unchanged. This can be useful for correcting the orientation of individual pages within a document.
Explanation:
input.pdf
: Specifies the input PDF file that will be rotated.cat
: This command instructspdftk
to concatenate the pages.1-2
: Specifies the range of pages (1 and 2) that will not be rotated.3east
: Specifies the page number (3) that will be rotated by 90 degrees clockwise.4-end
: Specifies the range of pages (4 to the end of the document) that will not be rotated.output output.pdf
: Specifies the output file name for the rotated PDF. In this case, the rotated PDF will be saved asoutput.pdf
.
Example output:
Page 3 of the input PDF file will be rotated by 90 degrees clockwise, while pages 1, 2, and any remaining pages will remain unchanged. The resulting document will be saved as output.pdf
.
Conclusion:
The pdftk
command is a powerful PDF toolkit that provides various capabilities for manipulating PDF files. Whether you need to extract specific pages, merge multiple files, split pages, or rotate pages, pdftk
offers a versatile solution. By understanding and utilizing the different use cases illustrated above, you can effectively leverage pdftk
to handle your PDF-related tasks.