you're reading...
Database, IT-Center, MySQL, PHP, Web Based

PHP Report : iReport (JasperReport) with PHP JavaBridge

NOW! Professional Reporting can embeding in PHP source, that report will be helping Reporting data from Mysql and handle paper size dan page number. iReport will helping design and setting print data.

A. AppServer

AppServ 2.5.10

  • Apache 2.2.8
  • PHP 5.2.6
  • MySQL 5.0.51b
  • phpMyAdmin-2.10.3

Sourceforge.net :

MD5SUM : 279c0c39866fbecb8a3904969fd5d0f4

B. iReport
Download from http://sourceforge.net/projects/ireport/files/iReport%20(classic)/ (test using iReport 0.5.0 version)
Dowload zip version and run using iReport.bat and wait until see Designer report page.
Tutorial see http://jasperforge.org/uploads/publish/ireportwebsite/IR%20Website/ir_documentation.html

Simple Tutorial :
a. Extract iReport 0.5.0 and run iReport.bat, wait application Report Designer Open.
b. Setting folder path compiler, Tools –> Option –> Browse folder

c. Create datasource, to handle database connection. Datasource Menu –> Connection/Datasource –> New

see JDBC Driver, URL and server address (port)
d. Create field binding using Datasource Menu –> Report Query –> Report SQL Query tab –> checked (auto retrive field)

e. Create Report page, using File Menu –> New Document.
Getting element report GUI in Edit Menu –> insert Element. Create header using staticText Element, and use textField to getting value field $F{field_name}


Don’t know what version you are using, but I was able to do it in iReport 3.7.6.

First I add the parameters to the dataset.

Then, to pass the values to the parameters, right-click on the Table in Report Inspector,
choose “Edit table datasource”. Go to “Parameters” tab.
Each dataset parameter can take its value from parameter, field, or variable from the main report.

C. Apache Tomcat & Java Bridge (see : http://php-java-bridge.sourceforge.net/doc/tomcat6.php)

Download and install Java, Tomcat and PHP

Install a PHP web application into Tomcat

  1. Copy the PHP web application JavaBridgeTemplate.war or the demo JavaBridge.war to the Tomcat webapps directory.
  2. Wait two seconds until Tomcat has loaded the web application.
  3. Browse to and to see the PHP info page.
  4. the file file:webapps/JavaBridge directory,Browse to the name of your web application, for example, to run it from your internet browser.

PHP support for all existing Tomcat web applications

If you want to enable PHP for all of your web applications, move the Java libraries from the local web application folder to the Tomcat library folder and edit the Tomcat web configuration as follows:

  1. Stop Tomcat.
  2. Move the libraries JavaBridge.jarphp-servlet.jar and php-script.jar from the webapps/JavaBridge/WEB-INF/lib directory over to the tomcat lib directory.  (Example : G:\Program Files\Apache Software Foundation\Tomcat 7.0\lib)
  3. Edit the Tomcat conf/web.xml. Add the following 9 lines marked with a +:

    <web-app xmlns=... >

    + <listener><listener-class>php.java.servlet.ContextLoaderListener</listener-class></listener>
    + <servlet><servlet-name>PhpJavaServlet</servlet-name><servlet-class>php.java.servlet.PhpJavaServlet</servlet-class>
    + </servlet>
    + <servlet><servlet-name>PhpCGIServlet</servlet-name><servlet-class>php.java.servlet.fastcgi.FastCGIServlet</servlet-class>
    + <init-param><param-name>prefer_system_php_exec</param-name><param-value>On</param-value></init-param>
    + <init-param><param-name>php_include_java</param-name><param-value>Off</param-value></init-param>
    + </servlet>
    + <servlet-mapping><servlet-name>PhpJavaServlet</servlet-name><url-pattern>*.phpjavabridge</url-pattern> </servlet-mapping>
    + <servlet-mapping><servlet-name>PhpCGIServlet</servlet-name><url-pattern>*.php</url-pattern></servlet-mapping>


  4. Start Tomcat again. Now you can add PHP scripts to tomcat.
  5. Add a PHP test file

    <?php require_once("java/Java.inc");
    echo java("java.lang.System")->getProperties(); ?>

    to some web context, for example “examples”, and browse to http://yourHost.com:8080/examples/test.php.
    **URL file-access is disabled in the server configuration error
    set php.ini 
    allow_url_fopen = ON

  6. Check the process list using the Unix/Linux or Windows task manager. There should be 5 php-cgi FastCGI executables waiting for requests from Java.
  7. Please see our FAQ for more information how to set up a load balancer or how to create a distributable PHP/Java web application.

D. Final Test (Linked Jasper Report from PHP file)
a. Create index.php in appServer/www/iReportTest/

 function checkJavaExtension() {
$sapi_type = php_sapi_name();
 $port = (isset($_SERVER['SERVER_PORT'])
 && (($_SERVER['SERVER_PORT'])>1024))
 ? $_SERVER['SERVER_PORT'] : '8080';
 if ($sapi_type == "cgi" || $sapi_type == "cgi-fcgi"
 || $sapi_type == "cli")
 if(!(PHP_SHLIB_SUFFIX=="so" && @dl('java.so'))
 && @dl('php_java.dll'))
 return "java extension not installed.";
 return "The loaded java extension is not the PHP/Java Bridge";
 return true;
 if(checkJavaExtension() ) {
 $driverManager = new JavaClass("java.sql.DriverManager");
 $dbConnection = $driverManager->getConnection("jdbc:mysql://localhost/dbname", "user", "password");
try {
 // Open jasper report
 $compileManager = new JavaClass("net.sf.jasperreports.engine.JasperCompileManager");
 $viewer = new JavaClass("net.sf.jasperreports.view.JasperViewer");
 $report = $compileManager->compileReport(realpath("Report/ReportUser.jrxml"));
 // $report = $compileManager->compileReport(realpath("report/report1.jasper"));
 $fillManager = new JavaClass("net.sf.jasperreports.engine.JasperFillManager");
 $params = new java('java.util.HashMap');
// $emptyDataSource = new Java("net.sf.jasperreports.engine.JREmptyDataSource");
 $jasperPrint = $fillManager->fillReport($report, $params, $dbConnection);
 // $jasperPrint = $fillManager->fillReport("report/report1.jasper", $params, $dbConnection);
 // $pm = $fm->fillReport($report, $params, $conn);
 // $viewer->viewReport($jasperPrint,false);
 header('Cache-Control: no-cache private');
 header('Content-Description: File Transfer');
 header('Content-Disposition: attachment, filename=report/report.pdf');
 header('Content-Type: application/pdf');
 header('Content-Transfer-Encoding: binary');
 // We'll be outputting a PDF
 // header('Content-type: application/pdf');
 // It will be called downloaded.pdf
 // header('Content-Disposition: attachment; filename="report/report1.pdf"');
 // The PDF source is in original.pdf
 // readfile('original.pdf');
$outputPath = realpath(".")."/"."output.pdf";
 $em = java('net.sf.jasperreports.engine.JasperExportManager');
 // $result = $em->exportReportToPdf($jasperPrint);
 // header('Content-Length: ' . strlen( $result ) );
 // echo $result;
 $em-> exportReportToPdfFile($jasperPrint, $outputPath);
 header("Content-type: application/pdf");
 // header('Content-type: application/ms-excel');
}catch(JavaException $ex) {
 // ERROR generating Report
 echo "<b>ERROR generating Report:</b><br/> ".$ex->getCause();
 // exit 1;
 }catch(JavaException $ex) {
 echo "<b>ERROR generating Report:</b><br/> ".$ex->getCause();
 // $system = new Java('java.lang.System');
 // new java('java.sql.*');
 // new java('java.lang.*');
 $driverManager = new JavaClass("java.sql.DriverManager");
 $dbConnection = $driverManager->getConnection("jdbc:mysql://localhost/smep-server", "root", "");
 }catch(Exception e){
 String driver = "com.mysql.jdbc.Driver";
 String connection = "jdbc:mysql://localhost:3306/sme-server";
 String user = "root";
 String password = "root";
 try {
 // java("java.lang.Class")->forName($this->driver); // asli
 // java("java.lang.Class")->forName("org.gjt.mm.mysql.Driver");
 $driverManager = new JavaClass("java.sql.DriverManager");
 $dbConnection = $driverManager->getConnection("jdbc:mysql://localhost/sme-server", "root", "");
 try {
 // Open jasper report
 $compileManager = new JavaClass("net.sf.jasperreports.engine.JasperCompileManager");
 $report = $compileManager->compileReport($this->files['jrxml']);
// Fill report
 $fillManager = new JavaClass("net.sf.jasperreports.engine.JasperFillManager");
 $jasperPrint = $fillManager->fillReport($report, $this->params, $dbConnection);
// Export report to PDF
 $exportManager = new JavaClass("net.sf.jasperreports.engine.JasperExportManager");
if( strlen($this->files['pdf']) ) {
if( strlen($this->files['html'])) {
// return true;
 }catch(JavaException $ex) {
 // ERROR generating Report
 echo "<b>ERROR generating Report:</b><br/> ".$ex->getCause();
 // exit 1;
}catch(JavaException $ex) {
 // ERROR connecting to Database
 echo "<b>ERROR connecting to Database:</b><br/> ".$ex->getCause();
 // exit 1;
// http://p2p.wrox.com/j2ee/38785-exporting-data-into-excel-using-jasper-reports.html

 b. Create sample report (download here)
c. run with browser http://localhost/iReportTest/index.php

Problem :
How to Dierectory Listing in Tomcat




Make Alias Host in Tomcat :
Open Server.xml and edit like this :

<Host name="localhost" appBase="webapps"
 unpackWARs="true" autoDeploy="true">

About berbagisolusi

Berbagi merupakan sebuah bentuk simbol keikhlasan untuk membantu dan menolong, sedangkan solusi adalah cara menyelesaikan masalah. Setiap manusia pasti mengalami masalah, tetapi kita tidak perlu mengalami masalah yang sama jika orang lain pernah mengalami dan kita tahu hal tersebut.


8 thoughts on “PHP Report : iReport (JasperReport) with PHP JavaBridge

  1. Can I change the Java.inc file location from

    Because all my previous code are based on this.

    Thank you,

    Posted by Thanika | January 16, 2014, 2:06 pm
  2. how can i use this in linux?

    Posted by tanzeem1981 | May 7, 2014, 5:03 pm
  3. Plz how can I implement jasperviewer with Javabridge ?
    It is possible to use: $viewer->viewReport($jasperPrint,false);

    Posted by kylesupermon | June 24, 2014, 5:14 am
  4. Hi there colleagues, good piece of writing and pleasant urging commented
    here, I am really enjoying by these.

    Posted by http://blog.ericboukaram.com/ | September 12, 2014, 8:41 am
  5. whoah this weblog is magnificent i like reading your articles.
    Stay up the great work! You understand, a lot of persons are looking around for
    this information, you could aid them greatly.

    Posted by Wie Wachstumshormon erhöhen | September 28, 2014, 8:16 pm


  1. Pingback: Fix Ireport Error Loading The Report Template Windows XP, Vista, 7, 8 [Solved] - November 18, 2014

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

Enter your email address to follow this blog and receive notifications of new posts by email.

Join 10 other followers

July 2012
« Jun   Aug »


Web Statistic

Blog Stats

  • 160,286 hits
%d bloggers like this: