KoolReport's Forum

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

Error when Exporting using Chrome Headless #1566

Open Frank DiGiandomenico opened this topic on on Aug 13, 2020 - 4 comments

Frank DiGiandomenico commented on Aug 13, 2020

I'm trying to setup exporting to PDF via Chrome Headless, but I'm getting the following PHP exception:

Fatal error: Uncaught RuntimeException: Directory name must not be empty. in C:\wamp64\www\LegalBillingAPI\koolreport\cloudexport\vendor\chromeheadlessio\php-client\src\Exporter.php on line 46

Here are the files I created for this report...

index.php

<?php 

require_once "MatterTotalsByAccount.php";
$mattertotalsbyaccount = new MatterTotalsByAccount;

$mattertotalsbyaccount
    ->cloudExport("MatterTotalsByAccount")
    ->chromeHeadlessio("my token")
    ->pdf(array(
        "format"=>"A4",
        "landscape"=>false
    ))
    ->toBrowser("MatterTotalsByAccount.pdf");

MatterTotalsByAccount

<?php

// Require autoload.php from koolreport library
require_once "../../koolreport/core/autoload.php";

//Specify some data processes that will be used to process
use \koolreport\processes\Group;
use \koolreport\processes\Sort;
use \koolreport\processes\Limit;

//Define the class
class MatterTotalsByAccount extends \koolreport\KoolReport
{
    use \koolreport\cloudexport\Exportable;

    protected function settings()
    {
        //Define the "matter_totals_by_account" data source which is the result of the MySQL Stored Procedure 
        return array(
            "dataSources"=>array(
                "matter_totals_by_account"=>array(
                    'host' => 'localhost',
                    'username' => 'root',
                    'password' => '',
                    'dbname' => 'databasename',
                    'charset' => 'utf8',  
                    'class' => "\koolreport\datasources\MySQLDataSource"  
                ),
            )
        );
    }
  
    protected function setup()
    {
        $this->src('matter_totals_by_account')
        ->query("CALL MatterTotalsByAccount('2019-01-01','2019-12-31')")
        //Select the data source then pipe data through various process
        //until it reach the end which is the dataStore named "sales_by_customer".
        ->pipe(new Group(array(
            "by"=>array("accountName","matter"),
            "sum"=>"total"
        )))
        ->pipe(new Sort(array(
            "accountName","matter"=>"asc"
        )))
        //->pipe(new Limit(array(10)))
        ->pipe($this->dataStore('totals_by_matter_and_account'));
    }
}

MatterTotalsByAccount.view.php

<?php 
    use \koolreport\widgets\koolphp\Table;
?>

<div class="report-content">
    <div class="text-center">
        <h1>Legal Fees Paid: xx/xx/xxxx to xx/xx/xxxx</h1>
        <p class="lead">Sorted by Account Name, Matter</p>
    </div>

    <?php
    ?>
    <?php
    Table::create(array(
        "dataStore"=>$this->dataStore('totals_by_matter_and_account'),
            "grouping"=>array(
                "accountName"=>array(
                    "calculate"=>array(
                        "{sumAmount}"=>array("sum","total")
                    ),
                    "top"=>"<b>{accountName}</b>",
                    "bottom"=>"<td align='right'><b>Total:</b></td><td align='right'><b>{sumAmount}</b></td>",
                    //"bottom"=>"<td><b>{accountName} TOTAL:</b></td><td><b>{sumAmount}</b></td>",
                ),
            ),
            "sorting"=>array(
                "matter"=>"asc"
            ),
            "showHeader"=>false,
            "showFooter"=>true,
            "columns"=>array(
                /*
                "accountName"=>array(
                    "label"=>"Account",
                ),
                */
                "matter"=>array(
                    "label"=>"Matter",
                    "cssStyle"=>"padding-left: 50px"
                ),
                "total"=>array(
                    "type"=>"number",
                    "label"=>"Total",
                    "prefix"=>"$",
                    "decimals"=>2,
                    "cssStyle"=>"text-align:right",
                    "footer"=>"sum",
                    "footerText" =>"<b><i>TOTAL FEES PAID</i></b>: <b>@value</b>"
                )
            ),
        "cssClass"=>array(
            "table"=>"table table-hover table-bordered"
        )
    ));
    ?>
</div>
David Winterburn commented on Aug 14, 2020

Hi Frank,

This could be because your php user doesn't have enough right to create a temporary folder in your system temp folder. Please try either of these solutions:

  1. Grant write access of the system temp folder for your php user.
  2. Use a local temp folder instead:
    $mattertotalsbyaccount
    ->cloudExport("MatterTotalsByAccount")
    ->chromeHeadlessio("my token")
    ->settings(array(
        'useLocalTempFolder' => true,
    ))
    ->pdf(array(
        "format"=>"A4",
        "landscape"=>false
    ))
    ->toBrowser("MatterTotalsByAccount.pdf");

Let us know how these works for you. Thanks!

Frank DiGiandomenico commented on Aug 14, 2020

Hi David, that took away my error, thank you! However, one more small thing - instead of the PDF displaying in a browser window (as it used to before I started receiving that error), it now just shows a Save dialog to save the PDF to my local PC. Is there a way to have the rendered PDF display in the browser window? Again, it used to do this, prior to me receiving that error you just helped with.

Also, I notice the tmp folder that's created is never automatically deleted/removed - is that the intention?

Frank DiGiandomenico commented on Aug 15, 2020

Nevermind the above - I updated user permissions and all is well again. Thank you for the help!

David Winterburn commented on Aug 17, 2020

Hi Frank,

To display the exported PDF file directly on browser you could add a second parameter to function toBrowser() like this:

    $report->run()
    ->export(...)
    ->pdf(...)
    ->toBrowser("MyReport.pdf", true);

Regarding local temporary folders, the current default option is to leave it. We will consider adding an auto delete option for it in case users doesn't want to manually delete them periodically. Thanks!

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
bug
help needed
solved

Export