PHP calls Jasperreport report _php tutorial 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)

1

2

3/**

4 * See if the Java extension is loaded.

5 */

6 function Checkjavaextension ()

7 {

8 if (!extension_loaded (' Java '))

9 {

Ten $sapi _type = Php_sapi_name ();

11

+//$port = (isset ($_server[' Server_port ') && (($_server[' Server_port ']) >1024)? $_server[' server_port ': ' 6000 ';

//echo $port;

$port = 6000;

if ($sapi _type = = "CGI" | | $sapi _type = = "cgi-fcgi" | | $sapi _type = = "CLI")

16 {

if (! ( 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")))

18 {

Return "Java extension not installed.";

20}

21}

All else

23 {

if (!) ( @include_once ("Java/java.inc")))

25 {

26

Require_once ("http://127.0.0.1: $port/javabridge/java/java.inc");

28}

29}

30}

if (!function_exists ("Java_get_server_name"))

32 {

Return "The loaded Java extension is not the Php/java Bridge";

34}

35

return true;

37}

38

39/**

* Convert a PHP value to a Java one ...

* @param string $value

* @param string $className

* @returns Boolean success

44 */

function Convertvalue ($value, $className)

46 {

If we is a string, just use the normal conversion

//methods from the Java extension ...

Try

50 {

Wuyi if ($className = = ' java.lang.String ')

52 {

$temp = new Java (' java.lang.String ', $value);

The return $temp;

55}

($className = = ' Java.lang.Boolean ' | |

$className = = ' Java.lang.Integer ' | |

$className = = ' Java.lang.Long ' | |

$className = = ' Java.lang.Short ' | |

$className = = ' Java.lang.Double ' | |

$className = = ' Java.math.BigDecimal ')

62 {

$temp = new Java ($className, $value);

return $temp;

65}

($className = = ' Java.sql.Timestamp ' | |

$className = = ' Java.sql.Time ')

68 {

$temp = new Java ($className);

$javaObject = $temp->valueof ($value);

return $javaObject;

72}

73}

Exception catch ($err)

75 {

Echo (' Unable to convert value, '. $value.

The could not being converted to '. $className);

return false;

79}

80

Bayi Echo (' Unable to convert value, class name '. $className.

"Not recognised");

return false;

84}

85

86

Checkjavaextension ();

88

$compileManager = new Javaclass ("Net.sf.jasperreports.engine.JasperCompileManager");

$report = $compileManager->compilereport (Realpath ("Test.jrxml"));

91

$fillManager = new Javaclass ("Net.sf.jasperreports.engine.JasperFillManager");

93

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

->exportreporttopdffile $exportManager ($jasperPrint, $outputPath);

106

107 Header ("Content-type:application/pdf");

108 ReadFile ($outputPath);

109

Unlink ($outputPath);

111

?>

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

An excerpt from Something, something that is not

http://www.bkjia.com/PHPjc/478560.html www.bkjia.com true http://www.bkjia.com/PHPjc/478560.html techarticle 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, flexible report generation tool that can show abundance ...

  • Related Article

    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.