Forum Bugs

convert_string_to_passthru doesn't work (PHP)

theodorejb
Using the following code:
$prince = new \Prince('C:\Program Files (x86)\Prince\Engine\bin\prince.exe');
if ($prince->convert_string_to_passthru($html) !== true) {
    echo "Failed to generate PDF from provided HTML";
}

I always get the message "Failed to generate PDF from provided HTML." I tried using setLog() (with a file the PHP user has write access to) to get more detailed errors, but nothing is written to the log.

However, I am able to open the Prince GUI as the PHP user and generate PDFs just fine from the same HTML.

I delved deeper into the prince.php wrapper to see where the error was occurring. In the convert_internal_file_to_passthru function, the call to proc_open appears to be working because the following is_resource check is true. However, the $result variable does not equal 'success'.

In the readMessages function, I added the following code at line 700:
echo '$line is ' . $line;

I now get "$line is The current directory is invalid." in my output. What could be causing this error? My hunch is that there is some permissions issue, but if so I'm not sure what it is.
mikeday
What web server are you running PHP under? Is safe mode enabled?
theodorejb
No, we're using an up-to-date version of PHP 5.5 on Server 2012 R2. From a quick Process Monitor test it appears that the prince executable is never running, even though proc_open seems to be getting a resource for it.
mikeday
proc_open takes three optional arguments: $cwd, $env, and $other_options. Currently prince.php does not set any of these. The error about the current directory could indicate that we need to set $cwd to something, perhaps the path to the folder containing the Prince executable? Another possibility is the $other_options argument, which on Windows can take a "bypass_shell" value to avoid using cmd.exe. Since I don't think we need to go via cmd.exe, this might simplify matters also.
theodorejb
Thanks for the reply. Setting the "bypass_shell" option to true did indeed fix the error. I was also able to fix it by simply setting the current working directory argument to "C:\". I'm thinking the error is possibly related to http://support.microsoft.com/kb/832434. Could an update be made to the Prince wrapper to work around this issue (or possibly provide a hook to set the current working directory)? I'd rather not have to maintain a fork.
mikeday
Yes, I think you've found the cause of the problem. Bypassing the shell altogether seems like the safest approach, as we don't want the shell to do anything anyway. We can set bypass_shell in the next release of the PHP wrapper.
mikeday
Today we have updated the PHP wrapper to revision 14, including the bypass_shell fix. Next on the list: better error logging. :)