KoolReport's Forum

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

Export to pdf #2062

Open Augustine Arthur opened this topic on on May 9, 2021 - 20 comments

Augustine Arthur commented on May 9, 2021

I have a report designed in the cube package. When an option say year 2019 is checked the report rely gives me the correct result. However if I use the export to pdf functionality the exported result gives me report for the whole range of options for the years (eg 2019, 2020, 2021). Please any help so as to only export the result for the selected year to pdf. Thank you.

Sebastian Morales commented on May 10, 2021

Pls put your export button inside your form tag and see how it goes. Tks,

Augustine Arthur commented on May 10, 2021

The export button is actually within the form tag of the view php.

Sebastian Morales commented on May 11, 2021

Would you mind posting your report's setup and view code? Rgds,

Augustine Arthur commented on May 11, 2021

Please these are the files. Thank you

  1. Index.php
<?php
    require_once "RevenueQuarters.php";
    $salesYear = isset($_POST['salesYear']) ? $_POST['salesYear'] : array(2017, 2018, 2019, 2020, 2021, 2022);
	$report = new RevenueQuarters(array(
        'salesYear' => $salesYear
    ));
    echo $report->run()->render(); ?>
  1. RevenueQuarters.php
<?php
require_once "../load.koolreport.php";
use \koolreport\processes\ColumnMeta;
use \koolreport\processes\Limit;
use \koolreport\processes\Sort;
use \koolreport\processes\TimeBucket;
use \koolreport\processes\RemoveColumn;
use \koolreport\processes\OnlyColumn;
use \koolreport\processes\Filter;
use \koolreport\processes\ValueMap;
use \koolreport\cube\processes\Cube;
use \koolreport\core\Utility;

class RevenueQuarters extends koolreport\KoolReport
{
	use \koolreport\export\Exportable;
	use \koolreport\clients\Bootstrap;
	use \koolreport\inputs\Bindable;

   use \koolreport\inputs\POSTBinding;
   
    function settings()
    {
        return array(
            "dataSources" => array(
                "dollarsales"=>array(
                    "connectionString"=>"mysql:host=localhost;dbname=mlnrgh",
                    "username"=>"root",
                    "password"=>"",
                    "charset"=>"utf8"    
                ), 
            )
        );
    }
	
    Protected function setup()
	
    {
		//$salesYear = $this->params['salesYear'];
        $salesYear = isset($_POST['salesYear']) ? 
        $_POST['salesYear'] : array(2017, 2018, 2019, 2020, 2021, 2022);
		$node=$this->src('dollarsales')
        ->query("SELECT QUARTER(Revenue_Date) as 'Q', YEAR(Revenue_Date) as 'Y', Region, Revenue_Source, Amount FROM revenue_summary")
        ->pipe(new ColumnMeta(array(
            "Amount"=>array(
                'type' => 'number',
                "prefix" => "ยข",
            ),
        )))
         ->pipe(new ValueMap(array( 
            'Q' => array(
                '{func}' => function ($value) {
                    return 'Q' . $value;
                },
                "{meta}" => array(
                    "type" => "string"
                ),
            )
        )));
        
        $filters = array('or');
       foreach ($salesYear as $year)
            array_push($filters, array('Y', '=', ''.$year));
        $node = $node->pipe(new Filter($filters));
        
        $node->pipe($this->dataStore('salesFilter'));
        
        $node->pipe(new Cube(array(
            "row" => "Revenue_Source",
            "column" => "Q",
            "sum" => "Amount"
        )))
        ->pipe(new Sort(array(
            '{{all}}' => 'asc'
        )))
        ->pipe(new Limit(array(
            20, 0
        )))->pipe(new ColumnMeta(array(
            "{{all}}"=>array(
                "label"=>"Total",
            ),
            "Revenue_Source"=>array(
                "label"=>"Revenue",
            ),
        )))->saveTo($node2);
        
        $node2->pipe($this->dataStore('salesQuarterProductName'));
        
        $node2->pipe(new RemoveColumn(array(
           "{{all}}"
       )))
        ->pipe($this->dataStore('salesQuarterProductNameNoAll'));
        
       $node2->pipe(new OnlyColumn(array(
            'Region', "{{all}}"
        )))->pipe($this->dataStore('salesQuarterProductNameAll'));
        
    }
}
  1. RevenueQuarters.view.php
<?php
    use \koolreport\widgets\google;
    use \koolreport\widgets\koolphp\Table;
	use \koolreport\widgets\google\ColumnChart;
	use \koolreport\inputs\Select;
    use \koolreport\clients\bootstrap;
?>

<div class="report-content">
  
    <div class="text-center">
       <h1>Revenue Summary by Regions Per Quarter </h1>
        <p class="lead">
           
        </p>
    </div>
    <?php
        $salesYear = isset($_POST['salesYear']) ? 
            $_POST['salesYear'] : array(2017, 2018, 2019, 2020, 2021, 2022);
    ?>
    <form method="post" class="text-center">
        <div class="form-group">
            <span style="margin-left:10px;">
                <input id="y2017" type="checkbox" name="salesYear[]" value="2017"
                <?php echo in_array(2017, $salesYear) ? 'checked' : '' ?> />
                <label for="y2017">2017</label>
            </span>
            <span style="margin-left:10px;">
                <input id="y2018" type="checkbox" name="salesYear[]" value="2018" 
                <?php echo in_array(2018, $salesYear) ? 'checked' : '' ?> />
                <label for="y2018">2018</label>
            </span>
            <span style="margin-left:10px;">
                <input id="y2019" type="checkbox" name="salesYear[]" value="2019" 
                <?php echo in_array(2019, $salesYear) ? 'checked' : '' ?> />
                <label for="y2019">2019</label>
            </span>
			<span style="margin-left:10px;">
                <input id="y2020" type="checkbox" name="salesYear[]" value="2020" 
                <?php echo in_array(2020, $salesYear) ? 'checked' : '' ?> />
                <label for="y2020">2020</label>
            </span>
			<span style="margin-left:10px;">
                <input id="y2021" type="checkbox" name="salesYear[]" value="2021" 
                <?php echo in_array(2021, $salesYear) ? 'checked' : '' ?> />
                <label for="y2021">2021</label>
            </span>
			<span style="margin-left:10px;">
                <input id="y2022" type="checkbox" name="salesYear[]" value="2022" 
                <?php echo in_array(2022, $salesYear) ? 'checked' : '' ?> />
                <label for="y2022">2022</label>
            </span>
        </div>
        <div class="form-group">
            <button type="submit" class="btn btn-primary">Submit</button>
	<a href="export.php" class="btn btn-primary">Download PDF</a>	
        </div>
		
    </form>
    <?php 
	
      Table::create(array(
        "dataStore" => $this->dataStore('salesQuarterProductName'),
      ));
    ?>
    <div class='row'>
        <div class="col-md-6">
            <?php 
            google\BarChart::create(array(
                "dataStore"=>$this->dataStore('salesQuarterProductNameNoAll'),
                "options"=>array(
                'title' => 'Barchart Showing Revenue by Regions',
                'isStacked' => true
                ),
                "width"=>'100%',
                // 'height'=>'400px',
            ));
            ?>
        </div>
        <div class="col-md-6">
            <?php 
            google\PieChart::create(array(
                "dataStore"=>$this->dataStore('salesQuarterProductNameAll'),
                "options"=>array(
                'title' => 'Piechart Showing Revenue by Regions',
                ),
                "width"=>'100%',
                // 'height'=>'300px',
            ));
            ?>
        </div>
    </div>

</div>
  1. RevenueQuartersPdf.view.php
<?php
    use \koolreport\widgets\koolphp\Table;
    use \koolreport\widgets\google\ColumnChart;
	use \koolreport\widgets\google\PieChart;
?>
<html>
    <body style="margin:0.5in 1in 0.5in 1in">
        <link rel="stylesheet" href="../../assets/bootstrap3/bootstrap.min.css" />
        <link rel="stylesheet" href="../../assets/bootstrap3/bootstrap-theme.min.css" />   
       <div class="page-header" style="text-align:right"><i>Revenue Summary</i></div>
        <div class="page-footer" style="text-align:right">{pageNum}</div>
		
        
        <hr/>

<div class="report-content">
   
	 <?php
	//$salesYear = array(2017, 2018, 2019, 2020, 2021, 2022);
     //  $salesYear = isset($_POST['salesYear']) ? 
      //  $_POST['salesYear'] : array(2017, 2018, 2019, 2020, 2021, 2022);
		// foreach ($_POST as $key => $value) {
        // if (strpos($key, "salesYear") === true) {
            // echo $value;
        // }
    // }
		
    ?> 
    <div class="text-center">
        <h1>Revenue Summary By Region</h1> 
		<?php
//foreach ($salesYear as $key => $value) {
    //    if($value['cust_type'] == 'Corporate')
	//  if($value == $salesYear && $value==1)
     //     {
       //         echo $value;
       //     }
      //
 // echo " ";
 //   }
?>

<?php
		
        // if (isset($_POST['salesYear'])){
// echo $_POST['salesYear']; // Displays value of checked checkbox.
// }
		// else{
			// echo 2019;
		// }
     ?>
	 </h1>
        <p class="lead">
        
        </p>
    </div>
	

    <?php 
      Table::create(array(
        "dataStore" => $this->dataStore('salesQuarterProductName'),
		//WHERE salesYear  !checked;
      ));
    ?>
	
    <div class='row'>
        <div class="col-md-6">
            <?php 
			
            ColumnChart::create(array(
                "dataStore"=>$this->dataStore('salesQuarterProductNameNoAll'),
                "options"=>array(
                'title' => 'Barchart Showing Revenue Summary Ahafo Region',
                'isStacked' => true
                ),
                "width"=>'100%',
                // 'height'=>'400px',
            ));
            ?>
        </div>
        <div class="col-md-6">
            <?php 
            PieChart::create(array(
                "dataStore"=>$this->dataStore('salesQuarterProductNameAll'),
                "options"=>array(
                'title' => 'Piechart Showing Revenue Summary Ahafo Region',
                ),
                "width"=>'100%',
                // 'height'=>'300px',
            ));
			
            ?>
			
        </div>
    </div>

</div>
</body>
</html>
  1. export.php
<?php
require_once "RevenueQuarters.php";
$report = new RevenueQuarters;

$report->run()
->export('RevenueQuartersPdf')
->pdf(array(
    "format"=>"A4",
    "orientation"=>"portrait",
    "zoom"=>2
))

->toBrowser("Revenue_Quarters.pdf", true);
Sebastian Morales commented on May 12, 2021

Pls add the following command to the beginning of your pdf view file:

    //RevenueQuartersPdf.view.php
    print_r($_POST);
    ...

Then run export and let us know the result of the $_POST print. Tks,

Augustine Arthur commented on May 12, 2021

When I added the code to the top of the file, the result of the export just prints Array ( ) on top of the page with still all the results of the record for all the years.

Augustine Arthur commented on May 13, 2021

When I added the code to the top of the //RevenueQuartersPdf.view.php file, the result of the export just prints Array ( ) on top of the page and the results of the record for all the years regardless of which of the years I selected. Thank you.

Sebastian Morales commented on May 13, 2021

Oh, I missed this detail with your export button:

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

It's not really a button but an <a> tag which doesn't submit the form to "export.php", only link to it. Pls replace it with this button and see how it goes:

  <button formaction="export.php" class="btn btn-primary">Download PDF</button>
Augustine Arthur commented on May 13, 2021

Hi Sebastian, I have replaced it but the problem still exist. As I explained when I check any of the years , I expect it to export to Pdf records for only the selected years but it export all the results of the years. I guess we have to do something about the datastore.

Sebastian Morales commented on May 14, 2021

After you change the Export a link to a Export button, what is the result of print_r($_POST) in your pdf view?

Augustine Arthur commented on May 14, 2021

It exports all the data regardless of which year I check or select. The print_r($Post) prints Array() on the Pdf view. What I expect to achieve is just export to Pdf only the results of the selected year.

Sebastian Morales commented on May 17, 2021

Pls show us your report view code (not pdf view). Tks,

Augustine Arthur commented on May 18, 2021

Pls this is the report view code.

RevenueQuarters.view.php
<?php
    use \koolreport\widgets\google;
    use \koolreport\widgets\koolphp\Table;
	use \koolreport\widgets\google\ColumnChart;
	use \koolreport\inputs\Select;
    use \koolreport\clients\bootstrap;
?>

<div class="report-content">
  
    <div class="text-center">
       <h1>Revenue Summary by Regions Per Quarter </h1>
        <p class="lead">
           
        </p>
    </div>
    <?php
        $salesYear = isset($_POST['salesYear']) ? 
            $_POST['salesYear'] : array(2017, 2018, 2019, 2020, 2021, 2022);
    ?>
    <form method="post" class="text-center">
        <div class="form-group">
            <span style="margin-left:10px;">
                <input id="y2017" type="checkbox" name="salesYear[]" value="2017"
                <?php echo in_array(2017, $salesYear) ? 'checked' : '' ?> />
                <label for="y2017">2017</label>
            </span>
            <span style="margin-left:10px;">
                <input id="y2018" type="checkbox" name="salesYear[]" value="2018" 
                <?php echo in_array(2018, $salesYear) ? 'checked' : '' ?> />
                <label for="y2018">2018</label>
            </span>
            <span style="margin-left:10px;">
                <input id="y2019" type="checkbox" name="salesYear[]" value="2019" 
                <?php echo in_array(2019, $salesYear) ? 'checked' : '' ?> />
                <label for="y2019">2019</label>
            </span>
			<span style="margin-left:10px;">
                <input id="y2020" type="checkbox" name="salesYear[]" value="2020" 
                <?php echo in_array(2020, $salesYear) ? 'checked' : '' ?> />
                <label for="y2020">2020</label>
            </span>
			<span style="margin-left:10px;">
                <input id="y2021" type="checkbox" name="salesYear[]" value="2021" 
                <?php echo in_array(2021, $salesYear) ? 'checked' : '' ?> />
                <label for="y2021">2021</label>
            </span>
			<span style="margin-left:10px;">
                <input id="y2022" type="checkbox" name="salesYear[]" value="2022" 
                <?php echo in_array(2022, $salesYear) ? 'checked' : '' ?> />
                <label for="y2022">2022</label>
            </span>
        </div>
        <div class="form-group">
            <button type="submit" class="btn btn-primary">Submit</button>
	<a href="export.php" class="btn btn-primary">Download PDF</a>	
        </div>
		
    </form>
    <?php 
	
      Table::create(array(
        "dataStore" => $this->dataStore('salesQuarterProductName'),
      ));
    ?>
    <div class='row'>
        <div class="col-md-6">
            <?php 
            google\BarChart::create(array(
                "dataStore"=>$this->dataStore('salesQuarterProductNameNoAll'),
                "options"=>array(
                'title' => 'Barchart Showing Revenue by Regions',
                'isStacked' => true
                ),
                "width"=>'100%',
                // 'height'=>'400px',
            ));
            ?>
        </div>
        <div class="col-md-6">
            <?php 
            google\PieChart::create(array(
                "dataStore"=>$this->dataStore('salesQuarterProductNameAll'),
                "options"=>array(
                'title' => 'Piechart Showing Revenue by Regions',
                ),
                "width"=>'100%',
                // 'height'=>'300px',
            ));
            ?>
        </div>
    </div>

</div>
Sebastian Morales commented on May 19, 2021

Didn't I ask you to replace this <a> export link:

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

with an export button like this:

<button formaction="export.php" class="btn btn-primary">Download PDF</button>

Pls replace it and click export button and let us know the result. Tks,

Augustine Arthur commented on May 19, 2021

Great !! it has worked. Thank you so much. That's fantastic. Pls how can I also let the years selected to be displayed in my title in the exported pdf view. For example if I select 2019 , I want my title to read "Quarterly report for 2019". Thank you.

Sebastian Morales commented on May 20, 2021

Pls use the following code just before your title

        $salesYear = isset($_POST['salesYear']) ? 
            $_POST['salesYear'] : array(2017, 2018, 2019, 2020, 2021, 2022);
        $salesYearStr = explode(", ", $salesYear);

Then use $salesYearStr for your title. Rgds,

Augustine Arthur commented on May 20, 2021

I have implemented your code however when I run the export I get this error message "Warning: explode() expects parameter 2 to be string, array given inC:\xampp\htdocs\koolreport\koolreport\pmmc\RevenueQuartersPdf.view.php on line 32"

//I have included the RevenueQuartersPdf.view.php code below: <?php

use \koolreport\widgets\koolphp\Table;
use \koolreport\widgets\google\ColumnChart;
use \koolreport\widgets\google\PieChart;

?> <html>

<body style="margin:0.5in 1in 0.5in 1in">
    <link rel="stylesheet" href="../../assets/bootstrap3/bootstrap.min.css" />
    <link rel="stylesheet" href="../../assets/bootstrap3/bootstrap-theme.min.css" />   
   <div class="page-header" style="text-align:right"><i>Revenue Summary</i></div>
    <div class="page-footer" style="text-align:right">{pageNum}</div>
	
    
    <hr/>
$value) { // if (strpos($key, "salesYear") === true) { // echo $value; // } // } ?>

Revenue Summary By Region

$value) { // if($value['cust_type'] == 'Corporate') // if($value == $salesYear && $value==1) // { // echo $value; // } // // echo " "; // } ?>

$this->dataStore('salesQuarterProductName'), //WHERE salesYear !checked; )); ?>
$this->dataStore('salesQuarterProductNameNoAll'), "options"=>array( 'title' => 'Barchart Showing Revenue Summary Ahafo Region', 'isStacked' => true ), "width"=>'100%', // 'height'=>'400px', )); ?>
$this->dataStore('salesQuarterProductNameAll'), "options"=>array( 'title' => 'Piechart Showing Revenue Summary Ahafo Region', ), "width"=>'100%', // 'height'=>'300px', )); ?>

</body> </html>

Augustine Arthur commented on May 20, 2021

I have implemented your code however when I run the export I get this error message "Warning: explode() expects parameter 2 to be string, array given inC:\xampp\htdocs\koolreport\koolreport\pmmc\RevenueQuartersPdf.view.php on line 32"

//I have included the RevenueQuartersPdf.view.php code below:

<?php

use \koolreport\widgets\koolphp\Table;
use \koolreport\widgets\google\ColumnChart;
use \koolreport\widgets\google\PieChart;

?> <html>

<body style="margin:0.5in 1in 0.5in 1in">
    <link rel="stylesheet" href="../../assets/bootstrap3/bootstrap.min.css" />
    <link rel="stylesheet" href="../../assets/bootstrap3/bootstrap-theme.min.css" />   
   <div class="page-header" style="text-align:right"><i>Revenue Summary</i></div>
    <div class="page-footer" style="text-align:right">{pageNum}</div>
	
    
    <hr/>
$value) { // if (strpos($key, "salesYear") === true) { // echo $value; // } // } ?>

Revenue Summary By Region

$value) { // if($value['cust_type'] == 'Corporate') // if($value == $salesYear && $value==1) // { // echo $value; // } // // echo " "; // } ?>

$this->dataStore('salesQuarterProductName'), //WHERE salesYear !checked; )); ?>
$this->dataStore('salesQuarterProductNameNoAll'), "options"=>array( 'title' => 'Barchart Showing Revenue Summary Ahafo Region', 'isStacked' => true ), "width"=>'100%', // 'height'=>'400px', )); ?>
$this->dataStore('salesQuarterProductNameAll'), "options"=>array( 'title' => 'Piechart Showing Revenue Summary Ahafo Region', ), "width"=>'100%', // 'height'=>'300px', )); ?>

</body> </html>

Sebastian Morales commented on May 21, 2021

Oh sorry, it should have been implode(", ", $salesYear); instead of explode. Implode converts an array to a string while explode does the opposite. Rgds,

Augustine Arthur commented on May 21, 2021

Thank you it works with implode. Thank you once again.

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
solved

None