KoolReport's Forum

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

Report shows fine. pdf export won't actually export #473

Open Thom Jones opened this topic on on Sep 18, 2018 - 12 comments

Thom Jones commented on Sep 18, 2018

Hi - In generating a report, the report itself, along with a generated pie chart, works great. When I try to export, then, to pdf, I either generate errors (when rendering or doing a saveAs) or, I get a pdf with logo and headers but no date (when going toBrowser) This is what is looks like going to browser:

when trying to Render, it kicks a PHP Fatal error: Uncaught Error: Call to undefined method koolreport\export\File::render() And when doing a saveAs, I get:

failed to open stream:  Permission denied

Even though I have 777 permissions on the tmp directory AND the pdf actually gets placed there (and is actually the same as going to browser - logo and headers are there but no data).

phantomjs is installed with executable permissions.

I am confused here. Any ideas?

Thanks!

KoolReport commented on Sep 19, 2018

From what you have shown us, I think:

  1. Export package is working well ( proof that it is generate pdf through toBrowser())
  2. It seems that when you export, you forget to run() report before export (because there is no data show)
  3. There is no render() function when export, there is only saveAs() or toBrowser().
  4. The saveAs() generate error meaning there is no permission for php in the folder that you want to save PDF to. We just use the copy() function of PHP to copy file from tmp folder to the folder you want to save.
Thom Jones commented on Sep 20, 2018

Yeah, the export is working - it brings in the header info. This is my export.php (shows that I do the run():

<?php
require_once "topRestaurants.php";
$report = new topRestaurants;

$report->run()
->export('topRestaurantsPdf')
//->settings(array(
//    "useLocalTempFolder"=>true,
//))
->pdf(array(
    "format"=>"Letter",
    "orientation"=>"portrait"
))
->toBrowser("topRestaurants.pdf")
//->saveAs("topRestaurants.pdf")
;

I have the topRestaurantsPdf.view.php which I have checked for valid HTML and the topRestaurants.php (required) is the same that pulls data for the initial browser report. Any other thoughts?

Thom Jones commented on Sep 20, 2018

Oh, and I have no idea where I got the ->render possibility - I thought I had read somewhere that was usable but I must have read it wrong.

Thom Jones commented on Sep 24, 2018

I think I have figured out what the issue is, but I can't figure out how to resolve it.
Basically, I use the Input package to get a date range. So beginning date is

$this->params['dateRange'][0]
and ending date is
$this->params['dateRange'][1]

which works great for the initial view. When I get to export though, those dates don't seem to get passed to the export functionality. If I echo the dates in the echo is is giving me today from midnight to today at 23:59 which, since this is test data, has no values. Hence the empty chart and table. How can I get the Input values to pass through? Thanks for any assistance!

Thom Jones commented on Sep 26, 2018

So is there a way that I am missing to pass these input parameters from the Input Package to the Export package?

KoolReport commented on Sep 26, 2018

Please do this:

  1. On the button export, you put onclick="doExport()"
  2. This is the javascript code for doExport() function:
<script type="text/javascript">
function doExport()
{
    var _form = document.getElementById("parameterForm");
    _form.action = "export.php";
    _form.submit();
    _form.action = "index.php";
}
</script>

We assume your form has name of "parameterForm". What we have done here is when user hit export button, the form action will be change to export.php and intead of submitting form to index.php, it submit the export.php to do export. Then later the action is assigned back to "index.php"

Hope that helps.

Thom Jones commented on Sep 26, 2018

I must be missing something very basic. Added the js code and the onclick parameter. Made sure the id of the form is 'parameterForm'. No console errors showing up. But I still end up with the default date range of today and the org id I select on the initial form is blank. I echoed that into the pdf view script to see this:

So close here - love that the reports show up perfectly. The graphs and charts display great. Just missing that export piece to have a fantastic framework for the future.
Do I have to supply hidden variables back to the query building page perhaps? Thanks for the help - I just can't seem to figure this last piece out.

KoolReport commented on Sep 26, 2018

Could you please show me your code.

P/s: your case is very similar to this.

Thom Jones commented on Sep 26, 2018

I looked through that other thread and wasn't able to find anything else that might be thwarting me. So, here is the code. I appreciate you taking a look! index.php:

<?php 
require_once "topRestaurants.php";
$report = new topRestaurants;
$report->run();
?>
<!DOCTYPE >
<html>
    <head>
        <title>Athlete Spending</title>
        <link rel="stylesheet" href="/assets/bootstrap/css/bootstrap.min.css" />
        <link rel="stylesheet" href="/assets/bootstrap/css/bootstrap-theme.min.css" />
        <link rel="stylesheet" href="/assets/css/example.css" />
        <style>
            .box-container { max-width: 100%; }
        </style>
    </head>
    <body>      
        <div class="container box-container">
            <?php $report->render();?>
        </div>
    </body>
</html>

topRestaurants.php:

<?php

require_once ($_SERVER['DOCUMENT_ROOT']."/koolreport/autoload.php");

use \koolreport\KoolReport;

class topRestaurants extends KoolReport
{
    use \koolreport\export\Exportable;
    use \koolreport\clients\Bootstrap;
    use \koolreport\inputs\Bindable;
    use \koolreport\inputs\POSTBinding;
    use \koolreport\excel\ExcelExportable;
    
    public function settings()
    {
        //Get default connection from config.php
        $config = include ($_SERVER['DOCUMENT_ROOT']."/config.php");

        return array(
            "dataSources"=>array(
                "spend_data"=>$config["camp-dashboard"]
            ),
        );
    }   
    
    protected function defaultParamValues()
    {
        return array(
        "select"=>"",
        "chooseOrg"=>"",
        "startDatePicker"=>date("Y-m-d 00:00:00"),
        "endDatePicker"=>date("Y-m-d 23:59:59"),
        "dateRange"=>array(date("Y-m-d 00:00:00"),date("Y-m-d 23:59:59")),
        );
    }
    
    protected function bindParamsToInputs()
    {
        return array(
            "dateRange"=>"dateRange",
            "select",
            "multiSelect",
            "textBox",
            "radioList",
            "checkBoxList"=>"isActive",
            "startDatePicker",
            "endDatePicker",
            "singleSelect2",
            "multipleSelect2",
            "singleBSelect",
            "multipleBSelect",
            "rangeSliderOne",
            "rangeSliderTwo",
            "account_plan"=>"choosePlan",
            "active"=>"isActive",
            "chooseOrg",
        );
    }
    
    protected function setup()
    {
        $this->src('spend_data')
        ->params(array(
            ":orgid"=>$this->params["chooseOrg"],
            ":start"=>$this->params["dateRange"][0],
            ":end"=>$this->params["dateRange"][1],
            
        ))
        
        ->query("SELECT sum(a.amount) as rest_total, b.name, b.city FROM Transaction a, Restaurant b WHERE a.transaction_timestamp >= :start AND transaction_timestamp <= :end AND a.restaurant_id = b.id AND b.organization_id = :orgid AND b.active > 0 AND b.performance_table = 0 AND b.fueling_station = 0 GROUP BY a.restaurant_id ORDER BY rest_total DESC")
         ->pipe($this->dataStore('restaurant_spending'));       
        
        $this->src("spend_data")
        ->query("select id, name, timezone from Organizations ORDER BY name ASC")
 
        ->pipe($this->dataStore("selectOrg"));
    } 
}

topRestaurants.view.php:

<?php 
    use \koolreport\widgets\koolphp\Table;
    use \koolreport\widgets\google\PieChart;
    use \koolreport\inputs\Select;
    use \koolreport\inputs\DateRangePicker;
    
?>
<html>
    <head>
        <title>List of Athletes</title>
        <script type="text/javascript">
            function doExport()
            {
                var _form = document.getElementById("parameterForm");
                _form.action = "export.php";
                _form.submit();
                _form.action = "index.php";
            }
        </script>
    </head>
    <body>
        <link rel="stylesheet" href="/assets/css/example.css" />
        <div class="text-center" style="padding-bottom:0.8em;">
           <h1>Top Restaurant Spending</h1>
        </div>
        <form method="post" id="parameterForm">
          <fieldset>
          <div class="row">
          <div class="col-md-6 form-group">
            <label>School Name</label>
            <?php
              Select::create(array(
                "name"=>"chooseOrg",
                "dataStore"=>$this->dataStore("selectOrg"),
                "defaultOption"=>array("Choose School"=>""),
                "dataBind"=>array("text"=>"name","value"=>"id"),
                "attributes"=>array(
                "class"=>"form-control",
                )
              ));
              ?>
            </div>
            <div class="col-md-6 form-group">
               <label>DateRangePicker</label>
               <?php
               DateRangePicker::create(array(
               "name"=>"dateRange",
               ));
              ?>
            </div>
          </div>
         <div class="form-group">
           <button class="btn btn-primary">Submit</button>
         </div>
           </fieldset>
           </form>

<?php
PieChart::create(array(
    "dataStore"=>$this->dataStore('restaurant_spending'),
    //"width"=>"600px",
    //"height"=>"600px",
    "columns"=>array(
        "name"=>array(
            "label"=>"Name",
            "type"=>"string",
        ),
        "rest_total"=>array(
            "label"=>"Amount",
            "type"=>"number",
            "prefix"=>"$",
        )
    ),
    "options"=>array(
        "title"=>"Top Restaurant Spending"
    )
))
    
?>
<?php
Table::create(array(
    "dataStore"=>$this->dataStore('restaurant_spending'),
    "columns"=>array(
        "name"=>array(
            "label"=>"Name",
            "type"=>"string",
        ),
        "city"=>array(
            "label"=>"City",
            "type"=>"string",
        ),
        "rest_total"=>array(
            "label"=>"Amount",
            "type"=>"number",
            "prefix"=>"$",
                    )
    ),
    "cssClass"=>array(
        "table"=>"table table-hover table-bordered"
    )
));
?>

        <div class="text-center" style="padding-bottom:0.8em;">
            <a href="export.php" class="btn btn-primary" onclick="doExport()">Download PDF</a><br />
        </div>   
    </body>
</html>

And, finally, the export.php:

<?php
require_once "topRestaurants.php";
$report = new topRestaurants;

$report->run()
->export('topRestaurantsPdf')

->pdf(array(
    "format"=>"Letter",
    "orientation"=>"portrait"
))
->toBrowser("topRestaurants.pdf")
;

Thanks again!!

KoolReport commented on Sep 26, 2018

You change this:

<a href="export.php" class="btn btn-primary" onclick="doExport()">Download PDF</a>

to this:

<button class="btn btn-primary" onclick="doExport()">Download PDF</button>

Let me know if it works.

Thom Jones commented on Sep 26, 2018

Well, that certainly did the trick. Working now. Such a small thing to fix. Thanks so much!

KoolReport commented on Sep 26, 2018

Oh, great! Thank you very much for your tips. Really appreciated :)

By the way, small comment: you have repeated <html> <head> and <body>. As you see that you have those tags inside index.php and then the index.php calls $report->render(). The view of report contain <html><head> <body> will be rendered there. As a final result, you will have repeated <html><head> and <body>. You may remove those tags inside the view of report.

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