<img alt="" src="https://secure.leadforensics.com/150446.png " style="display:none;">

Integration of Jasper Report – PHP CodeIgniter Web Framework

Archana Mulye Feb 18, 2016

PHP Jasper report

If you are dealing with billing or management projects where reporting is required with simple pdf formatted files, you need a quick way to interact with jasper reports along with tcpdf helper class file in PHP CodeIgniter Framework. Here’s how you can integrate Jasper Report with PHP CodeIgniter Web Framework:

Suppose you have a html (or any other) template file which has to be integrated with dynamic data into jasper report:

$template = file_get_contents("public/templates/template_test.html");  

$this->jasperreport($template);

// Function which will interact with jasper helper class file and output a pdf file integrated with jasper report

function jasperreport($temp)

          {

// Loading class helper files for pdf and jasper report as used in a particular function. Otherwise, you can load it in a constructor

                   $this->load->helper("jasper");

                   $this->load->library("tcpdf");

                   $xml =  simplexml_load_file("public/report/test.jrxml");

                   $jasperXmlObj = new Jasper();

                   $ jasperXmlObj ->debugsql=false;

                   // $data = array("parameter1"=>$temp);

                   // print_r($data);

                   $ jasperXmlObj ->arrayParameter=array("parameter1"=>$temp); // passing custom parameter for e.g, template

                   $ jasperXmlObj ->xml_dismantle($xml);

                   $ jasperXmlObj ->transferDBtoArray("localhost","root","root","test"); // hostname, username, password, database name

                   $ jasperXmlObj ->outpage("F");    //page output method I:standard output  D:Download file F: Save file

          }

A typical jasper report jrxml file will look like as follows:

<?xml version="1.0" encoding="UTF-8"?>

<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="testbill" language="groovy" pageWidth="612" pageHeight="792" whenNoDataType="NoDataSection" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">

          <property name="ireport.zoom" value="1.0"/>

          <property name="ireport.x" value="0"/>

          <property name="ireport.y" value="0"/>

          <parameter name="parameter1" class="java.lang.String">

                   <parameterDescription><![CDATA[]]></parameterDescription>

          </parameter>

          <queryString>

                   <![CDATA[select user_email from user_main limit 5]]>

          </queryString>

          <field name="user_email" class="java.lang.String"/>

          <title>

                   <band height="15" splitType="Stretch">

                             <staticText>

                                      <reportElement isPrintRepeatedValues="false" x="217" y="0" width="119" height="14"/>

                                      <textElement/>

                                      <text><![CDATA[Sample text for testing]]></text>

                             </staticText>

                   </band>

          </title>

          <pageHeader>

                   <band height="5" splitType="Stretch"/>

          </pageHeader>

          <columnHeader>

                   <band splitType="Stretch"/>

          </columnHeader>

          <detail>

                   <band height="18" splitType="Stretch">

                             <textField pattern="">

                                      <reportElement isPrintRepeatedValues="false" x="83" y="0" width="92" height="16">

                                                <property name="writeHTML" value="1"/>

                                      </reportElement>

                                      <textElement markup="html">

                                                <font pdfEncoding="Cp1252" isPdfEmbedded="false"/>

                                      </textElement>

                                       <textFieldExpression><![CDATA[$P{parameter1}]]></textFieldExpression>

                             </textField>

                   </band>

          </detail>

</jasperReport>

In the jrxml file above, we are accessing our custom parameter passed from php code i.e. parameter1, using standard jasper report supported format,

<![CDATA[$P{parameter1}]]>

Similarly, you can pass custom data to jrxml file from php and work with jasper report for dynamic data.

Note: To use helper/library files in core php, you can simply include those class files with include_once function.

Fill the below Form to download Library Files:

e-Zest is a leading digital innovation partner for enterprises and technology companies that utilizes emerging technologies for creating engaging customers experiences. Being a customer-focused and technology-driven company, it always helps clients in crafting holistic business value for their software development efforts. It offers software development and consulting services for cloud computing, enterprise mobility, big data and analytics, user experience and digital commerce.