mikehaertl/php-pdftk

Pdftk doesn't work through php

Clox opened this issue · 2 comments

Clox commented

I want to begin by saying that this is not an issue of php-pdftk, and I apologize for opening an issue here but I couldn't figure a better place to ask for help since it is really closely related since it touches on both php&shell&pdftk.

I'm using php-pdftk on my server to fill a pdf-file and return to the user and this works great on the local development-server.
But when I test this in production it generates a 0-byte file.

At this point I rules out php-pdftk by calling:
shell_exec("java -jar ../bin/pdftk-all.jar ../app/forms/pdf/mypdf.pdf output ../storage/test.pdf")
This works on the development-server but not in production.

So I started picking this apart.

  • Does it work running the above command on production-server but outside of php, directly in terminal? Yes, it does.
    So the problem is isolated somewhere to production-server & php & shell
  • Does the call to java through php work?
    Yes, the following works shell_exec('java -version 2>&1')
  • Does the call to pdftk through php work?
    Yes, the following works: shell_exec('java -jar ../bin/pdftk-all.jar -version')
  • Does access to the pdf-file work?
    Yes, both read_file('../app/forms/pdf/mypdf.pdf') and shell_exec('stat ../app/forms/pdf/mypdf.pdf') work.
  • Does writing to the output-folder work? Yes, shell_exec('touch ../storage/shelltouch') works.

Why doesn't it work when put together?

Any help would be appreciated, including directing me to better place to ask the question.

Thanks in advance!

Clox commented

I managed to solve it and it was something really stupid.
The filename of the source pdf-file had a special character in it (ä). This wasn't a problem directly via the terminal on either test or production server, and wasn't a problem through php on test either, but it apparently tripped it up in shell via php on production.

I don't remember the details but there is a way to make this work even with utf8 encoded filenames. You could try to set the locale in the options for php-shellcommand (passed as 2nd constructor arg):

<?php
$pdf = new Pdf($file, [
    'locale' => 'en_US.UTF-8',
]);

You may also have to set the LANG env var in the procEnv option (see https://stackoverflow.com/a/10029316/767752).

Just make sure, that you use a locale that is actually available on your production server. You can check with locale -a.