1. Overview

PDFtk is a featureful utility that lets us manipulate PDF files. It was dropped from most official package repositories due to its dependency on GCJ (GNU Compiler for Java), which has been deprecated since 2016.

However, we can still use PDFtk by installing it from other sources. In addition, we can also use PDFtk forks.

In this article, we discuss how to install PDFtk on Linux. We’ll begin with installing PDFtk from the official package repositories. Then, we’ll look at alternative sources if the package isn’t available in the package repository.

2. Package Repository

PDFtk is available in most package repositories:

# Debian
$ sudo apt install -y pdftk-java

# Ubuntu and derivatives
$ sudo apt install -y pdftk

# openSUSE
$ sudo zypper install --non-interactive pdftk

# Arch Linux
$ sudo pacman -Syu --noconfirm pdftk

Notably, the package isn’t available in Fedora’s official package repository. Therefore, we’ll need to install it from alternative sources, which we’re going to see in the next sections.

3. Building From Source

PDFtk is an open-source application, which means we can build it ourselves from source code. However, it depends on several essential packages. So, let’s go ahead and install them beforehand:

# Debian and derivatives
$ sudo apt install -y git default-jdk-headless ant libcommons-lang3-java libbcprov-java

# Fedora and Red Hat
$ sudo dnf install -y git java-17-openjdk-headless ant apache-commons-lang3 bouncycastle

# Arch Linux and derivatives
$ sudo pacman -Syu --noconfirm git jre-openjdk-headless ant java-commons-lang bcprov 

Next, we clone Marc Vinyal’s fork of PDFtk:

$ git clone --depth 1 "https://gitlab.com/pdftk-java/pdftk" && cd pdftk

Then, we’ll create the lib directory to put the symlinks to the downloaded libraries. The location of these libraries vary among distributions. However, they’re usually located inside /usr/share/java on Debian-based systems:

$ ln -st /usr/share/java/commons-lang3.jar
$ ln -st /usr/share/java/bcprov.jar

Afterward, we build the source with Apache Ant:

$ ant build

Finally, we simply use the PDFtk jar:

$ java -jar build/jar/pdftk.jar --version
pdftk port to java 3.3.2 a Handy Tool for Manipulating PDF Documents
Copyright (c) 2017-2018 Marc Vinyals - https://gitlab.com/pdftk-java/pdftk

4. Snap Store

PDFtk is also available as a snap, which we can install from the Snap Store:

$ sudo snap install pdftk

We should keep in mind that the latest available version of PDFtk is quite old:

$ snap info pdftk | grep latest/stable
latest/stable:    2.02-4 2018-09-05  (9) 18MB -

If we need the latest version, we should look into the other methods.

5. Docker

Alternatively, we can use the Docker image for Ubuntu 16.04, where we can install and use PDFtk. First of all, we’ll need to install Docker.

Next, we pull the Ubuntu 16.04 image and run the shell:

$ sudo docker run -it ubuntu:16.04 bash

Then, we install the pdftk package:

$ apt update && apt install -y pdftk

Next, we commit this image to a new image with PDFtk installed:

$ sudo docker commit <CONTAINER_ID> ubuntu_pdftk

Here, we replace CONTAINER_ID with the container ID of the image. Afterward, we create a new file in /usr/bin and name it pdftk. In the file, we run PDFtk inside the ubuntu_pdftk container:

#!/bin/sh
set -eu
docker run --name pdftk -it -v "$PWD:/workdir$PWD" -w "/workdir$PWD" ubuntu_pdftk pdftk "$@"
docker rm pdftk

Finally, let’s not forget to make this executable:

$ chmod +x /usr/bin/pdftk

6. Conclusion

In this tutorial, we explored the different ways to install PDFtk on a Linux system.

Besides installing from package repositories, we also saw how to build PDFtk from source, install it from Snap Store, and run it inside a Docker image.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.