KoolReport's Forum

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

KoolReport not working if called by a file that isn't index.php #872

Open Giulia Fois opened this topic on on May 15, 2019 - 12 comments

Giulia Fois commented on May 15, 2019

Hello, I'm integrating my report class and view in an already existing project. I'm trying to export my report in pdf format using the same code I previously used in my index.php file (used for testing). The code is the following:

$report = new PdfPaging; //my report class
$report->run()
->cloudExport("PdfPagingPDF")
->chromeHeadlessio($myKey)
->pdf([
    "displayHeaderFooter" => true,
    "headerTemplate" => "<div class='page-header'></div>",
    "footerTemplate" => "<div class='page-footer'></div>",
    "format" => "A4",
    "margin" => [
        'top'=> '50px',
        'right'=> '20px',
        'bottom'=> '20px',
        'left'=>'20px'
    ]
])
->toBrowser("myreport.pdf");

When I ran this piece of code in index.php it worked just fine. However, I'm trying to run it in another file, where I do other stuff prior to running the report part. It doesn't work and my page remains stalled, looping for a long time until it reaches a timeout. What could be the reason? Thank you!

David Winterburn commented on May 15, 2019

Hi Giulia,

Probably your previous codes return output before the pdf content which messed with output header. Please try this approach:

ob_start();
//Your other php codes
...
ob_end_clean();

//Your pdf export codes
$report = new PdfPaging; //my report class
$report->run()
->cloudExport("PdfPagingPDF")
->chromeHeadlessio($myKey)
->pdf([
    "displayHeaderFooter" => true,
    "headerTemplate" => "<div class='page-header'></div>",
    "footerTemplate" => "<div class='page-footer'></div>",
    "format" => "A4",
    "margin" => [
        'top'=> '50px',
        'right'=> '20px',
        'bottom'=> '20px',
        'left'=>'20px'
    ]
])
->toBrowser("myreport.pdf");

Let us know if this solves your problem or there's still issues. Thanks!

KoolReport commented on May 15, 2019

Make sure that you don't echo anything in other code because it will interfere with the content of pdf pushed to browser. That is reason why we prefer to put the export code in a single file to avoid any interference.

Giulia Fois commented on May 16, 2019

Hello, thank you for your support. Unfortunately, I wasn't able to make it work, even following your suggestions. When the script ends for a 504 Gateway Time-out, it gives me the following error:

Warning: file_get_contents(http://localhost:8080/ReportCreator/src/css): failed to open stream: HTTP request failed! HTTP/1.1 403 Forbidden in /Library/WebServer/Documents/ReportCreator/koolreport/packages/cloudexport/vendor/chromeheadlessio/php-client/src/Exporter.php on line 163

Warning: file_get_contents(http://localhost:8080/ReportCreator/src/js): failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found in /Library/WebServer/Documents/ReportCreator/koolreport/packages/cloudexport/vendor/chromeheadlessio/php-client/src/Exporter.php on line 163 Request failed:

I don't even have a js directory in my src directory. What could be the problem? Thanks a lot.

Giulia Fois commented on May 16, 2019

It also gives me this error:

Fatal error: Uncaught Exception: Error when sending request: LibreSSL SSL_connect: SSL_ERROR_SYSCALL in connection to service.chromeheadless.io:443 in /Library/WebServer/Documents/ReportCreator/koolreport/packages/cloudexport/vendor/chromeheadlessio/php-client/src/Exporter.php:304 Stack trace: #0 /Library/WebServer/Documents/ReportCreator/koolreport/packages/cloudexport/vendor/chromeheadlessio/php-client/src/Service.php(31): chromeheadlessio\Exporter->cloudRequest('pdf', Array) #1 /Library/WebServer/Documents/ReportCreator/koolreport/packages/cloudexport/ChromeHeadlessIoService.php(117): chromeheadlessio\Service->pdf(Array) #2 /Library/WebServer/Documents/ReportCreator/koolreport/packages/cloudexport/ChromeHeadlessIoService.php(77): koolreport\cloudexport\ChromeHeadlessIoService->exportTo('pdf', Array) #3 /Library/WebServer/Documents/ReportCreator/src/index.php(14): koolreport\cloudexport\ChromeHeadlessIoService->pdf(Array) #4 {main} thrown in /Library/WebServer/Documents/ReportCreator/koolreport/packages/cloudexport/vendor/chromeheadlessio/php-client/src/Exporter.php on line 304

David Winterburn commented on May 16, 2019

Hi Giulia,

Please open the file packages/cloudexport/ChromeHeadlessIoService.php and replace the following lines:

            'resourcePatterns' => [
                [
                    "regex" => '~((KoolReport.load.resources|KoolReport.widget.init)\([^\)]*)["\']([^"\',\)\[\]\:]+)["\']~',
                    "replace" => "{group1}'{group3}'",
                    "urlGroup" => "{group3}"
                ]
            ]

with:

            'resourcePatterns' => [
                [
                    "regex" => '~((KoolReport.load.resources|KoolReport.widget.init)\([^\)]*)["\']([^"\']+css|[^"\']+js|css[^"\']+|js[^"\']+|(?![^"\',\)\[\]\:]*(css|js))[^"\',\)\[\]\:]*)["\']~',
                    "replace" => "{group1}'{group3}'",
                    "urlGroup" => "{group3}"
                ]
            ]

If you page has a lot of widgets, you could also try to increase PHP max_execution_time (in php.ini) or your web server's timeout. Let us know if this solves your issue. Thanks!

Giulia Fois commented on May 16, 2019

Thank you! Now the first two warnings have disappeared, but I get this error again:

Fatal error: Uncaught Exception: Error when sending request: LibreSSL SSL_connect: SSL_ERROR_SYSCALL in connection to service.chromeheadless.io:443 in /Library/WebServer/Documents/ReportCreator/koolreport/packages/cloudexport/vendor/chromeheadlessio/php-client/src/Exporter.php:304 Stack trace: #0 /Library/WebServer/Documents/ReportCreator/koolreport/packages/cloudexport/vendor/chromeheadlessio/php-client/src/Service.php(31): chromeheadlessio\Exporter->cloudRequest('pdf', Array) #1 /Library/WebServer/Documents/ReportCreator/koolreport/packages/cloudexport/ChromeHeadlessIoService.php(117): chromeheadlessio\Service->pdf(Array) #2 /Library/WebServer/Documents/ReportCreator/koolreport/packages/cloudexport/ChromeHeadlessIoService.php(77): koolreport\cloudexport\ChromeHeadlessIoService->exportTo('pdf', Array) #3 /Library/WebServer/Documents/ReportCreator/src/index.php(14): koolreport\cloudexport\ChromeHeadlessIoService->pdf(Array) #4 {main} thrown in /Library/WebServer/Documents/ReportCreator/koolreport/packages/cloudexport/vendor/chromeheadlessio/php-client/src/Exporter.php on line 304

David Winterburn commented on May 16, 2019

Hi Giulia,

Our server had just been overloaded and has been restarted. Please try your export again and let us know the result. Thanks!

Giulia Fois commented on May 16, 2019

Thanks a lot, it worked! Your help was really precious!!

David Winterburn commented on May 16, 2019

Hi Giulia,

Is your request token name Nuvap Token Prova? Thanks!

David Winterburn commented on May 16, 2019

By the way, would you please try some export with index.php and some with another file? Thanks!

Giulia Fois commented on May 16, 2019

Yes the token is Nuvap Token Prova. I tried with both index.php and the other file I have and it works everywhere. Thank you so much again!!

David Winterburn commented on May 16, 2019

You're much welcome, Giulia.

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

None