KoolReport's Forum

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

Export Pdf give permission error #1256

Open Jason opened this topic on on Jan 13, 2020 - 53 comments

Jason commented on Jan 13, 2020

Hello,

I am getting an error when i am trying to export to pdf saying:

Error! Please set executable permission for phantomjs

Can someone please help me thanks

KoolReport commented on Jan 13, 2020

I mean that the export package found phantomjs execution file but seems phantomjs is not permitted to run. Could you please set the execute permission for phantomjs.

Jason commented on Jan 13, 2020

Thanks for the reply, I am using PHP added and followed the guide for the export package. https://www.koolreport.com/docs/export/overview/

In my controller, I have added,

$report ->run() ->export($reports['source'].'Pdf')->pdf()->toBrowser($reports['source'].'.pdf');

I was getting the pdf blank, now when adding the phantomjs got this error above.

Thanks in advance

Jason commented on Jan 13, 2020

changed permission on my folder export/bin and also on the phantomjs.exe getting same error

Jason commented on Jan 13, 2020

changed my code to:

 $phantomjs_path = public_path("\plugins\koolreport\\export\bin");   
                           $report
                                ->run()
                                ->export($reports['source'].'Pdf')
                                ->settings(array(
                                    "phantomjs"=>$phantomjs_path
                                ))
                                ->pdf()
                                ->toBrowser($reports['source'].'.pdf');

now getting the following error:

Error! Could not execute phantomjs

please any advice for this one...

KoolReport commented on Jan 13, 2020

Please remove the settings() method for now and do this:

$report ->run() ->export($reports['source'].'Pdf')->pdf(array(
    "format"=>"A4",
    "orientation"=>"portrait",
))->toBrowser($reports['source'].'.pdf');

Let us know if it works.

Jason commented on Jan 13, 2020

I have the KoolReport Pro version

Jason commented on Jan 13, 2020

Thanks but I am still getting this error :( Error! Please set executable permission for phantomjs

KoolReport commented on Jan 13, 2020

If you go to the line 52 of koolreport/export/Handler.php, you will see this:

            if (!is_executable($this->phantomjs)) {
                throw new \Exception("Please set executable permission for phantomjs");
            }

In here we try to check if the phantomjs has permission to run or in another word, if it is executable. If you have set the permission for phantomjs.exe in bin folder, please comment those above line to bypass the permission checking. Let us know if it works.

Jason commented on Jan 13, 2020

Yes I have try to comment it but now the pdf is downloaded as blank...

Anna Tolve commented on Oct 6, 2021

I have the same problem on a server with the following characteristics:

Product: Plesk Obsidian
OS: CentOS Linux 7.9.2009
Php: 7.4.24 FPM served by Apache
shell_exec () allowed

How did you solve then?

Sebastian Morales commented on Oct 6, 2021

Anna, pls check executable permission of the file koolreport/export/bin/phantomjs. For example, use this terminal command

ls -l koolreport/export/bin/phantomjs

The result should have "x" right in the first column.

Anna Tolve commented on Oct 6, 2021

Hi Sebastian, running the command get this result: -lrwxrwxrwx. , despite having applied all 777 permissions to the entire project folder, how can I do?

Sebastian Morales commented on Oct 6, 2021

The permission looks good. Would you pls post the error message (with screenshot if possible) when you run export?

Anna Tolve commented on Oct 6, 2021

The error:

AH01071: Got error 'PHP message: PHP Fatal error: Uncaught Exception: Could not execute phantomjs in /APT_KoolReport/vendor/koolreport/export/Handler.php:68\nStack trace:\n#0 /APT_KoolReport/vendor/koolreport/export/Handler.php(274): koolreport\export\Handler->runPhantom()\n#1 /APT_KoolReport/report/1.1.1/export_pdf.php(8): koolreport\export\Handler->pdf()\n#2 {main}\n thrown in /APT_KoolReport/vendor/koolreport/export/Handler.php on line 68', referer: /APT_KoolReport/report/1.1.1/Report_1.1.1.php

My code:

<?php

    require_once "Report_1_1_1.php";
    $report = new Report_1_1_1;
    $report->run()
    ->export('Report_1_1_1_pdf')
    ->pdf(array(
        "format"=>"A4",
        "orientation"=>"landscape",
        "zoom"=>"0.8",
        "margin"=>"0.5in"
        ))
    ->toBrowser("Report_1.1.1.pdf", true);
Sebastian Morales commented on Oct 6, 2021

Pls try to run a standard example in the following link with your `koolreport/export/bin/phantomjs' file:

https://phantomjs.org/screen-capture.html

Let us know the result. Tks,

Anna Tolve commented on Oct 6, 2021

I tried using this example code

var page = require('webpage').create();
page.open('http://github.com/', function() {
  page.render('github.png');
  phantom.exit();
});

by replacing the link to my report but the result is a completely white image... :'(

Sebastian Morales commented on Oct 7, 2021

Don't replace the GitHub link with your report one. We are trying to evaluate PhantomJS first. Create the Github.js file and a phantomjs.php file with this content:

<?php
//phantomjs.php
shell_exec("phantomjs github.js");

Put both of these file in koolreport/export/bin and open phantomjs.php on your browser. Then see if there's a github.png image file created or there's any error message.

Anna Tolve commented on Oct 7, 2021

I ran the test the github.png image is downloaded correctly and I just get a warning:

AH01071: Got error 'PHP message: PHP Warning: Use of undefined constant \xe2\x80\x98after_setup_theme\xe2\x80\x99 - assumed '\xe2\x80\x98after_setup_theme\xe2\x80\x99' (this will throw an Error in a future version of PHP) in /var/www/vhosts/.../.../wp-content/plugins/types-custom/types.php on line 29PHP message: PHP Warning: Use of undefined constant \xe2\x80\x98macchine_plugin_setup\xe2\x80\x99 - assumed '\xe2\x80\x98macchine_plugin_setup\xe2\x80\x99' (this will throw an Error in a future version of PHP) in /var/www/vhosts/.../.../wp-content/plugins/types-custom/types.php on line 29', referer: .../vendor/koolreport/export/bin/phantomjs.php
Sebastian Morales commented on Oct 7, 2021

Pls open the file koolreport/export/Handler.php and find the following line:

    $command = $this->phantomjs." --ignore-ssl-errors=true $script $source $output $params";

Add the following command right after it:

    $command = $this->phantomjs." --ignore-ssl-errors=true $script $source $output $params";
    echo "command = $command"; exit;

run your report export to see this command echoed. Then copy and paste it to your command line/terminal and run to see if there's any error.

Anna Tolve commented on Oct 7, 2021

I ran the command and I get the following message:

0;Source file not found
Sebastian Morales commented on Oct 7, 2021

Is "Report_1_1_1_pdf" your pdf view, i.e there exists the file "Report_1_1_1_pdf.view.php"? Normally we use the following files: "MyReport.php" as setup file, "MyReport.view.php" as web view file, "MyReportPDF.view.php" as pdf view file. Then we use this export command:

    $report->run()->export("MyReportPDF") //this should be the file name without the ".view.php" part
    ->settings([
        'useLocalTempFolder' => true,
        'autoDeleteLocalTempFile' => true,
    ])
    ->pdf(array(...))
    ->toBrowser("MyReport.pdf")
    ; 

If your pdf view file is correct, pls try to use a local temp folder setting like in my code to see if it works.

Anna Tolve commented on Oct 7, 2021

yes the file Report_1_1_1_pdf.view.php exists in the folder, on my windows pc everything works correctly, on the server instead I always get the same error

AH01071: Got error 'PHP message: PHP Fatal error: Uncaught Exception: Could not execute phantomjs in         .../vendor/koolreport/export/Handler.php:69\nStack trace:\n#0 

also with the following code:

<? php

require_once "Report_1_1_1.php";
$ report = new Report_1_1_1;
$ report-> run ()
-> export ('Report_1_1_1_pdf')
-> settings ([
     'useLocalTempFolder' => true,
     'autoDeleteLocalTempFile' => true,
     ])
-> pdf (array (
     "format" => "A4",
     "orientation" => "landscape",
     "zoom" => "0.8",
     "margin" => "0.5in"
))
-> toBrowser ("Report_1.1.1.pdf", true);
Sebastian Morales commented on Oct 7, 2021

Pls remove auto delete local temp file setting:

-> settings ([
     'useLocalTempFolder' => true,
     // 'autoDeleteLocalTempFile' => true, comment out this line
     ])

Then echo the phantomjs command in koolreport/export/Handlers.php:

    $command = $this->phantomjs." --ignore-ssl-errors=true $script $source $output $params";
    echo "command = $command"; exit;

Finally run your report export and copy the echoed phantomjs command to terminal and let us know the result. Tks,

Anna Tolve commented on Oct 7, 2021

Done this time I do not get errors the .pdf is not yet shown in the browser page but is generated in the tmp folder. How can I do to show it in the browser?

Sebastian Morales commented on Oct 7, 2021

Comment out this line:

    // echo "command = $command"; exit;

Then run export and let us know if there's any error message or if any new pdf is generated in the local tmp directory?

Anna Tolve commented on Oct 7, 2021

I always get the same error

AH01071: Got error 'PHP message: PHP Fatal error: Uncaught Exception: Could not execute phantomjs in .../vendor/koolreport/export/Handler.php:69\nStack trace:\n#0 

and the .pdf in tmp is not updated :(

Sebastian Morales commented on Oct 7, 2021

Pls try to run the following command in terminal:

php -r 'shell_exec("{echoed phantomjs command}")';

Remember to replace the string {echoed phantomjs command} with the previous successful echoed phantomjs command.

Anna Tolve commented on Oct 7, 2021

Now I get the following error:

PHP parsing error: syntax error, unexpected end of file in command line code on line 1
Sebastian Morales commented on Oct 8, 2021

There probably be quotes in your echoed command. Pls escape them with backward slash like this

\'
\"
Anna Tolve commented on Oct 8, 2021

I don't have an ' o " in command, what am I doing wrong?

php -r 'shell_exec("{phantomjs --ignore-ssl-errors=true /var/www/vhosts/domain-name/subdomain-name/APT_KoolReport/vendor/koolreport/export/pdf/pdf.js /var/www/vhosts/domain-name/subdomain-name/APT_KoolReport/report/1.1.1/tmp/615ef384af06d1.tmp /var/www/vhosts/domain-name/subdomain-name/APT_KoolReport/report/1.1.1/tmp/615ef384af1b52.pdf eyJmb3JtYXQiOiJBNCIsIm9yaWVudGF0aW9uIjoibGFuZHNjYXBlIiwiem9vbSI6IjAuOCIsIm1hcmdpbiI6IjAuNWluIiwiZXhwZWN0ZWRMb2NhdGlvbiI6Imh0dHBzOlwvXC93cC5hcHRiYXNpbGljYXRhLml0XC9BUFRfS29vbFJlcG9ydFwvcmVwb3J0XC8xLjEuMVwvZXhwb3J0X3BkZi5waHAiLCJyZXNvdXJjZVdhaXRpbmciOjEwMDB9}")';
Sebastian Morales commented on Oct 8, 2021

Oh, I can see that you don't replace the curly braces in {echoed phantomjs command}. The syntax should not be:

php -r 'shell_exec("{phantomjs ...}")'

It should be:

php -r 'shell_exec("phantomjs --ignore-ssl-errors=true /var/www/vhosts/domain-name/subdomain-name/APT_KoolReport/vendor/koolreport/export/pdf/pdf.js /var/www/vhosts/domain-name/subdomain-name/APT_KoolReport/report/1.1.1/tmp/615ef384af06d1.tmp /var/www/vhosts/domain-name/subdomain-name/APT_KoolReport/report/1.1.1/tmp/615ef384af1b52.pdf eyJmb3JtYXQiOiJBNCIsIm9yaWVudGF0aW9uIjoibGFuZHNjYXBlIiwiem9vbSI6IjAuOCIsIm1hcmdpbiI6IjAuNWluIiwiZXhwZWN0ZWRMb2NhdGlvbiI6Imh0dHBzOlwvXC93cC5hcHRiYXNpbGljYXRhLml0XC9BUFRfS29vbFJlcG9ydFwvcmVwb3J0XC8xLjEuMVwvZXhwb3J0X3BkZi5waHAiLCJyZXNvdXJjZVdhaXRpbmciOjEwMDB9");'

Another point is to put a semicolon at the end of the the string. Pls try this command with no curly braces in the shell command and an added semicolon at the end (just before the last single quote):

php -r 'shell_exec("phantomjs ...");'
Anna Tolve commented on Oct 8, 2021

ok now the command is executed, and the .pdf is generated in the tmp folder, by re-executing the command several times the .pdf is updated. But how can I do now to view it in the browser?

Sebastian Morales commented on Oct 8, 2021

Create a file called phantomjs.php with this content:

<?php
shell_exec("phantomjs ...");

Open it in your browser to see if a new pdf is generated.

Anna Tolve commented on Oct 8, 2021

I don't get errors but the .pdf is not updated!

Sebastian Morales commented on Oct 8, 2021

Pls try this with phantomjs.php and let us know the output:

<?php
$result = shell_exec($command);
var_dump($result);
Anna Tolve commented on Oct 8, 2021

I get the following message:

string (106) "1; /var/www/vhosts/domain-name/subdomain-name/APT_KoolReport/report/1.1.1/tmp/615ef384af1b52.pdf"

is pointing to the .pdf created by the shell command earlier...

Sebastian Morales commented on Oct 8, 2021

It's a good result. Pls delete /var/www/vhosts/domain-name/subdomain-name/APT_KoolReport/report/1.1.1/tmp/615ef384af1b52.pdf and open phantomjs.php again to see if it's regenerated.

Anna Tolve commented on Oct 8, 2021

sadly no, I get this:

string (24) "0; Source file not found"

:'(

Sebastian Morales commented on Oct 8, 2021

Don't delete the .tmp files in your local temp directory. We need them to generate pdf. Now run your report export again to get a new echoed phantomjs shell command. Then copy it to phantomjs.php and run again.

Anna Tolve commented on Oct 8, 2021

I tried again, only the .tmp file is created in the tmp folder but not the .pdf file and I always get the following message:
string (24) "0; Source file not found"

Sebastian Morales commented on Oct 8, 2021

If you run:

php -r 'shell_exec("phantomjs ...");'

Does it work?

Anna Tolve commented on Oct 8, 2021

If you run the command it works, and the .pdf in tmp is generated, from phantomjs.php it doesn't work

Sebastian Morales commented on Oct 11, 2021

Pls find out which PHP user your web server run as with this php page:

<?php
  echo exec('whoami');
?> 

Then try to run the phantomjs command as that user. For example with runuser command or any other way:

runuser -l php_user -c ' php -r \'shell_exec("phantomjs ...");\' '
Anna Tolve commented on Oct 13, 2021

Hi Sebastian, we already ran the command with that user, the problem is to understand why from the command line now it always produces the .pdf in tmp folder, but when exporting from the php page it is not executed and always returns the error

         AH01071: Got error 'PHP message: PHP Fatal error: Uncaught Exception: Could not execute phantomjs in
Sebastian Morales commented on Oct 14, 2021

The most likely reason is that your terminal user is different from your php user (which user web server uses to process php). You already know which terminal user you are using. That's why I asked you to open a php page with this content:

<?php
  echo exec('whoami');
?> 

Then we can try to run the phantomjs command from terminal with the php user to see why it failed.

Anna Tolve commented on Oct 14, 2021

executing the command with the user:

runuser -l php_user -c ' php -r \'shell_exec("phantomjs --ignore-ssl-errors=true /var/www/vhosts/domain_name/subdomain_name/APT_KoolReport/vendor/koolreport/export/pdf/pdf.js /tmp/6167dd3c62ac01.tmp /tmp/6167dd3c62bad2.pdf eyJmb3JtYXQiOiJBNCIsIm9yaWVudGF0aW9uIjoibGFuZHNjYXBlIiwiem9vbSI6IjAuNTUiLCJtYXJnaW4iOnsidG9wIjoiMC4yaW4iLCJib3R0b20iOiIwLjJpbiIsImxlZnQiOiIyaW4iLCJyaWdodCI6IjJpbiJ9LCJleHBlY3RlZExvY2F0aW9uIjoiaHR0cHM6XC9cL3dwLmFwdGJhc2lsaWNhdGEuaXRcL0FQVF9Lb29sUmVwb3J0XC9yZXBvcnRcLzEuMi4xXC9leHBvcnRfcGRmLnBocCIsInJlc291cmNlV2FpdGluZyI6MTAwMH0=");\' '

I get from the print it returns me this error:

-bash: syntax error near unexpected token `('

where am I wrong?

Sebastian Morales commented on Oct 15, 2021

Ok, you can try to switch to your php_user in terminal like this:

su - php_user

After switching run whoami to check if terminal user is switched:

whoami

Finally run the phantomjs command to see if there's any problem with running phantomjs with the switched user

phantomjs ...
Anna Tolve commented on Oct 15, 2021

Done everything works from the command line, the .pdf is created in the tmp folder.

Now how do I get the export to work normally from the browser by clicking the export button dove ottengo ancora lo stesso errore?

Sebastian Morales commented on Oct 18, 2021

Can you pls check if your tmp directory is readable and writable for your php_user?

Anna Tolve commented on Oct 20, 2021

I tried to insert this code in Handler.php:

$command = $this->phantomjs." --ignore-ssl-errors=true $script $source $output $params";
    // echo "command = <br>" . str_replace(" ", "<br>", $command) . "<br>"; exit;
    // echo "command = $command"; exit;

    $dir = '../../../report/1.1.1/tmp';

    // try to create this directory if it doesn't exist
    $booExists     = is_dir($dir) || (mkdir($dir, 0774, true) && is_dir($dir));
    $booIsWritable = false;
    if ($booExists && is_writable($dir)) {
        $tempFile = tempnam($dir, 'tmp');
        if ($tempFile !== false) {
            $res = file_put_contents($tempFile, 'test');

            $booIsWritable = $res !== false;
            @unlink($tempFile);
        }
    }

the tmp folder and the temporary file are created but not the .pdf

Sebastian Morales commented on Oct 21, 2021

Anna, pls try these terminal commands and let me know the result. First switch to your php user:

su - php_user

Then execute the phantomjs command but via php like this:

php -r 'shell_exec("phantomjs ...");'
Anna Tolve commented on Oct 22, 2021

Sebastian if I run the command from the terminal it works fine, I don't care because the same command executed in Handler.php does not produce the .pdf and returns an error.. I am desperate! :'(

Sebastian Morales commented on Oct 22, 2021

I understand your frustration. All signs point to insufficient permission to either execute Phantomjs or writable tmp directory but your tests show that your php user has those rights when executing in command line, just not when executing via web server.

Now this is not a good advice for a production system but for debugging purpose I would suggest trying to change your Apache's php user and group to ones with admin rights:

https://www.simplified.guide/apache/change-user-and-group

Then try to run report export via browser to see how it works. Our next step depends on whether this test runs successfully or not.

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
None yet

None