How to install and use PDFTK on Linux to merge or split PDF files

PDFTK is a versatile command line utility that is used to manipulate PDF documents. It has the possibility to perform simple and complex actions on PDF documents such as merging, encrypting, splitting (including removing specific pages) , decrypting, compressing, decompressing and repairing PDF files. It is also used to handle metadata, forms and watermarks. This is said, do not be put off by these functionalities as the capabilities will far outweigh the syntax.

1 – PDFTK Ubuntu Installation 

Since the PDFTK package in Ubuntu was dropped due to its dependency on the already deprecated GCJ-runtime and starting with Ubuntu 18.10 (Cosmic), PDFTK-java is now shipped from ‘default-jdk-headless’ source code . When trying to install PDFTK therefore, the package PDFTK-java will get installed instead. Users who have earlier releases of Ubuntu, can download this package from the official package repository and then install it on their systems.

Read: WorkinTool review- A Free and Practical PDF Converter

2 – Installation steps of PDFTK

  1. Install the required build tools and their dependencies by running the sudo command below:
    sudo apt install git default-jdk-headless ant \ libcommons-lang3-java libbcprov-java
    It is also possible of course to use another supported JDK than the one provided by default-jdk-headless.
  2. Visit the page Marc Vinyal’s PDFTK fork and start a git download using the command :
    git clone https://gitlab.com/pdftk-java/pdftk.git cd pdftk
  3. Put the required libraries symbolic links into the lib folder:
    mkdir lib ln -st lib /usr/share/java/{commons-lang3,bcprov}.jar
  4. Proceed to building the JAR package using the command :
    ant jar
  5. Run it by invoking the command below:
    java -jar build/jar/pdftk.jar –help

You may be interested to read: How to install and uninstall applications on Ubuntu ? A Beginner’s guide

3 – Merging files into a single document

In some circumstances you may wish to merge many PDF files into a single document. This can be achieved very easily by PDFTK by running the command below (pdftk merge pdf):

pdftk file1.pdf file2.pdf fiel3.pdf cat output single_document.pdf                      

In case you have a high number of files in your current directory you wish to merge into a single one, you could proceed as follows (pdftk command line example) :

pdftk *.pdf cat output single_document.pdf

If however you would want to use the so called handles (in order to carry out some single operations for instance on specific input files), proceed as shown below :

pdftk A=file1.pdf B=file2.pdf cat A B output output_file.pdf

4 – Splitting a pdf document

The PDFTK Linux utility has the ability also to split a file into multiple single-page files, so that each resulting file contains only one single page of the original document.This can be done by using the switch burst as shown below (pdftk split pdf) :

pdftk bigfile.pdf burst

PDFTK will then place the single-page files in the same directory as the original file .i.e. bigfile.pdf.

If you would like to have the single-page files encrypted, use the command below :

pdftk bigfile.pdf burst owner_pw [ownerpassword] allow DegradedPrinting

Provide your password in the placeholder attribute [ownerpassword]. Enable low-quality printing.

5 – Excluding pages from a PDF file

It is possible using the PDFTK tool to exclude one or more pages from a PDF file by using the command below where we want to remove for instance page 12 from input_file.pdf in order to create output_file.pdf :

pdftk input_file.pdf cat 1-11 13-end output output_file.pdf

or:

pdftk A=input_file.pdf cat A1-11 A13-end output output_file.pdf

6 – Output file encryption

In order to apply 40-bit encryption while collating many files into a single PDF file while revoking all permissions (default), apply the command below :

pdftk file1.pdf file2.pdf cat output file3.pdf encrypt_40bit owner_pw [theownerpwd]

Remember to set the owner password in the placeholder attribute theownerpwd.

To use 128 encryptions, just replace encrypt_40bit with encrypt_128bit.

Read: How to merge or split PDF files on Linux?

7 – Merging password protected files

In order to merge two files where one of them requires a password file1pwd, execute the command below :

pdftk A=securedfile1.pdf file2.pdf input_pw A=file1pwd cat output 3.pdf

Note that the output is not encrypted.

8 – Rotating PDF files

In order to rotate an entire PDF file by 180 degrees, run the command below :

pdftk input_file.pdf cat 1-endsouth output output_file.pdf

if however you would want to only rotate the first page by 90 degrees clockwise, proceed as follows :

pdftk input_file.pdf cat 1east 2-end output output_file.pdf

9 – Decrypting PDF files

In order to decrypt a password protected PDF file, run the command below :

pdftk secured_file.pdf input_pw [secured_filepwd] output unsecured_file.pdf

Remember to provide the password of the secured document in the placeholder attribute above.

10 – Attaching files

PDFTK is able to attach binary as well as text files to a PDF document with ease. It is even possible to specify the page you want the attachment to be visible on. For example:

pdftk File1.pdf attach_files file_to_attach.html to_page 20 output OuputFile.pdf

Where here we are telling PDFTK to make the attachment appear on page 20 of the final output file outputFile.pdf.


If you like the content, we would appreciate your support by buying us a coffee. Thank you so much for your visit and support.

 

amin nahdy

Amin Nahdy, an aspiring software engineer and a computer geek by nature as well as an avid Ubuntu and open source user. He is interested in information technology especially Linux based ecosystem as well as Windows and MacOS. He loves to share and disseminate knowledge to others in a transparent and responsible way.

Leave a Reply