36

I'm trying to compile a bunch of PDFs into a single document. ls shows the files are in the directory that I'm in and they're readable (-rw-r--r--). But when I try to run pdftk 2017.pdf cat output test.pdf I get an error:

Error: Unexpected Exception in open_reader()
java.io.FileNotFoundException: 2017.pdf (Permission denied)
   at gnu.java.nio.channels.FileChannelImpl.open(libgcj.so.16)
   at gnu.java.nio.channels.FileChannelImpl.<init>(libgcj.so.16)
   at gnu.java.nio.channels.FileChannelImpl.create(libgcj.so.16)
   at java.io.RandomAccessFile.<init>(libgcj.so.16)
   at pdftk.com.lowagie.text.pdf.RandomAccessFileOrArray.<init>(pdftk)
   at pdftk.com.lowagie.text.pdf.PRTokeniser.<init>(pdftk)
   at pdftk.com.lowagie.text.pdf.PdfReader.<init>(pdftk)
   at pdftk.com.lowagie.text.pdf.PdfReader.<init>(pdftk)
Error: Failed to open PDF file: 
   2017.pdf
Errors encountered.  No output created.
Done.  Input errors, so no output created.

If I add more files to that operation I just get the error for each of them.

I can rename the PDFs from the command line mv 2017.pdf foo.pdf and I get the same error. Error: Unexpected Exception in open_reader() java.io.FileNotFoundException: foo.pdf (Permission denied)

If I try to call a non-existent file, eg. pdftk 123.pdf cat output test.pdf I get a different error:

Error: Unable to find file.
Error: Failed to open PDF file: 
   123.pdf
Errors encountered.  No output created.
Done.  Input errors, so no output created.

Even tail 2017.pdf shows the last few lines of 2017.pdf:

<<
/Info 63 0 R
/ID [<cc59759cedaf07420bbe3250ba5d8971><f259ad128310d106c7aa80b673c4bd70>]
/Root 62 0 R
/Size 64
>>
startxref
42883
%%EOF

If I can see the file and read it with tail, why would pdftk not be able to read it?

cas
  • 1
  • 7
  • 119
  • 185
Amanda
  • 1,709
  • 2
  • 13
  • 26
  • That seems very strange. Could you just confirm that you get the same issue if you rename the file to `foo.pdf` or something, just in case there's a weird bug in pdftk that makes it fail on that filename? I really doubt it will help, but you never know... – terdon Aug 04 '19 at 17:32
  • Tried renaming and copying. Still getting the error. – Amanda Aug 04 '19 at 17:37
  • Hang on, do you have the necessary permissions to all parent directories? Could you have changed the permissions of a parent after having `cd`ed into the target dir? Do you get the same issue if you copy your file to your `$HOME`? – terdon Aug 04 '19 at 17:43
  • Guess: Something is messed up between `pdftk` and the Java libraries it uses. Try to update both to the newest versions with your package manager. If you have multiple versions of Java installed, try to get rid of the oldest ones. – dirkt Aug 04 '19 at 17:45
  • 1
    @terdon Works fine when I move them to my desktop, so that's a fix. The initial directory is a separate ext4 partition that I created for Dropbox to sync to, but I can do other command line work in that directory just fine. – Amanda Aug 04 '19 at 17:47
  • @Amanda ah, good. I suspect that while you can enter the directory (or have done so) the java libraries cannot access it. If so, this is really very specific to your setup and I'll probably close this question if you can confirm that running the concatenation command elsewhere and then copying the `test.pdf` to the target directory works for you. – terdon Aug 04 '19 at 18:02
  • No, it's not specific to his setup. I get the very same error on regular files in my Documents directory. In an attempt to resolve this I copied the files to a temporary folder. Still getting the same error. As everything is running under the same user account Java libraries should be able to access these files perfectly well. – Regis May Mar 14 '21 at 20:48
  • In my case it turned out that PDFTK reports valid files being not in PDF format as non -existent. This is incorrenct and must be considered as being a bug. This is quite disappointing. – Regis May Mar 14 '21 at 21:16

5 Answers5

28

TL;DR

Snaps access right management appears to be the source of the issue.

To solve this, you can either:

  • Do your work from your $HOME folder. Note that symlinks will not work.
  • Install pdftk from another source than the one of your distribution. For instance, ppa:malteworld/ppa has version 3.0.0 of pdftk-java.

Original reply

I am having the same issue. I was doing it from a folder on a USB drive. And indeed, doing this from a subfolder of my home directory works.

That puzzled me as I tried to do it from a subfolder under /tmp and it did not work neither (with a different error, less verbose, "Failed to open PDF file"). Same if I try from a subfolder on a secondary disk mounted under /mnt.

I suspect it could be related to limitation with snaps (I am on an up-to-date Ubuntu 18.04.3). But I have very little experience dealing with snaps, so I cannot explore further.

If so, that would be quite broken as that prevent Ubuntu users from using pdftk from anywhere else than their home folder. Eg. a USB drive, a extra disk, a shared network drive.

(sorry I could not reply as comment, not enough reputation...)

ntc2
  • 146
  • 7
AxelVoitier
  • 381
  • 3
  • 5
  • 1
    Welcome to the site. Please elaborate why you suspect that the Ubuntu Snaps are the reason of this issue, so that other readers may use this as a starting point for further investigations. – AdminBee Nov 08 '19 at 12:18
  • 3
    I suspect that because on this Ubuntu version pdftk comes as a snap, and snaps main point of interest is their focus on extra application security by sandboxing and isolation with least-privilege paradigm. It notably uses AppArmor, namespace, etc. to achieve that. And strange permission errors like the ones encountered here could be symptomatic of a too restrictive rule with one of those security subsystems. – AxelVoitier Nov 12 '19 at 10:52
  • Ok, that's a good point. – AdminBee Nov 12 '19 at 10:59
  • 2
    Not sure if it is pdftk or Ubuntu's snaps, but the issue is exactly that one needs to copy the file in home to do the pdftk work on it. This is quite obnoxius – Davide Jan 02 '20 at 18:49
  • 3
    I believe this error should between pdftk and snap too, I have the same problem here, I tried on /tmp, using links, files in other homedir and other partitions and only works on $HOME and subdirs. So I solved my case, installed pdftk via ppa: malteworld / ppa repository them works fine. – SauloAlessandre Jan 14 '20 at 02:59
  • 3
    Thanks, that helped me too. I will never ever use snap again. – Kjeld Flarup Feb 11 '20 at 22:03
  • @sauloAlessandre and AxelVoitier: probably the comments of Axel and Saulo's "ppa:maltework/ppa" could appear emphasized in the answer; "snap" should be added as a question tag – JJoao Mar 13 '20 at 09:00
  • This answer does **not apply to you** if you installed pdftk via package `pdftk-java`, which is the default under Ubuntu 18.10 and higher. To test: follow the symlinks from `/usr/bin/pdftk`. If they point to `/usr/bin/pdftk.pdftk-java`, that's coming from a `.deb`, not a Snap package. Proof: `dpkg -S /usr/bin/pdftk.pdftk-java` – tanius Sep 21 '20 at 17:00
4

Another solution is posted as part of GitHub issue #7 for the pdftk project:

sudo ln -s /snap/pdftk/current/usr/bin/pdftk /usr/bin/pdftk

It's simpler but does require sudo permissions. This worked for me on Ubuntu 18.04.

In the same GitHub issue, another user mentions

sudo ln -s /snap/pdftk/current/usr/bin/pdftk /usr/bin/pdftk
sudo rm /snap/bin/pdftk
sudo ln -s /usr/bin/pdftk /snap/bin/pdftk

This is essentially the same thing, but also replaces /snap/bin/pdftk with a symbolic link to /usr/bin/pdftk (useful if /snap/bin comes before /usr/bin in your $PATH).

Kusalananda
  • 320,670
  • 36
  • 633
  • 936
ifokkema
  • 51
  • 1
  • 1
    Thanks, I prefer this answer as it is normally better to use snap rather than a ppa for compatibility and security. I used the extra "delete /snap/bin/pdftk" step and now it is working. You saved my day :-) – Kulgar Jun 07 '23 at 11:57
1

For a file on a different drive than /snap, the solution that worked for me was to extend pdftk access to removable media:

snap connect pdftk:removable-media
Flo
  • 11
  • 1
1

I was trying to combine several .pdf files to a single .pdf file using pdftk.

The files were in my USB disk, and i transferred the files to the computer prior to conversion.

It worked fine.

Hope it is useful to others.

gerhard d.
  • 2,168
  • 12
  • 21
0

I don't have it installed from snap (it's from the Arch repo) and I'm still seeing weird stuff. Not the same permission error as you, just the one you see when you try to load a non-existent file. Lucky pdftk can read from stdin with -. You only have one file, so you can cat it in

cat 2017.pdf | pdftk - cat output test.pdf
Tom Saleeba
  • 101
  • 4
  • "PDFtk is no longer available in Debian. This package enables an upgrade to pdftk-java, a port of PDFtk to Java. This package can be safely removed." – Jeremy Boden Jul 16 '21 at 12:19