PDFTK – How to Install PDFTK in Ubuntu 18.04 and Later

18.04pdfpdftksoftware installation

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.

Best Answer

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.
Related Question