PHP: Create reports with the PHPReport library
Long story short, I was looking for a framework for reporting on the web, with export to excel and pdf functions, after trying here and there, frameworks like Kooreport, with quite tempting functions, I finally found >PHPReport.
PHPReport has a feature for exporting to excel and pdf, and can also generate printouts based on templates from excel, after assessment, this feature is very useful for invoice printouts which can be modified as needed.
In that case, just step using PHPReport:
- Please download the library to https://github.com/vernes/PHPReport
- Run composer to download PHPExcel required library PHPReport
- Download also mpdf version 6.0 at the link http: //www.mpdf1.com/mpdf/index.php for the export to pdf function
- Put mpdf to vendor folder and rename folder to mpdf60
- Create a sample testing file with the following name print-pdf-sample.php
<?php
include 'PHPReport.php';
$rendererName = PHPExcel_Settings::PDF_RENDERER_MPDF;
//$rendererName = PHPExcel_Settings::PDF_RENDERER_DOMPDF;
//$rendererLibrary = 'tcPDF5.9';
$rendererLibrary = 'mpdf60';
//$rendererLibrary = 'domPDF0.6.0beta3';
$rendererLibraryPath = dirname(__FILE__).'/vendor/' . $rendererLibrary;
PHPExcel_Settings::setPdfRenderer($rendererName,$rendererLibraryPath);
//which template to use
if(isset($_GET['template']))
$template=$_GET['template'];
else
$template='invoice. xls';
//set absolute path to directory with template files
$templateDir='examples\\templates\\';
//we get some products, e.g. from database
$products=array(
array('description'=>'Example product','qty'=>2,'price'=>4.5,'total'=>9),
array('description'=>'Another product','qty'=>1,'price'=>13.9,'total'=>13.9),
array('description'=>'Super product!','qty'=>3,'price'=>1.5,'total'=>4.5),
array('description'=>'Yet another great product','qty'=>2,'price'=>10.8,'total'=>21.6),
array('description'=>'Awesome','qty'=>1,'price'=>19.9,'total'=>19.9)
);
//set config for reports
$config=array(
'template'=>$template,
'templateDir'=>$templateDir
);
$R=new PHPReport($config);
$R->load(array(
array(
'id'=>'inv',
'data'=>array('date'=>date('Y-m-d'), 'number'=>312,
'customerid'=>12,'orderid'=>517,
'company'=>'Example Inc.',
'address'=>'Some addresses',
'city'=>'Some City, 1122',
'phone'=>'+111222333'),
'format'=>array('date'=>array('datetime'=>'d/m/Y'))
),
array(
'id'=>'prod',
'repeat'=>true,
'data'=>$products,
'minRows'=>0,
'format'=>array(
'price'=>array('number'=>array('prefix'=>'$','decimals'=>2)),
'total'=>array('number'=>array('prefix'=>'$','decimals'=>2))
)
),
array(
'id'=>'total',
'data'=>array('price'=>68.9),
'format'=>array(
'price'=>array('number'=>array('prefix'=>'$','decimals'=>2))
)
)
)
);
echo $R->render('pdf');
exit();
Here sample file which can be put into practice right away, but minus the phpexcel and mpdf60 libraries.
Because it’s too big to include in the sample file. Please download and make sure the downloaded results are extracted according to the folder in the vendor.
Good luck
kalau data nya diganti dengan database