I have just successfully integrated Jasper Report, PHP, PostgreSQL and Zend Framework. I tried to create a class based on the results that I have gathered upon googling Jasper Report and PHP.

To save all the pains here’s my work.

Make sure that you have successfully ran and tested JavaBridge.

I renamed “Java.inc” to Java.php and placed it under my zend library folder. Or you can place it under php include directory. Java.inc is included in the JavaBridge project.

Jasper report can now be easily exported directly to your Printer, to a PDF file or to an Html format.

Here’s my MyJasper Class


class MyJasper{
private $_objDbConnect;
private $_objJep;
private $_objPrint;
private $_objReport;
private $_objStream;

function __construct($username,$password,$dataSourceServerUrl) {
$this->initReport($username,$password,$dataSourceServerUrl);
}
function initReport($username, $password,$dataSourceServerUrl){
$this->_objStream = new Java(“java.io.ByteArrayOutputStream”);
$this->_objJep = new Java(“net.sf.jasperreports.engine.JRExporterParameter”);
$objClass = new Java(“java.lang.Class”);
$objClass->forName(“org.postgresql.Driver”);
$objDbm = new Java(“java.sql.DriverManager”);
$this->_objDbConnect = $objDbm->getConnection($dataSourceServerUrl,$username, $password);
return;
}
public function compileReport($jrxmlReport){
$objJcm = new Java(“net.sf.jasperreports.engine.JasperCompileManager”);
$this->_objReport = $objJcm->compileReport($jrxmlReport);
return;
}
public function setParams($mapArray){
$map =  new Java(“java.util.HashMap”);
foreach ($mapArray as $key=>$value){
$map->put($key,$value);
}
$objJfm = new Java(“net.sf.jasperreports.engine.JasperFillManager”);
$this->_objPrint = $objJfm->fillReport($this->_objReport, $map, $this->_objDbConnect);
return;
}
public function toPrinter(){
$objJhe = new Java(“net.sf.jasperreports.engine.export.JRPrintServiceExporter”);
$prn = $objJhe->checkAvailablePrinters();
if ($prn){
$objJhe->setParameter($this->_objJep->JASPER_PRINT, $this->_objPrint );
$objJhe->exportReport();
}
return;
}
public function toPDF($fileName=””){
$objJhe = new Java(“net.sf.jasperreports.engine.export.JRPdfExporter”);
$objJhe->setParameter($this->_objJep->JASPER_PRINT, $this->_objPrint );
if ($fileName == “”){
$objJhe->setParameter($this->_objJep->OUTPUT_STREAM,$this->_objStream);
$objJhe->exportReport();
header(“Content-type: application/pdf”);
echo java_cast($this->_objStream->toByteArray(),”S”);
}else{
$objJhe->setParameter($this->_objJep->OUTPUT_FILE_NAME,$fileName);
$objJhe->exportReport();
}

return;
}
public function toHtml(){
$objJhe = new Java(“net.sf.jasperreports.engine.export.JRHtmlExporter”);
$objJhe->setParameter($this->_objJep->JASPER_PRINT, $this->_objPrint );
$objJhe->setParameter($this->_objJep->OUTPUT_STREAM,$this->_objStream);
$objJhe->exportReport();
echo $this->_objStream->toString();
return;
}
public function hasPrinter(){
$objJhe = new Java(“net.sf.jasperreports.engine.export.JRPrintServiceExporter”);
$prn = $objJhe->checkAvailablePrinters();
return $prn;
}
}

This is my reportcontroller class

require_once(“Java.php”);

class ReportController extends Zend_Controller_Action{
private $_param;
function init(){
$this->_param = $this->_getAllParams();
}
function indexAction(){
$jreport = new MyJasper(“username”, “password”,”jdbc:postgresql://localhost/mydb”);
$jreport->compileReport(“d:/wamp/www/newent2/jasper/report1.jrxml”);
$map = array(“port_id”=>(int) $this->_param[‘portid’]);
$jreport->setParams($map);

$jreport->toPDF(); //- export to pdf and view at the browser

$jreport->toPDF(“d:/report.pdf”) //- export to pdf file

$jreport->toPrinter(); //-export directly to default printer

$jreport->toHtml(); //-export to html

exit;
}

}

My report1.jrxml report has a parameter “port_id” in which Im going to pass to filter my report.

my report’s address in zend framework “http://localhost/myreport/index/portid/7”

Happy coding everyone…:)