PDF Export debugging

I just ran into an issue where PDF export wasn’t working and I went through some debugging to figure out why. I haven’t seen this detail posted before, so posting here in case it helps someone else.

The SC PDF process:

  1. The app generates a simplified html page and saves it into the _lib/tmp folder.
  2. It then calls wkhtmltopdf from _lib/prod/third/wkhtmltopdf
  3. A debug file with the command line to be executed is saved into _lib/tmp folder.
  4. If wkhtmltopdf is successful, then the PDF is created in _lib/tmp folder.

First checks:

  1. Make sure _lib/tmp can be written to
  2. Make sure that the wkhtmltopdf executable for your system can be executed (755).
  3. Make sure that PHP is allowed to call ‘exec’ (this is blocked on some hosting platforms).

Further debug:
We need to capture the output from the exec command so we can see whats going wrong.
If there is no output at all, then its most probably that php isn’t allowing the exec command. You can double check this through phpinfo() → “disable_functions”.

In the production environment, edit the application code as follows:

application/index.php
Search for “exec($str_execcmd”

Edit it, also adding the following lines:

exec($str_execcmd . " 2>&1",$execoutput,$returncode);
error_log(“Returned with status $returncode and output:\n”);
error_log(implode("\n",$execoutput));

Now you can check the error_log and see what the problem is.
NOTE that SC changes the path just before calling wkhtmltopdf, so you’ll most likely find the correct error log in _lib/prod/third/wkhtmltopdf as I did.
If you can’t find it, run the following command on the server: “find ~ -name error_log”

If anyone else has any more tips, please feel free to add them here.

2 Likes