Headers & Footers

This examples show how to set headers and footers for exported PDF

Customer Total Q 1 Q 2 Q 3 Q 4 H 1 H 2
Super Scale Inc. 4,139.92 0.00 3,582.16 0.00 4,767.40 3,582 4,767
Mini Caravy 3,992.60 0.00 3,992.60 0.00 0.00 3,993 0
Tekni Collectables Inc. 3,895.55 2,392.89 4,145.99 0.00 0.00 6,539 0
Gift Depot Inc. 3,816.99 0.00 4,149.74 0.00 3,109.89 4,150 3,110
La Corne D'abondance, Co. 3,763.20 0.00 3,338.31 1,960.80 4,267.47 3,338 6,228

The example demonstrate how to use CloudExport package with ChromeHeadless.io service to generate PDF and JPG. The example also demonstrates how to add header and footer to PDF. The example requires token key to run and you can follow this instruction in below link to get token key:

Get ChromeHeadless.io Token Key.

Enjoy!

<?php
require_once "MyReport.php";

$report = new MyReport;
$report->run()->render();
<?php
//Step 1: Load KoolReport
require_once "../../../load.koolreport.php";

use \koolreport\processes\ColumnMeta;
use \koolreport\processes\Limit;
use \koolreport\processes\Sort;
use \koolreport\processes\RemoveColumn;
use \koolreport\processes\OnlyColumn;
use \koolreport\processes\Map;
use \koolreport\cube\processes\Cube;
use \koolreport\core\Utility as Util;

//Step 2: Creating Report class
class MyReport extends \koolreport\KoolReport
{
	// use \koolreport\clients\Bootstrap;
	use \koolreport\cloudexport\Exportable;
	
    public function settings()
    {
        //Get default connection from config.php
        $config = include "../../../config.php";

        return array(
            "dataSources"=>$config
        );
    }   
    protected function setup()
    {
        $node = $this->src('salesCSV')
        
        ->pipe(new Map(array(
            '{value}' => function($row, $metaData) {
                $row['orderQuarter'] = 'Q ' . $row['orderQuarter'];
                return array($row);
            },
            '{meta}' => function($metaData) {
                $metaData['columns']['dollar_sales'] = array(
                    'type' => 'number',
                    // "prefix" => "$",
                    "decPoint" => ".",
                    "thoudsandSeparator" => ",",
                    "decimals" => 2,
                );
                $metaData['columns']['orderQuarter'] = array(
                    'type' => 'string',
                );
                return $metaData;
            },
        )))
        ;

        $node->pipe(new Cube(array(
            "rows" => "customerName",
            "column" => "orderQuarter",
            "sum" => "dollar_sales",
            "avg" => "dollar_sales",
            // "sum percent" => "dollar_sales",
            // 'debug' => true,
        )))
        

        ->pipe(new Map(array(
            '{value}' => function($row, $meta) {
                $row['H 1'] = Util::get($row, 'Q 1', 0) + Util::get($row, 'Q 2', 0);
                $row['H 2'] = Util::get($row, 'Q 3', 0) + Util::get($row, 'Q 4', 0);
                return $row;
            },
            '{meta}' => function($meta) {
                $cMetas = & $meta['columns'];
                $cMetas['Q 1']['type'] = 'number';
                $cMetas['Q 1']['decimals'] = 2;
                return $meta;
            }
        )))
        ->pipe(new Sort(array(
            '{{all}}' => 'desc'
        )))
        ->pipe(new Limit(array(
            5, 0
        )))
        ->pipe(new ColumnMeta(array(
            "{{all}}"=>array(
                "label"=>"Total",
            ),
            "customerName"=>array(
                "label"=>"Customer",
            ),
        )))
        ->saveTo($node2);
        
        $node2->pipe($this->dataStore('salesQuarterCustomer'));

        
        
        $node2->pipe(new RemoveColumn(array(
            "{{all}}", 'H 1', 'H 2'
        )))
        ->pipe(new Map([
            '{value}' => function($row) {
                foreach ($row as $k => $v)
                    if ($v == 0)
                        $row[$k] = null;
                return $row;
            }

        ]))
        ->pipe($this->dataStore('salesQuarterCustomerNoAll'));
        
        $node2->pipe(new OnlyColumn(array(
            'customerName', "{{all}}"
        )))->pipe($this->dataStore('salesQuarterCustomerAll'));


    } 

}
<?php
require_once "MyReport.php";
$report = new MyReport;
$report->run();
/**
 * Follow the instruction here to get token key
 * https://www.koolreport.com/docs/cloudexport/chromeheadlessio/#get-token-key 
 */
$secretToken = 'my_cloud_export_secret_token';
$type=isset($_GET['type']) ? $_GET['type'] : 'PDF';
$settings = [
	// 'useLocalTempFolder' => true,
	"pageWaiting" => "networkidle2", //load, domcontentloaded, networkidle0, networkidle2
];
if ($type === 'cloudJPG') {
    $report->cloudExport("MyReportPDF")
    ->chromeHeadlessio($secretToken)
	->settings($settings)
    ->jpg(array(
        // "format"=>"A4",
        // "fullPage" => false
    ))
    ->toBrowser("MyReport.jpg")
    ;
} else if ($type === 'cloudPDF') {
    $pdfOptions = [
        "format"=>"A4",
        'landscape'=>false,
        // 'displayHeaderFooter' => true,
        // 'headerTemplate' => '
            // <div id="header-template" 
                // style="font-size:10px !important; color:#808080; padding-left:10px">
                // <span>Footer command: </span>
                // <span class="date"></span>
                // <span class="title"></span>
                // <span class="url"></span>
                // <span class="pageNumber"></span>
                // <span class="totalPages"></span>
            // </div>
        // ',
        // 'footerTemplate' => '
            // <div id="footer-template" 
                // style="font-size:10px !important; color:#808080; padding-left:10px">
                // <span>Footer command: </span>
                // <span class="date"></span>
                // <span class="title"></span>
                // <span class="url"></span>
                // <span class="pageNumber"></span>
                // <span class="totalPages"></span>
            // </div>
        // ',
        // 'margin' => [
            // 'top'    => '100px',
            // 'bottom' => '200px',
            // 'right'  => '30px',
            // 'left'   => '30px'
        // ],
        // "noRepeatTableHeader" => true,
        "noRepeatTableFooter" => true,
    ];
    $report->cloudExport("MyReportPDF")
    ->chromeHeadlessio($secretToken)
    ->settings($settings)
    ->pdf($pdfOptions)
    ->toBrowser("MyReport.pdf")
    ;
} 
<?php
use \koolreport\datagrid\DataTables;
use \koolreport\morris_chart;
use \koolreport\sparklines;
use \koolreport\widgets\google;
use \koolreport\widgets\koolphp\Table;
use \koolreport\core\Utility as Util;
?>
<form method="post">
	<div class="report-content">
		<div class="text-center">
			<h1>Headers & Footers</h1>
			<p class="lead">
				This examples show how to set headers and footers for exported PDF
			</p>
			<button type="submit" class="btn btn-primary" formaction="export.php?type=cloudPDF">
				Cloud PDF</button>
			<button type="submit" class="btn btn-primary" formaction="export.php?type=cloudJPG">
				Cloud JPG</button>
		</div>
		
		<?php
		$ds = $this->dataStore('salesQuarterCustomer');
		DataTables::create(array(
			'name' => 'salesQuarterCustomer',
			// "dataSource" => $data, 
			"dataSource" => $ds,
			// "columns" => ['customerName'],
			"options" => array(
				"searching" => true,
				"paging" => true,
				"colReorder" => true,
				// "ordering" => false,
				"order" => [],
				// "order" => [[0, 'desc']],
				// 'columnDefs' => array(
				//     array(
				//         'type' => 'customType',
				//         'targets' => 0, //target the first column
				//     )
				// )
			),
			// "columns"=>array(
			//     "customerName" => array(
			//         "label" => "Customer",
			//     ),
			//     "Q 1" => array(
			//         "footer" => "sum",
			//         "footerText"=>"<b>Total: @value</b>",
			//     )
			// ),
			"showFooter" => true,
			// "paging" => array(
			//   "pageSize" => 2
			// )
			"searchOnEnter" => true,
			"searchMode" => "OR",
		));
		
		google\LineChart::create(array(
			"dataStore" => $this->dataStore('salesQuarterCustomerNoAll'),
			"options" => array(
				'title' => 'Top 5 Customers\' Quarterly Sales',
				'isStacked' => true,
				// 'legend' => 'none',
				'pointShape' => 'circle',
				'pointSize' => 10,
				'hAxis' => [
					// 'textPosition' => 'none',
					'showTextEvery' => 4
				],
				'interpolateNulls' => true,
			),
			// 'columns' => array('customerName', 'Q 1'),
			"width" => '100%',
			// 'height'=>'400px',
		));
		
		google\PieChart::create(array(
			"dataStore" => $this->dataStore('salesQuarterCustomerAll'),
			"options" => array(
				'title' => 'Top 5 Customers\' Yearly Sales',
				// 'legend' => 'bottom',
				// 'is3D' => true,
				'chartArea' => array(
					// 'height' => '90%'
					
				),
				
			),
			"width" => '100%',
			// 'height'=>'600px',
		));
		?>
	</div>
</form>
<?php
    use \koolreport\widgets\google;
    use \koolreport\datagrid\DataTables;
?>
<style>
    @media  print {
      #table1 .break-row td {
        padding: 0 !important;
      }

      * {
        -webkit-print-color-adjust: exact !important;
      }

      #salesQuarterCustomer td	{
        // background-color: #b1dfbb !important;
      }

    }
</style>
<html>
	<head>
		<title>Customer Sales</title>
	</head>
  <body style='margin: 1in'>
    <header>
        <div style="font-size:10px !important; color:#808080; padding-left:10px; width:575px;">
			<span style='position: relative; top: 10px; float: right'>
				Title: <span class="title"></span> || 
				Page: <span class="pageNumber"></span> || 
				Total pages: <span class="totalPages"></span> || 
				Date: <span class="date"></span>
			</span>
            <span style=''>
				<img src='bar.png' height='20px' style='position:relative; top:5px'/>
				KoolReport 
			</span>
        </div>
    </header>
    <footer>
		<div style="font-size:10px !important; color:#808080; padding-left:10px; width:575px;">
			<span style='position: relative; top: 10px; float: right'>
				Title: {title} || 
				Page: {pageNumber} || 
				Total pages: {totalPages} || 
				Date: {date}
			</span>
            <span style=''>
				<img src='bar.png' height='20px' style='position:relative; top:5px'/>
				KoolReport 
			</span>
        </div>
    </footer> 
	<?php 
		$ds = $this->dataStore('salesQuarterCustomer');
		DataTables::create(array(
			'name' => 'salesQuarterCustomer',
			// "dataSource" => $data, 
			"dataSource" => $ds,
			// "columns" => ['customerName'],
			"options" => array(
				"searching" => true,
				"paging" => true,
				"colReorder" => true,
				// "ordering" => false,
				"order" => [],
				// "order" => [[0, 'desc']],
				// 'columnDefs' => array(
				//     array(
				//         'type' => 'customType',
				//         'targets' => 0, //target the first column
				//     )
				// )
			),
			// "columns"=>array(
			//     "customerName" => array(
			//         "label" => "Customer",
			//     ),
			//     "Q 1" => array(
			//         "footer" => "sum",
			//         "footerText"=>"<b>Total: @value</b>",
			//     )
			// ),
			"showFooter" => true,
			// "paging" => array(
			//   "pageSize" => 2
			// )
			"searchOnEnter" => true,
			"searchMode" => "OR",
		));
		google\LineChart::create(array(
			"dataStore"=>$this->dataStore('salesQuarterCustomerNoAll'),
			"options"=>array(
			  'title' => 'Top 5 Customers\' Quarterly Sales',
			  'isStacked' => true,
			  'interpolateNulls' => true,
			),
			"width"=>'100%',
			// 'height'=>'400px',
		));
		google\PieChart::create(array(
			"dataStore"=>$this->dataStore('salesQuarterCustomerAll'),
			"options"=>array(
			  'title' => 'Top 5 Customers\' Yearly Sales',
			  // 'legend' => 'bottom',
			  // 'is3D' => true,
			),
			"width"=>'100%',
			// 'height'=>'300px',
		));
	?>
  </body>
</html>

What People Are Saying

"KoolReport helps me very much in creating data report for my corporate! Keep up your good work!"
-- Alain Melsens

"The first use of your product. I was impressed by its easiness and powerfulness. This product is a great and amazing."
-- Dr. Lew Choy Onn

"Fantastic framework for reporting!"
-- Greg Schneider

Download KoolReport Get KoolReport Pro