//
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

Download
Sourceforge.net :
 http://prdownloads.sourceforge.net/appserv/appserv-win32-2.5.10.exe?download

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}

NOTE :

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.

http://www.opentaps.org/docs/index.php/Tutorial_iReports
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 http://127.0.0.1:8080/JavaBridge and http://127.0.0.1:8080/JavaBridge/test.php to see the PHP info page.
  4. the file file:webapps/JavaBridge directory,Browse to the name of your web application, for example http://127.0.0.1:8080/JavaBridge, 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>

    </web-app>

  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
    allow_url_include=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/

<?php
 set_time_limit(60);
 ob_start();
 function checkJavaExtension() {
if(!extension_loaded('java'))
 {
$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")
 {
 echo PHP_SHLIB_SUFFIX;
 if(!(PHP_SHLIB_SUFFIX=="so" && @dl('java.so'))
 &&!(PHP_SHLIB_SUFFIX=="dll"
 && @dl('php_java.dll'))
 &&!(@include_once("JavaBridge/java/Java.inc"))
 &&!(require_once("http://127.0.0.1:$port/JavaBridge/java/Java.inc")))
 {
 return "java extension not installed.";
 }
 }
 else
 {
 if(!(@include_once("JavaBridge/java/Java.inc")))
 {
 require_once("http://127.0.0.1:$port/JavaBridge/java/Java.inc");
 }
 }
 }
if(!function_exists("java_get_server_name"))
 {
 return "The loaded java extension is not the PHP/Java Bridge";
 }
 return true;
 }
 if(checkJavaExtension() ) {
try{
 java("java.lang.Class")->forName("com.mysql.jdbc.Driver");
 $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');
 //$params->put("id",'111');
 //$params->put("bulan","12");
 //$params->put("tahun",'2010');
// $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";
 java_set_file_encoding("ISO-8859-1");
 $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');
 readfile($outputPath);
 unlink($outputPath);
}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.*');
 /**
 try{
 java("java.lang.Class")->forName("com.mysql.jdbc.Driver");
 java("java.lang.Class")->forName("org.gjt.mm.mysql.Driver");
 $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");
 java("java.lang.Class")->forName("com.mysql.jdbc.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']) ) {
 $exportManager->exportReportToPdfFile($jasperPrint,$this->files['pdf']);
 }
if( strlen($this->files['html'])) {
 $exportManager->exportReportToHtmlFile($jasperPrint,$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

Code:
<servlet>
<servlet-name>default</servlet-name>
<servlet-class>
org.apache.catalina.servlets.DefaultServlet
</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>0</param-value>
</init-param>
<init-param>
<param-name>listings</param-name>
<param-value>false</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

to:

Code:
<servlet>
<servlet-name>default</servlet-name>
<servlet-class>
org.apache.catalina.servlets.DefaultServlet
</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>0</param-value>
</init-param>
<init-param>
<param-name>listings</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

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

<Host name="localhost" appBase="webapps"
 unpackWARs="true" autoDeploy="true">
 <Alias>127.0.0.1</Alias>
About these ads

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.

Discussion

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

  1. Can I change the Java.inc file location from
    require_once(“http://localhost:8080/JavaBridge/java/Java.inc”);
    to
    require_once(“http://localhost:8080/Java.inc”);

    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);
    Thanks

    Posted by kylesupermon | June 24, 2014, 5:14 am

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

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

Join 8 other followers

July 2012
M T W T F S S
« Jun   Aug »
 1
2345678
9101112131415
16171819202122
23242526272829
3031  

Archives

Web Statistic

Blog Stats

  • 112,441 hits
Follow

Get every new post delivered to your Inbox.

%d bloggers like this: