How can I install pdftk in Ubuntu 18.04 and later?

97,183

Solution 1

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.

Solution 2

For Ubuntu 18.04, just install the pdftk snap package:

sudo snap install pdftk

Solution 3

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.

Solution 4

This works for ubuntu 18.04:

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

Solution 5

You can try use a docker image of Ubuntu 16.04 with pdftk installed to run pdftk:

  1. Install docker:

    sudo apt install docker.io
    
  2. Pull Ubuntu 16.04 and run a bash shell:

    sudo docker run -it ubuntu:16.04 bash
    
  3. Update and install pdftk from container prompt:

    apt update
    apt install pdftk
    
  4. On a new terminal run:

    sudo docker ps -a
    
  5. Commit the image using the CONTAINER ID of ubuntu:16.04 to a new image with pdftk installed:

    sudo docker commit CONTAINER_ID ubuntu_pdftk
    

    (Replace CONTAINER_ID with your container ID.)

  6. Create a file named pdftk in /usr/bin and then make it executable using chmod +x /usr/bin/pdftk:

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

Related videos on Youtube

WiKrIe
Author by

WiKrIe

Updated on September 18, 2022

Comments

  • WiKrIe
    WiKrIe over 1 year

    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.

    • Joshp.23
      Joshp.23 about 6 years
      and PDF Chain, too! These two tools were invaluable to me!
    • DK Bose
      DK Bose about 6 years
      See bugs.launchpad.net/ubuntu/+source/pdftk/+bug/1764450 and mark the bug as affecting you as well.
    • Ademir F Furtado
      Ademir F Furtado about 6 years
      You can try use a docker image of ubuntu 16:04 with pdftk installed to run pdftk.
    • ADDB
      ADDB about 6 years
      Don't just write "You can try...", explain how you do it and the steps you have to follow to make the answer more helpful
    • WiKrIe
      WiKrIe about 6 years
      thats a nice idea, will try it right now ... this sounds more usefull than install pdftk from artful packages.
    • WiKrIe
      WiKrIe about 6 years
      if it is needed I could make a short tutorial how I get the docker version up and running.
    • David Foerster
      David Foerster about 6 years
      If you solved your problem yourself, please answer your own question and accept your answer. Don’t put the answer in your question or the comments! :-) I took the liberty to revert the change that added the answer but you can always review a post’s history through the link below it.
    • WiKrIe
      WiKrIe about 6 years
      I already do a short answer as Update2 into my original Question, I only ask if anybody needs a detailed step by step tutorial for this.
    • David Foerster
      David Foerster about 6 years
      FYI, pdftk was dropped from the repositories and there's a feature request to add it back.
    • Daniel Alder
      Daniel Alder almost 6 years
      it's a shame that such a nice tool got removed just because the developers didn't find an acceptable solution
  • CalvT
    CalvT about 6 years
    Hi Christian, would you mind updating your answer to include what your workaround is? We prefer if you include the essential parts of the answer here and then provide the link for reference. Thanks!
  • terdon
    terdon about 6 years
    Yes, especially since the site you linked to seems to be down.
  • WiKrIe
    WiKrIe about 6 years
    Hi CalvT, the workaround I use is simply add the artfull packages to apt, install pdftk and remove them. And terdon my site was not down within the last 30 days so I do not know why you should not access the site.
  • Hee Jin
    Hee Jin almost 6 years
    I was reading another question about installing a different package that was dropped from the repos for 18.04, and one user suggested installing the .deb package using gdebi, which should handle dependencies. Do you think that would work or would it create some problems? That may be a dumb question--I'm just trying to understand more about package management.
  • Dɑvïd
    Dɑvïd almost 6 years
    @WiKrIe There is a problem with your site. I can reach the base URL, but not the page you linked. Neither can Wayback Machine. Google cache won't load it, but will load the source. There must be a problem on that page somewhere. So that's why terdon♦ thought your site was down. So did I until I checked. Hope that helps.
  • WiKrIe
    WiKrIe almost 6 years
    @David I check my site right now, and for me all pages are online, but I see that there is maybe a block from your IP range, because I use fail2ban to block out bad IP's so if you would send me your IP I could check if I can find it in any jail and why it is in that jail. That would be the only explain from my side.
  • David Foerster
    David Foerster almost 6 years
    -1 for the suggestion to mix repositories meant for different Ubuntu releases without proper priority rules.
  • Ondra Žižka
    Ondra Žižka almost 6 years
    Almost exactly what I did. Except, don't you have a mistake in -v "$PWD:/workdir$PWD" -w "/workdir$PWD" ?
  • user485805
    user485805 almost 6 years
    Symlinking didn't work for me, because snap seems to need the script name. But a minimal wrapper script /snap/bin/pdftk-smoser.pdftk "$@" works equally well.
  • pgoetz
    pgoetz almost 6 years
    What version of Ubuntu? The precise instructions I give above worked for me. I call pdftk from some perl xml processing scripts and the acid test is the scripts work and produce merged pdf's.
  • user485805
    user485805 almost 6 years
    Kubuntu 18.04, just upgraded from 16.04 and found pdftk missing. When I do the symlink, running pdftk in bash yields snaps help message. ¯\_(ツ)_/¯
  • pgoetz
    pgoetz almost 6 years
    Make sure the snap daemon is running.
  • smoser
    smoser almost 6 years
    i updated the answer above to use just 'pdftk' rather than 'pdftk' as I (smoser) have uploaded a snap named 'pdftk' to the store with the same content. (snapcraft.io/pdftk)
  • cypherabe
    cypherabe almost 6 years
    can you (@pgoetz or @smoser) expand a bit on what additional steps are necessary? I needed to symlink the snap to /usr/local/bin/pdftk to get the command working in the bash and still have trouble accessing a pdf in /var/www/some/path, even if I reinstall the snap in devmode
  • pgoetz
    pgoetz almost 6 years
    The symlink is just a path issue I suspect. You want to do this in any case for convenience sake. I'm not sure why you'd have trouble accessing a pdf in /var/www/* though -- have you checked to make sure this isn't just a permissions problem? What happens when you try to open a pdf from these locations? I seem to remember having left a much more detailed answer, which smoser appears to have edited. Maybe he can chime in on this as well. One would think that at the very least you'd want the symlink to a common path location.
  • scoobydoo
    scoobydoo almost 6 years
    This seemed to install fine but then pdftk would not open or work on any files (always says 'Error: Unable to find file. Error: Failed to open PDF file:'), also has no man page. In the end I removed it with snap remove and went with @abu_bua solution above, which works perfectly.
  • Daniel Martin
    Daniel Martin almost 6 years
    This snap-installed version of pdftk doesn't seem to work with files owned by a user who is not the user running pdftk, even if those files are set world readable. I went with the PPA by "David Foerster" mentioned above and it worked better. (Though I still have issues with using the file "-")
  • smoser
    smoser almost 6 years
    Due to design points of snaps, a snap cannot access all files. Some information on this is available at github.com/smoser/pdftk/issues/1 . The easiest solution is to put files in your home directory.
  • Timo Jyrinki
    Timo Jyrinki over 5 years
    Thank you, I did snap install pdftk on Ubuntu 18.04 LTS and it's working perfectly out-of-the-box!
  • tanius
    tanius over 5 years
    Both sets of instructions in this answer install Marc Vinyal’s pdftk-java fork. That will be (very probably) the official replacement for pdftk in Debian (see) and in Ubuntu (with a package available for 18.10). So this seems to be the best answer, as it is "future proof": from 18.10 on, you'll get the same software served in the official repos.
  • SYK
    SYK over 5 years
    /snap/bin/whatever would work for a while and break with all kinds of permission denied problems on Ubuntu 18.04. For pdftk, the error is java.io.FileNotFoundException. The list of failure for other apps is endless. I had to remove virtually all snap apps and replace them with apt. Snap is the worst thing happening on Ubuntu 18.04 at the moment.
  • Jossef Harush Kadouri
    Jossef Harush Kadouri over 5 years
    looks like pdftk-dbg_2.02-4build1_amd64.deb is optional
  • Raphael
    Raphael over 5 years
    @DavidFoerster On it. Question: Is there a particular reason for recommending ant with libraries installed via apt over using Gradle for the build?
  • Raphael
    Raphael over 5 years
    At a follow-up question regarding said dummy packages, doubts arose as to whether the dependencies of pdftk-java are well-chose. You may want to check that out.
  • Raphael
    Raphael over 5 years
    I created package descriptions for equivs that provide Java installed by SDKMAN! as a package that fulfills the dependencies of pdftk-java.
  • simon
    simon over 5 years
    This is much preferable to the snap version or the java-based version from the PPA -- thanks!
  • Randall Whitman
    Randall Whitman about 5 years
    To offer some explanation, this appears to download the Ubuntu-artful (17.10) packages.
  • HJLebbink
    HJLebbink almost 5 years
    The links for libgcj and libgcj17 are broken.
  • Eduard Florinescu
    Eduard Florinescu almost 5 years
    @HJLebbink Ubuntu no longer keeps artful :( libraries links
  • Developer
    Developer almost 5 years
    All Links is broken
  • abu_bua
    abu_bua almost 5 years
    links updated now!
  • Geppettvs D'Constanzo
    Geppettvs D'Constanzo over 4 years
    Yes. It does. And this solves an issue on Inkscape when you try to use the olibia/inkscape-multipage-export (github.com/olibia/inkscape-multipage-export)
  • AVK
    AVK over 4 years
    Great solution. PPA doesnt always work and snap is too restrictive
  • Divyesh Prajapati
    Divyesh Prajapati about 4 years
    This worked well for me in 18.04
  • Noah Duncan
    Noah Duncan about 4 years
    Did not work for me as of 5/1/2020. The PPA maintainer has stated it will go away, maybe that time has come.
  • senty
    senty almost 4 years
    Great solution! Thanks
  • abu_bua
    abu_bua almost 4 years
    @senty: I think the best solution, which may also work in future distributions, is to use the snap pdftk, or even the dock container solution.
  • senty
    senty almost 4 years
    snap pdftk didn't work for me; and removing it messed up symlinks
  • Krzysztof Drabik
    Krzysztof Drabik over 3 years
    The script installed pdftk perfectly on two separate occasions, the best answer by far
  • Nathan Chappell
    Nathan Chappell over 3 years
    got an upvote for the Test with line. That's the functionality I wanted, probably all I'll ever use this thing for.
  • Astrid_Redfern
    Astrid_Redfern over 3 years
    Absolutely excellent answer - I now have pdftk on my 18.04 install, thank you so much!
  • Gino
    Gino almost 3 years
    The only solution that worked for me. Snap installs properly but it's buggy. Really growing to hate snap.
  • loved.by.Jesus
    loved.by.Jesus almost 3 years
    Out of order. It seems that this PPA is no more active. :-(
  • Max Carroll
    Max Carroll over 2 years
    this worked for me on wsl