Exporting Feature In KoolReport
November 10, 2017Exporting is a crucial feature in any reporting tools or framework and KoolReport is not exception. KoolReport is able to export report to PDF, Excel, JPG, PNG with the help of Excel
package and Export
package.
Export to Excel
The Excel
package support exporting KoolReport's data to Excel file. The package will export all dataStores of your report to excel sheets. Each dataStore per excel sheet.
To make your report able to Export to Excel, you add:
class MyReport extends \koolreport\KoolReport
{
use \koolreport\excel\ExcelExportable;
...
}
By adding the use \koolreport\excel\ExcelExportTable
, the report now has function exportToExcel()
function.
$report = new SaleReport;
$report->run()->exportToExcel()->toBrowser("myreport.xlsx");
Above code will export your report to excel file and push to browser for user to download.
Export to PDF
Let hand on an example to export a simple HTML page to PDF with Export
package
Step 1: Create two files MyPage.php
and MyPage.view.php
The structure of our file will be like below
mypage/
├── MyPage.php
├── MyPage.view.php
└── index.php
<?php
//MyPage.php
require "../koolreport/core/autoload.php";
class MyPage extends \koolreport\KoolReport
{
use \koolreport\export\Exportable;
}
The MyPage.view.php
is put in the same folder with MyPage.php
. This view file contains your content in form of HTML, CSS and Javascript that you want to export.
<html>
<head>
<title>Content that you want to convert to PDF</title>
</head>
<body>
<!-- CSS Style -->
<style>
p {font-size:20px;}
h1 {color:red}
</style>
<!-- Normal HTML content -->
<h1>Export HTML to PDF</h1>
<p>It is easy to convert HTML to PDF using KoolReport's Export package</p>
<p id="extra"></p>
<!-- Javascript embedded -->
<script type="text/javascript">
document.getElementById("extra").innerHTML = "Javascript is working";
</script>
<body>
</html>
Step 2: Export To PDF
To generate PDF file and push to browser so that users can download, you do:
<?php
// index.php
require "MyPage.php";
$mypage = new MyPage;
$mypage->export()
->pdf(array(
"format"=>"A4",
"orientation"=>"portrait"
))
->toBrowser("mypage.pdf");
Easy, is it? And if you want to save the file instead of pushing to browser, you do:
<?php
// index.php
require "MyPage.php";
$mypage = new MyPage;
$mypage->export()
->pdf(array(
"format"=>"A4",
"orientation"=>"portrait"
))
->saveAs("../my_folder/mypage.pdf");
It is all done. Super easy!
Summary
In this tutorial, we have gone through the Exporting feature in KoolReport. The Export to PDF feature requires Export
package and Export to Excel requires Excel
package. Although both of packages are not free, a little price for a completed solution is considered reasonable to the benefits it brings.
If you have any question, you may reply to us of this email.
See you in the next tutorial.
Resources
<3 koolreport team