How can I install pdftk in Ubuntu 18.04 and later?

Is there any chance of getting pdftk working in Ubuntu 18.04? I need this for creating PDF files with a watermark in shell.

Or, does anybody know a working alternative to pdftk to generate a PDF with a watermark in shell?

I already check/try out all of them:

sudo apt list pdf*
Listing... Done
pdf-presenter-console/bionic 4.1-2 amd64
pdf-redact-tools/bionic,bionic 0.1.2-1 all
pdf.js-common/bionic,bionic 1.5.188+dfsg-1 all
pdf2djvu/bionic 0.9.8-0ubuntu1 amd64
pdf2svg/bionic 0.2.3-1 amd64
pdfcrack/bionic 0.16-1 amd64
pdfcube/bionic 0.0.5-2build6 amd64
pdfcube-dbg/bionic 0.0.5-2build6 amd64
pdfgrep/bionic 2.0.1-1 amd64
pdfminer-data/bionic,bionic 20140328+dfsg-1 all
pdfmod/bionic,bionic 0.9.1-8 all
pdfmod-dbg/bionic,bionic 0.9.1-8 all
pdfposter/bionic,bionic 0.6.0-2 all
pdfresurrect/bionic 0.14-1 amd64
pdfsam/bionic,bionic 3.3.5-1 all
pdfsandwich/bionic 0.1.6-1 amd64
pdfshuffler/bionic,bionic 0.6.0-8 all
pdftoipe/bionic 1:7.2.7-1build1 amd64

But did not find a working tool.


The pdftk package in Ubuntu (and its upstream Debian package) was dropped due to its dependency on the now deprecated GCJ runtime. I found a fork that depends on OpenJDK or similar instead.

Install from a future Ubuntu release (recommended)

Starting with Cosmic (Ubuntu 18.10), Ubuntu ships pdftk-java from the same source code as below as a replacement. Attempting to install pdftk will install this package instead. Users of earlier releases can download it manually from the package repository and install it with their favourite package manager.

Install from PPA (outdated)

I built a Deb package (for Bionic only) with suitable dependencies:

sudo add-apt-repository ppa:malteworld/ppa
sudo apt update
sudo apt install pdftk

The package contains a wrapper script placed in /usr/bin, so you can invoke it as normally:

pdftk <arguments> ...

Install from source

  1. Install the build tools and dependencies:

    sudo apt install git default-jdk-headless ant \
        libcommons-lang3-java libbcprov-java
    

    Of course you can use a different supported JDK than the one supplied by default-jdk-headless.

  2. Download Marc Vinyal’s pdftk fork:

    git clone https://gitlab.com/pdftk-java/pdftk.git
    cd pdftk
    
  3. Place symbolic links to the required libraries into the lib folder:

    mkdir lib
    ln -st lib /usr/share/java/{commons-lang3,bcprov}.jar
    
  4. Build the JAR package:

    ant jar
    
  5. Run the JAR package:

    java -jar build/jar/pdftk.jar --help
    
  6. (Optional) To run the JAR package, e. g. when you distribute it to other systems, you need at least a working (headless) JRE like from the default-jre-headless package as well as the Java libraries libcommons-lang3-java and libbcprov-java:

    sudo apt install default-jre-headless libcommons-lang3-java libbcprov-java
    

    Again you can use a different JRE than default-jre-headless. This pdftk fork also supports builds for older JRE versions (≥ 7 according to the documentation).

  7. (Optional) You can teach Linux to execute JAR (Java Archive) files via update-binfmts(8). Most JREs shipped in Deb packages, including those in Canonical’s package repositories, take care of that during installation, though it appears to be buggy in some OpenJDK packages.

P.S.: I tried this with the non-headless OpenJDK 9 in Ubuntu Trusty but I see little reasons why it shouldn't work with headless OpenJDK 10 in Bionic.

Depending applications

A commenter raised the valid question whether the depending PDF Chain applications is affected by this change:

  • No, PDF Chain is a C++ application and not directly affected by the deprecation of GCJ. It needs a working pdftk executable but doesn’t care how it works under the hood. In any case, PDF Chain was dropped from Bionic as well as pdftk.

For Ubuntu 18.04, just install the pdftk snap package:

sudo snap install pdftk

Installing pdftk on Ubuntu 18.04 amd64

I've written a small bash script which automatise the installation on Ubuntu 18.04. Note that I've downloaded only amd64 packages!

#!/bin/bash
#
# author: abu
# date:   July 3 2019 (ver. 1.1)
# description: bash script to install pdftk on Ubuntu 18.04 for amd64 machines
##############################################################################
#
# change to /tmp directory
cd /tmp
# download packages
wget http://launchpadlibrarian.net/340410966/libgcj17_6.4.0-8ubuntu1_amd64.deb \
 http://launchpadlibrarian.net/337429932/libgcj-common_6.4-3ubuntu1_all.deb \
 https://launchpad.net/ubuntu/+source/pdftk/2.02-4build1/+build/10581759/+files/pdftk_2.02-4build1_amd64.deb \
 https://launchpad.net/ubuntu/+source/pdftk/2.02-4build1/+build/10581759/+files/pdftk-dbg_2.02-4build1_amd64.deb


echo -e "Packages for pdftk downloaded\n\n"
# install packages 
echo -e "\n\n Installing pdftk: \n\n"
sudo apt-get install ./libgcj17_6.4.0-8ubuntu1_amd64.deb \
    ./libgcj-common_6.4-3ubuntu1_all.deb \
    ./pdftk_2.02-4build1_amd64.deb \
    ./pdftk-dbg_2.02-4build1_amd64.deb
echo -e "\n\n pdftk installed\n"
echo -e "   try it in shell with: > pdftk \n"
# delete deb files in /tmp directory
rm ./libgcj17_6.4.0-8ubuntu1_amd64.deb
rm ./libgcj-common_6.4-3ubuntu1_all.deb
rm ./pdftk_2.02-4build1_amd64.deb
rm ./pdftk-dbg_2.02-4build1_amd64.deb

This script will download the packages to /tmp and install from there using an apt installcommand! Afterwards the packages in the /tmp directory will be removed.

To run this script, copy it in an editor and save it e.g. pdftk_installer. Then run it in a terminal with

chmod 755 pdftk_installer
./pdftk_installer

Installing pdftk on Ubuntu 20.04 amd64

The script above will fail due to missing gcc-6 libraries on Ubuntu 20.04. However, those who would like to avoid the snap or docker solution may use schroot. I know that this is a pretty overload - it takes about 500MB; but you can use this environment to install further elderly programs, libs, compilers, etc. in the Xenial (Ubuntu 16.04) environment.

First install the schroot package

 sudo apt install schroot debootstrap

The last package is required to install a debian-like-system. Now write a xenial.conf file into the /etc/schroot/schroot.d directory:

cat <<EOF | sudo tee /etc/schroot/chroot.d/xenial.conf
> [xenial]
> description=Ubuntu 16.04
> directory=/srv/chroot/xenial
> root-users=$USER
> type=directory
> users=$USER
> EOF

Verify that the new conf-file is written

cat /etc/schroot/chroot.d/xenial.conf

Next create the xenial directory (if you choose another directory alter the conf file above):

 sudo mkdir -p /srv/chroot/xenial

Now the show begins, while installing go for a coffee:

sudo debootstrap xenial /srv/chroot/xenial 

The xenial file system is now available on /srv/chroot/xenial.

Now it's time to include the necessary xenial apt repositories. To do so type

cat <<EOF | sudo tee /srv/chroot/xenial/etc/apt/sources.list
deb http://archive.ubuntu.com/ubuntu xenial main restricted universe multiverse
deb http://security.ubuntu.com/ubuntu xenial-security main restricted universe multiverse
EOF

Again check that the file /srv/chroot/xenial/etc/apt/sources.list exists

cat /srv/chroot/xenial/etc/apt/sources.list

It's time to enter the xenial schroot (note: no root privileges needed!) and update the apt

schroot -c xenial -u root apt-get update

Finally you are ready to install the pdftk package:

schroot -c xenial -u root apt-get install pdftk

You can use pdftk by invoking it via the schroot:

schroot -c xenial -- pdftk <your arguments>

E.g. to show the help menu type

schroot -c xenial -- pdftk --help

Last of all I suggest to make an alias for pdftk in your .bashrc file:

First make a copy of your .bashrc

cp ~/.bashrc ~/.bashrc_backup

and then add an alias

echo alias pdftk='schroot -c xenial -- pdftk' >> ~/.bashrc

Now you can use pdf as usual in your bash. To test open a new terminal and type

 pdftk --version

If you have to install other older programs as I had to do, use the apt commands similar as described above.

Note that all commands beside the apt-get and the xenial.config are executed with user privileges.


This works for ubuntu 18.04:

sudo add-apt-repository ppa:malteworld/ppa
sudo apt update
sudo apt install pdftk