KoolReport's Forum

Official Support Area, Q&As, Discussions, Suggestions and Bug reports.
Forum's Guidelines

Could not execute phantomjs #400

Open Gerard van de Ven opened this topic on on Jul 31, 2018 - 3 comments

Gerard van de Ven commented on Jul 31, 2018

Hi,

I just installed the "export" package and phantomjs, but I am getting the following error message:

Fatal error: Uncaught exception 'Exception' with message 'Could not execute phantomjs' in C:\Users\gven\vqr\vqr-development\www\vendor\koolphp\koolreport\koolreport\packages\export\Handler.php:71 Stack trace: #0 C:\Users\gven\vqr\vqr-development\www\vendor\koolphp\koolreport\koolreport\packages\export\Handler.php(156): koolreport\export\Handler->runPhantom('C:\\Users\\gven\\v...', 'C:\\Users\\gven\\v...', 'C:\\Users\\gven\\v...', 'eyJmb3JtYXQiOiJ...') #1 C:\Users\gven\vqr\vqr-development\www\mypage\index.php(13): koolreport\export\Handler->pdf(Array) #2 {main} thrown in C:\Users\gven\vqr\vqr-development\www\vendor\koolphp\koolreport\koolreport\packages\export\Handler.php on line 71

Looking at line 71, it is an issue with the shell_exec. On my development machine I am running PHP 5.6.36. It is a Windows 10 machine, and the website runs through IIS. I have already assigned an administrator account as the account that runs the website, but this does not make any difference.

The code I am running is just the "mypage.php" example from the package blog. I did have to specify the path to the phantomjs file, because otherwise it gave a not found error. When I did set the path though, that error went away.

What now? Does anybody has any idea what needs to be done to make this work in this context?

Thanks!

Gerard

KoolReport commented on Jul 31, 2018

Our developing machine is Windows 10 as well however we use Apache. Could you please check if shell_exec() function is disabled in php ini, also please check this link http://php.net/shell_exec. There are a comment related to IIS, it seems that you need to give permission to run cmd

With PHP on Windows, if you get the 'Warning: shell_exec() [function.shell-exec]: Unable to execute' error, then you need to check the permissions on file 'C:\WINDOWS\system32\cmd.exe'. You need read/execute permission on this file.  I would recommend using the sysinternals Process Monitor 'procmon.exe' to confirm the user that is trying to run 'cmd.exe'. Filter on 'Process Name' is 'php-cgi.exe' and 'Path' ends with 'cmd.exe'. Look at the event properties for the task with the access denied error, and it will show you the 'Impersonating' user name.  This is usually the 'Internet Guest Account', often 'NT AUTHORITY\IUSR'.

Please let us know.

Gerard van de Ven commented on Aug 1, 2018

Hi,

Thank you for your help. It has pointed me in the right direction, although in a different way than expected probably.

With process monitor, I found a set of "cmd.exe" executions, all running as the Administrator user that I had changed the application pool to run as. And that user has enough rights to run that command. That was not the problem. shell_exec was also not disabled in the php.ini file.

I also noticed that the tmp folder had a lot of "*.tmp" files, all with the html of the "mypage.view.php", from the example. So at least that part of the process ran alright.

What I did find in the list of cmd.exe in procmon was the specific command that executed phantomjs.exe. And it started with:

cmd.exe /c "C:\Users\gven\vqr\vqr-development\wwwendor\koolphp\koolreport\koolreport\packages\export\bin\phantomjs.exe --ignore-ssl-errors=true 

But if you look closely at the command line above, you see a number of strange characters in that line. It should be

cmd.exe /c "C:\Users\gven\vqr\vqr-development\www\vendor\koolphp\koolreport\koolreport\packages\export\bin\phantomjs.exe --ignore-ssl-errors=true

And apparently the "\v" and the "\e" get changed into a special character (is that a php thing? I am not a regular php user, so I am not sure). When I run the command like that in a cmd.exe window as an administrator, I saw that the command worked correctly.

I thought then that the strange characters in the phantomjs path might be caused by me adding

$mypage->export()
    ->settings(array(
    "useLocalTempFolder"=>true,
    "phantomjs"=>"C:\Users\gven\vqr\vqr-development\www\vendor\koolphp\koolreport\koolreport\packages\export\bin\phantomjs.exe"
))

In the index.php file in the example. I did this because originally I got an error that phantomjs.exe was not found. (Note that I installed koolreport with composer. Hence the slightly different folder structure than in your blog post about the export package).

What I did then is change the phantomjs path in the index.php file of the example into

"phantomjs"=>"C:/Users/gven/vqr/vqr-development/www/vendor/koolphp/koolreport/koolreport/packages/export/bin/phantomjs.exe"

with forward slashes instead of backward slashes.

That resolved the issue. The example now runs fine. And I can explore further. It may be a good idea to add something on the use of the back- / forward slash somewhere appropriate.

KoolReport commented on Aug 1, 2018

That's great that you have made it works :) and thank you very much for sharing your experience. It is extremely valuable.

In PHP, if a string is quoted with double quote (""), the string will be processed. You may use special char like \v or \t in the string or even the variable for example "My name is $name". If the string is quoted with single quote ('') then there is no further string process. What you inputed are just string. So '\v' is exactly \v in string. this will be extremely important if our project is in Windows and the path uses \. It is east to convert to special character if you use double quote like your case. Sometime small problem like this can take us a whole day.

Now PHP in Windows can take / slash like in Linux as you found out. I prefer to use this forward slash in path or url because it can work on both Window and Linux.

Again! Thank you very much for being our customer and thank you so so much for sharing your case. It will benefit others who have the same issue.

Have a great day!

Regards,

Karl from KoolPHP Inc

Build Your Excellent Data Report

Let KoolReport help you to make great reports. It's free & open-source released under MIT license.

Download KoolReport View demo
help needed
solved

Export