PHP calls Jasperreport report via Php/java Bridge

Source: Internet
Author: User
Tags sapi
This article is about using Php-java-bridge technology to implement the output of the Jasperreport Web report.

Jasperreport (http://jasperforge.org/) is a powerful and flexible report generation tool that can display rich page content and convert it to pdf,html, or XML format. The library is written entirely in Java and can be used to generate dynamic content in a variety of Java applications, including J2ee,web applications.

The accompanying report design tool is ireport (free), the tool can realize the visual report design, can output Pdf,html,word Common format report, save the file as. jrxml suffix, need the Java environment to be able to run normally, PHP cannot call directly.

Since PHP cannot be called directly, this has to be aided by Php-java-bridge technology. Refer to http://php-java-bridge.sourceforge.net/pjb/index.php for details

1, install Tomcat, if you choose EXE installation version, installation will automatically install the JRE environment, if it is compressed version of Tomcat, need to install another Java environment, configuration is more cumbersome, recommend the installation version of Tomcat.

The tomcat port is configured with 6000, the default 8080 port is occupied, and the site root directory is webapps under Tomcat.

2, download Php-java-bridge package, address http://php-java-bridge.sourceforge.net/pjb/ download.php, unzip after download, there is a Javabridge.war file, copy this file to Tomcat WebApps, after running http://localhost:6000/JavaBridge/, A Javabridge directory will be generated in WebApps.

3. Install ireport3.0 (with updated version) copy all contents of C:\Program files\jaspersoft\ireport-3.0.0\lib, copy to Tomcat webapps/javabridge/ web-inf/lib/, these packages need to be javabridge to find out.

4, from the generated Javabridge directory to copy the Java directory under the PHP site (or find php.ini this file, the Allow_url_include parameter is changed to ON, directly refer to Javabridge under the Java/java.inc). Download the report file Http://www.rjohnson.id.au/download/jasper/test.jrxml under the PHP site.

Then build a php file under the PHP site

Ireport.php (the code is related to the port, you need to change according to personal circumstances)

View Code

1
 1024))? $_server[' server_port ': ' 6000 '; //echo $port; $port = 6000; if ($sapi _type = = "CGI" | | $sapi _type = = "cgi-fcgi" | | $sapi _type = = "CLI") php_shlib_suffix== "So" && @dl (' java.so ')) &&! (php_shlib_suffix== "DLL" && @dl (' Php_java.dll ')) &&! (@include_once ("Java/java.inc")) &&!  (Require_once ("http://127.0.0.1: $port/javabridge/java/java.inc")))  {"Return" Java extension not installed.) (. @include_once ("Java/java.inc")) ("Require_once") ("http://127.0.0.1: $p Ort/javabridge/java/java.inc "); ("!function_exists (" Java_get_server_name ")) + (" + ") E loaded Java extension is not the Php/java Bridge "; The return is true; Notoginseng/** * Convert A PHP value to a Java one ... * @param string $value * @param string $className * @returns Boolean success * * Convertva  Lue ($value, $className) $ {//If we are a string, just with the normal conversion//methods from the   Java extension ... ($className = = ' java.lang.String '). {$temp = new Java   (' java.lang.String ', $value);   The return $temp;   ($className = = ' Java.lang.Boolean ' | |   $className = = ' Java.lang.Integer ' | |   $className = = ' Java.lang.Long ' | |   $className = = ' Java.lang.Short ' | |   $className = = ' Java.lang.Double ' | |    $className = = ' Java.math.BigDecimal ') $ $temp = new Java ($className, $value);   return $temp;   ($className = = ' Java.sql.Timestamp ' | |  67           $className = = ' Java.sql.Time '), {$temp = new Java ($className);   $javaObject = $temp->valueof ($value);   return $javaObject; (Exception $err) (' Unable to convert value, '. $val   Ue. The could not being converted to '.   $className);   return false;   (' Unable to convert value, class name '. $className.   "Not recognised");   return false; Checkjavaextension (); $compileManager = new Javaclass ("Net.sf.jasperreports.engine.JasperCompileManager"); $report = $compileManager->compilereport (Realpath ("Test.jrxml")); $fillManager = new Javaclass ("Net.sf.jasperreports.engine.JasperFillManager"); 94 $params = new Java ("Java.util.HashMap"); $params->put ("text", "This is a test string"); $params->put ("number", 3.00); $paramS->put ("Date", Convertvalue ("2007-12-31 0:0:0", "Java.sql.Timestamp")); 98 $emptyDataSource = new Java ("Net.sf.jasperreports.engine.JREmptyDataSource"), $jasperPrint = $fillManager- >fillreport ($report, $params, $emptyDataSource); 101 102 $outputPath = Realpath ("."). " /"." Output.pdf "; 103 104 $exportManager = new Javaclass (" Net.sf.jasperreports.engine.JasperExportManager "); 105 $ Exportmanager->exportreporttopdffile ($jasperPrint, $outputPath); 106 107 Header ("Content-type:application/pdf") ; 108 ReadFile ($outputPath); 109 unlink ($outputPath); 111-?>

5, visit PHP site, http://localhost:8080/ireport.php, you can export PDF documents.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.