php-pdftk: Can not write to temp directory

Whenever I try to fill in a form using fillForm I get an error message stating Error: Failed to open form data file: \n /tmp/php_pdftk_xfdf_uL6Hf2.xfdf\n No output created. code: $tempPdf = new \mikehaertl\pdftk\Pdf(__DIR__ . '/Mededelingsformulier_scheiding_2019_2.pdf'); $tempPdf->fillForm($arr = [ 'test' => 'test', ]); $tempPdf->saveAs(__DIR__ . '/tempfile.pdf'); $error = $tempPdf->getError(); $this->assertEquals('', $error); All files are accessible, other functions work perfectly (getDataFileds, GenerateFdf) I’m working with Laravel running on an updated Homestead vagrant instance using pdftk from a snap install.

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 27 (9 by maintainers)

Commits related to this issue

Most upvoted comments

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

worked for me too…

Okay, I now know what the error is. pdftk is a bit outdated. I believe a version 3.0 is in the works using the open version of Java

However to install pdftk on a recent version of ubuntu is using snap. this will install it in a secure way so that it doesn’t have access to the /tmp folder. That’s why it’s failing out of the box

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

This works. Maybe to put in your readme ? to avoid using the snaps (or you should rewrite parts of your code to generate fdf files outside of the /tmp folder

Here’s an update:

  • The version from ppa:malteworld/ppa is no longer available.
  • The maintainer now points to his answer on askubuntu: https://askubuntu.com/a/1028983/175814
  • For Ubuntu 18.10 and later you can install the pdftk-java package with apt
  • For Ubuntu 18.04 you can manually download that package for focal and still install it manually
  • If you didn’t install with apt you probably end up with the snap version which has the write permission issue with the temp file. In that case you can also set an alternative tempDir.

Im having this issue now on the latest Homestead. Any ideas? Totally lost, /tmp seems to be writeable so I dont get the issue.

I’ve copied a part of my after.sh script of my homestead installation. might provide you with a start to a solution…

#!/bin/sh

# If you would like to do some extra provisioning you may
# add any commands you wish to this file and they will
# be run after the Homestead machine is provisioned.

if [ ! -f /usr/local/extra_homestead_software_installed ]; then
	echo 'installing some extra software'

    sudo locale-gen nl_NL nl_NL.utf8

    sudo snap install pdftk
    sudo ln -s /snap/pdftk/current/usr/bin/pdftk /usr/bin/pdftk
    
    
    touch /usr/local/extra_homestead_software_installed
else    
    echo "extra software already installed... moving on..."
fi

I found the solution. I was wrong to give permission to the tmp folder 😉

okey i solved the issue, the php can not run pdftk command because the www-data has no permission to run script

Following the suggestions above, manually setting the tempDir resolved the error. For those using Laravel Forge, this would look something like:

On the server:

cd /home/forge
mkdir tmp

and then in the code:

$pdf = new Pdf('/some/file.pdf');
$pdf->tempDir = '/home/forge/tmp';

Although now there is unrelated issue I’m currently wrestling with:

sh: 1: pdftk: not found

But I think that may because pdftk was installed using snap. This will require further debugging.