January 27, 2018
We have just released the Instant 2.0.0 with new features: Report Settings and new Exporter class. The Instant package is Free
to download and also be available in the KoolReport Pro 2.32.7.
Report Settings
As you may know that Instant
package helps us to use Widget
of KoolReport instantly without setting up a whole report. You can use the Widget like Table or Chart in right in your php file. However, due to not setting up report, the resources of Widget may not able to publish to assets
or public location which they can be accessed. In this new version, we allow Widget::create
function to receive the third parameter which is the settings of temporary report. This feature is extremely important if you are using modern MVC frameworks like Laravel, CodeIgniter.
Widget::create(Table::class,array(
"dataSource"=>array(
array("name"=>"Peter","age"=>35),
array("name"=>"Karl","age"=>32),
)
),array(
"assets"=>array(
"path"=>"../../public/assets",
"url"=>"/assets"
)
))
Exporter
New Exporter
class allows us to export any HTML
or PHP
file to PDF
or other format.
<?php
require_once "koolreport/autoload.php";
use \koolreport\instant\Exporter;
Exporter::export("/full/path/to/your/file.php")
->pdf(array(
"format"=>"A4",
"orientation"=>"portrait"
))
->toBrowser("myfile.pdf");
As you can see, now it is very easy to utilize the power of Export
package to export any file to PDF. It does not limit to export the report of KoolReport.
Summary
The new version of Instant
package extends the capability of using KoolReport power outside of its environment. This adds flexibility to the usage of KoolReport for general purpose. We are working hard to make KoolReport the best library for reporting as well as extend its usefulness.
<3 koolreport team
October 17, 2017
We are glad to let you know that we have released Export 2.0.0. In this version, we have solved one of the mysterious issues SSL Handshake.
SSL Handshake
In the previous version, we found that Export functionality works unstably with HTTPS protocols. Most of the cases, it works well. However in some cases, it does not load resources from HTTPS such as images, javascript, css. The SSL Handshake fails that causes interruption in loading those resources. We have solved the problem in this version. If you experience this issue, please upgrade.
Plan for next version
In the next version, we plan to make exporting job work without setting up a full report. In many cases, we just want to export a normal PHP page to PDF. We hope that by adding this new feature, the Export package will be more flexible and extensible in term of use case.
<3 koolreport team
October 17, 2017
This article guides you how to use KoolReport to convert any HTML to PDF even if the HTML is embedded with Javascript and CSS.
About KoolReport
KoolReport is an intuitive and open source PHP Reporting Framework. It is born to make task of building data reports easier and faster. It supports various database connections, powerful data processing and stunning data visualization.
PDF Exporting is one of the cool packages created for KoolReport. The package is built to support KoolReport in exporting reports to PDF. However it can be used for general purpose of converting HTML to PDF. What makes this exporting solution stand out is the ability to support embedded CSS and Javascript beside pure HTML.
Hand-on
Step 1: Create two files MyPage.php and MyPage.view.php
mypage/
├── MyPage.php
├── MyPage.view.php
└── index.php
The MyPage.php contains MyPage
class which is derived from KoolReport
.
<?php
require "../koolreport/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!
Export package
In above example, we demonstrated how to export HTML embedded with CSS and Javascript to PDF. The ability to run Javascript is very important and is the feature that you may not find in other HTML to PDF solutions. This feature allows you to include any kinds of javascript-based charts into your exported PDF. Here is an good example of exporting Javascript Google Charts to PDF.
You may get Export package separately or purchase the bundle of all our commercial packages, KoolReport Pro.
<3 koolreport team
May 17, 2017
"For a long time, I have looked for but did not find a good solution to export HTML embedded CSS and Javascript to PDF" - John Marshall, one of our customers.
Fig 1: KoolReport converts HTML+CSS+JS to PDF
The problem of John is a common problem of developers working with PDF. We have worked with many PDF libraries such as TCPDF, FPDF, html2pdf or dompdf and we encounter some problems:
- It is not intuitive. If you use FPDF or TCPDF, you will feel quite difficult to write content, set style and position element with pure PHP code only. The Html2Pdf and DomPDF provide a better solution which is converting HTML to PDF but they have second problem.
- Low level of HTML/CSS support. This problem is associate with Html2Pdf and DomPdf. In those libraries, You should not use the latest HTML or CSS because they will not be rendered all correctly. Always be careful. Sometime the code breaks, things are not displayed in PDF and it takes a lot of time to debug and find a work around in lower level of HTML/CSS.
- Javascript is not supported. Uhh! All of them have this issue. Javascript now is the base of many chart libraries. Google Charts, Morris, D3, Sparkline, all of them are using Javascript to generate charts and graphs. So using pure HTML to PDF solution, we must forget all those beautiful charts.
So we was trying to find solution for above problems, a library that we can write PDF file easily with latest HTML/CSS combined with Javascript. And it is now all possible with Export package of KoolReport.
Some simple code to show
This is the view MyReport.view.php
<html>
<body style="margin: 1in">
<div class="page-header">My sample report</div>
<h1>Welcome to KoolReport</h1>
<div class="page-footer">{pageNum}</div>
<body>
</html>
This shows how to initiate KoolReport and export to PDF.
<?php
$report = new MyReport;
$report->run();
$report->export()->pdf()->saveAs("MyReport.pdf");
Working example
Let example speaks for itself. Below is a working example of exporting a HTML page with embedded Google Charts.
Additional features:
Beside the crucial features above, the Export package supports other necessary settings:
- Set page size with predefined size (A3, A4, Letter etc..) or any custom size in
px
- Set page orientation (portrait or landscape)
- Set page margin
- Input repeated header text and footer text on each page
- Show page number as well as number of pages.
For more information on features and documentation, please visit Export Package.
Summary
Export package is an excellent solution for exporting report to PDF and other formats. Not only can it be used for exporting report, it can help you to export any kind of HTML page to PDF with ease. The price of package is low compared to the benefits it brings to you.
If you have any question regarding this package, do not hesitate to join our forum to get answer. We are happy to assist you.
<3 koolreport team