KoolReport's Forum

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

Normal report showing correct no. of rows but export to pdf showing all rows? #3009

Open AhmedHaroon opened this topic on on Mar 8, 2023 - 5 comments

AhmedHaroon commented on Mar 8, 2023

created a report with export to PDF & Excel, testing for PDF and it is not showing correct no. of rows, in normal layout when it renders in browser page, it is showing correct data.

parameters: From Membership and To Membership

when select 1 and 50 it is showing 3 pages in normal report view but when exporting to PDF it shows 11 pages.

please check below code and help. ( please note, do not confuse with file names which i have copied from an existing app, when this will be finalize, i will modify names accordingly. )

Controllers/Customersummarypdf.php

<?php

namespace App\Controllers;

require APPPATH . "reports/CustomerSummary/CustomerSummary.php";

class Customersummarypdf extends BaseController
{

    public function index()
	{
		$report = new \App\reports\CustomerSummary;
		$report->run()->render();
	}

    public function DownloadPDF() {

        $filename =  'membership_list_'.date("Y-d-m").'_'.date("h-i-s").'.pdf';

        $report = new \App\reports\CustomerSummary;
        $report->run()
        ->export('CustomerSummaryPdf')
        ->settings([
            "useLocalTempFolder"=>true,
            "autoDeleteLocalTempFile"=>true,
            "autoDeleteTempFile" => true,
            // "phantomjs"=>dirname(__FILE__, 3)."/vendor/koolreport/export/bin/phantomjs.exe",
            "resourceWaiting"=>2000,
            "serverLocalAddress" => "MyServer",
        ])
        ->pdf(array(
            "format"=>"A4",
            "orientation"=>"portrait",
            "footer"=>array(
                "height"=>"1cm",
                "contents"=>"<p style='font-size: 12px; font-weight: 500; font-family: sans-serif;'>Page {pageNum} of {numPages}</p>"
            ),
            "headerCallback" => "function(headerContent, pageNum, numPages){
                if (pageNum == 1) return '';
                return headerContent;
            }",
        ))
        ->toBrowser($filename);
    }

    public function DownloadExcel() {

        date_default_timezone_set("Asia/Karachi");
        $filename =  'customer_summary_'.date("Y-d-m").'_'.date("h-i-s").'.xlsx';

        $report = new \App\reports\CustomerSummary;
        $report->run();
        $report->exportToExcel()->toBrowser($filename);
    }
}

App/reports/CustomerSummary.php

<?php
namespace App\reports;

// we have copied below file in root path of our app
require_once ROOTPATH . "load.koolreport.php";

use \koolreport\KoolReport;
use \koolreport\processes\Sort;
use \koolreport\processes\Map;
use \koolreport\processes\Limit;
use \koolreport\processes\Filter;
use \koolreport\cube\processes\Cube;
use \koolreport\pivot\processes\Pivot;

class CustomerSummary extends \koolreport\KoolReport
{
    //use \koolreport\bootstrap4\Theme;
    use \koolreport\amazing\Theme;
    use \koolreport\codeigniter\Friendship;
    use \koolreport\inputs\Bindable;
    use \koolreport\inputs\POSTBinding;
    use \koolreport\export\Exportable;
    use \koolreport\excel\ExcelExportable;
    use \koolreport\excel\BigSpreadsheetExportable;

    protected function defaultParamValues()
    {
        return array(
            "from_membership"=>1,
            "to_membership"=>9999,
        );
    }

    protected function bindParamsToInputs()
    {
        return array(
            "from_membership"=>"from_membership",
            "to_membership"=>"to_membership",
        );
    }

    function setup()
    {
        ->query("SELECT membership_date, member_full_name, membership_no, allotment_no, unit_number FROM memberships_list WHERE
                    membership_no between :from_membership and :to_membership
        ")
        ->params(array(
            "from_membership"=>$this->params["from_membership"],
            "to_membership"=>$this->params["to_membership"]
        ))
        ->pipe(Sort::process([
            "membership_no"=>"asc"
        ]))
        ->pipe($this->dataStore("memship_no"));
        $this->src("default")->query("
            SELECT DISTINCT
                membership_no,
                membership_no
            FROM
                memberships
            ORDER BY membership_no
        ")
        ->pipe($this->dataStore("membershipLov"));
    }
}

App/reports/CustomerSummary.view.php

<?php
use \koolreport\widgets\koolphp\Table;
use \koolreport\inputs\DateRangePicker;
use \koolreport\inputs\Select2;

?>

<style>
    .amazing {
        width: 40%;
    }
    .select2 {
        width: 100% !important;
    }

    .sidebar {
        background: #ffffff !important;
    }
</style>
<div class='report-content'>
<br />
    <div class="text-center">
        <h2>Membership</h2>
        <a href="<?php echo base_url(); ?>/Customersummarypdf/DownloadPDF" class="btn btn-primary">Download PDF</a>
        <a href="<?php echo base_url(); ?>/Customersummarypdf/DownloadExcel" class="btn btn-primary">Download Excel</a>
    </div>
    <br />

    <form method="post">
        <div class="row">
            <div class="col-md-8 offset-md-2">
                <div class="form-group" style="display: flex; flex-direction: column; flex-wrap: nowrap; align-content: center; justify-content: center; align-items: center; margin-bottom: 1rem;">
                <div style="display: flex; width: 100%; flex-direction: row; flex-wrap: nowrap; justify-content: center; align-items: center;">
                    <strong style="margin-right: 15px;">From Membership</strong>
                    <?php
                    Select2::create(array(
                        "name"=>"from_membership",
                        "dataStore"=>$this->dataStore("membershipLov"),
                        "defaultOption"=>array(
                        ),
                        "dataBind"=>array(
                            "text"=>"membership_no",
                            "value"=>"membership_no",
                        ),
                        "attributes"=>array(
                            "class"=>"form-control",
                        )
                    ));
                    ?>
                </div>
                <div style="display: flex; width: 100%; flex-direction: row; flex-wrap: nowrap; justify-content: center; align-items: center;padding-top: 16px;">
                    <strong style="margin-right: 34px;">To Membership</strong>
                    <?php
                        Select2::create(array(
                            "name"=>"to_membership",
                            "dataStore"=>$this->dataStore("membershipLov"),
                            "defaultOption"=>array(
                            ),
                            "dataBind"=>array(
                                "text"=>"membership_no",
                                "value"=>"membership_no",
                            ),
                            "attributes"=>array(
                                "class"=>"form-control",
                            )
                        ));
                    ?>
                </div>
                <div class="form-group text-center" style="margin-left: 1rem;margin-bottom: 0rem;margin-top: 16px;">
                    <button class="btn btn-success"><i class="glyphicon glyphicon-refresh"></i> Load</button>
                </div>
            </div>
        </div>
    </form>
    </div>

<script>
        function Backonclick() {
            window.location.href = '<?php echo base_url(); ?>/customersummary/back';
        }
    </script>
    <?php
    if($this->dataStore("memship_no")->countData()>0)
    {
    Table::create(array(
        "dataStore"=>$this->dataStore("memship_no"),
        "cssClass"=>array(
            "table"=>"table table-bordered"
        ),
        "showFooter"=>true,
        "options"=>array(
            "paging"=>true,
        ),
        "columns"=>array(

            "membership_date"=>array(
                "cssStyle"=>"text-align:center",
                "label"=>"Membership Date",
            ),
            "member_full_name"=>array(
                "label"=>"Member Name",
                // "footerText"=>"<b>Total</b>"
            ),
            "membership_no"=>array(
                "cssStyle"=>"text-align:center",
                "label"=>"Membership #",
                // "type"=>"number",
                // "footer"=>"sum",
                // "footerText"=>"<b>@value</b>"
            ),
            "allotment_no"=>array(
                "cssStyle"=>"text-align:center",
                "label"=>"Allotment #",
                // "footer"=>"sum",
                // "footerText"=>"<b>@value</b>"
            ),
            "unit_number"=>array(
                "cssStyle"=>"text-align:center",
                "label"=>"Unit #",
                // "type"=>"number",
                // "footer"=>"sum",
                // "footerText"=>"<b>@value</b>"
            ),
        ),
        "paging"=>array(
            "pageSize"=>10,
        ),
    ));
    }
    else
    {
    ?>
        <div class="alert alert-warning">
            <i class="glyphicon glyphicon-info-sign"></i> Sorry, we found no orders.
        </div>
    <?php
    }
    ?>
</div>

App/reports/CustomerSummaryPdf.view.php

<?php
    use \koolreport\widgets\koolphp\Table;
?>
<html>
    <head>
        <style>
            .table {
                font-family: Arial, Helvetica, sans-serif;
                border-collapse: collapse;
                width: 100%;
            }

            .table td, .table th {
                border: 1px solid #ddd;
                padding: 8px;
            }

            .table tr:nth-child(even){
                background-color: #f2f2f2;
            }

           .table tr:hover {
                background-color: #ddd;
            }

            .table th {
                padding-top: 12px;
                padding-bottom: 12px;
                text-align: left;
                background: #1e3d73;
                color: white;
            }
            tr td:last-child {
                width: 14%;
            }
            th {
                font-size: 10px;
                font-weight: 600;
                font-family: sans-serif;
            }
            td {
                font-size: 10px;
                font-weight: 500;
                font-family: sans-serif;
            }
        </style>
    </head>
    <body style="margin:0.5in 1in 0.5in 1in">
        <div class="text-center">
            <center><h1 style="font-size: 20px; font-weight: 600; font-family: sans-serif;">Memberships Report</h1></center>
        </div>
        <?php
            Table::create(array(
                "dataStore"=>$this->dataStore("memship_no"),
                "cssClass"=>array(
                    "table"=>"table table-hover table-bordered"
                ),
                "showFooter"=>true,
                "options"=>array(
                    "paging"=>true,
                ),
                "columns"=>array(

                    "membership_date"=>array(
                        "cssStyle"=>"text-align:center",
                        "label"=>"Membership Date",
                    ),
                    "member_full_name"=>array(
                        "label"=>"Member Name"
                    ),
                    "membership_no"=>array(
                        "cssStyle"=>"text-align:center",
                        "label"=>"Membership #"
                    ),
                    "allotment_no"=>array(
                        "cssStyle"=>"text-align:center",
                        "label"=>"Allotment #"
                    ),
                    "unit_number"=>array(
                        "cssStyle"=>"text-align:center",
                        "label"=>"Unit #"
                    ),
                ),

            ));
        ?>
    </body>
</html>

regards

AhmedHaroon commented on Mar 8, 2023

i think CustomerSummaryPdf.view.php is getting data for my defaultParamValues() , am i right ?

in this situation, is there need of session variables or something else? please help.

regards

AhmedHaroon commented on Mar 9, 2023

we have modified code below to have session variables for PDF output, please advise to have better approach.

CustomerSummary.php

<?php
namespace App\reports;

// we have copied below file in root path of our app
require_once ROOTPATH . "load.koolreport.php";

use \koolreport\KoolReport;
use \koolreport\processes\Sort;
use \koolreport\processes\Map;
use \koolreport\processes\Limit;
use \koolreport\processes\Filter;
use \koolreport\cube\processes\Cube;
use \koolreport\pivot\processes\Pivot;

class CustomerSummary extends \koolreport\KoolReport
{
    use \koolreport\amazing\Theme;
    use \koolreport\codeigniter\Friendship;
    use \koolreport\inputs\Bindable;
    use \koolreport\inputs\POSTBinding;
    use \koolreport\export\Exportable;
    use \koolreport\excel\ExcelExportable;
    use \koolreport\excel\BigSpreadsheetExportable;

    protected function defaultParamValues()
    {
        $membershipstart = session()->get('from_membership');
        $membershipend = session()->get('to_membership');
        $from_membership = isset($membershipstart) ? $membershipstart : 1;
        $to_membership = isset($membershipend) ? $membershipend : 9999;
        return array(
            "from_membership"=> $from_membership,
            "to_membership"=>$to_membership,
        );
    }

    protected function bindParamsToInputs()
    {
        return array(
            "from_membership"=>"from_membership",
            "to_membership"=>"to_membership",
        );
    }

    function setup()
    {
        session()->set('from_membership',$this->params["from_membership"]);
        session()->set('to_membership',$this->params["to_membership"]);
        $membershipstart = session()->get('from_membership');
        $membershipend = session()->get('to_membership');
        $this->src("default")
        ->query("SELECT membership_date, member_full_name, membership_no, allotment_no, unit_number FROM memberships_list WHERE
                    membership_no between :from_membership and :to_membership
        ")
        ->params(array(
            "from_membership"=> $membershipstart,
            "to_membership"=>$membershipend 
        ))
        ->pipe(Sort::process([
            "membership_no"=>"asc"
        ]))
        ->pipe($this->dataStore("memship_no"));
        $this->src("default")->query("
            SELECT DISTINCT
                membership_no,
                membership_no
            FROM
                memberships
            ORDER BY membership_no
        ")
        ->pipe($this->dataStore("membershipLov"));
    }
}

regards

Sebastian Morales commented on Mar 9, 2023

I think there's a difference between the Table widget in your web and pdf views. In the web view your Table has a "paging" property (not sub property "paging" in "options") while in our pdf view the Table doesn't. Pls try this in your pdf view:

//MyReportPDF.view.php
Table::create(array(
    ...
    "paging" => true, // or: "paging" => array("pageSize" => 20) to set page size explicitly instead of using the default pageSize = 10
));
AhmedHaroon commented on Mar 9, 2023

thanks @Sebastian Morales for reply.

but i am not asking regarding pagination, i asked about the rows returned in PDF and Normal report are different before i used session variables.

regards

Sebastian Morales commented on Mar 9, 2023

Ah I see. In that case the problem is perhaps because of the export button:

        <a href="<?php echo base_url(); ?>/Customersummarypdf/DownloadPDF" class="btn btn-primary">Download PDF</a>

It's not exactly a button but an a href link. When clicking an a href there's no form submission like when clicking a button (inside a form). Thus, no inputs/parameters are submitted. My suggestion is to convert the export button to a real button with type submit and place it inside your form tag. Rgds,

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