PHP uses PHP/JAVABridge to call JasperReport report _ PHP Tutorial

Source: Internet
Author: User
PHP calls the JasperReport report through PHPJAVABridge. This article describes how to use the PHP-Java-Bridge technology to output JasperReportweb reports. JasperReport (jasperge.org) is a powerful and flexible report generation tool that demonstrates how to use PHP-Java-Bridge technology to output JasperReport web reports.

JasperReport (http://jasperforge.org/) is a powerful and flexible report generation tool that displays rich page content and converts it to PDF, HTML, or XML format. This library is fully written in Java and can be used to generate dynamic content in various Java applications, including J2EE and Web applications.

The attached report design tool is iReport (free of charge). This tool can implement visual report design and output common reports in PDF, HTML, and WORD formats. the saved files are. jrxml suffix, which can run normally only in the Java environment. PHP cannot be called directly.

Since PHP cannot be called directly, it has to rely on PHP-Java-Bridge technology. See http://php-java-bridge.sourceforge.net/pjb/index.php for details

1. install tomcat. if you select the exe installation version, the jre environment will be automatically installed during installation. if it is a compressed version of tomcat, you need to install the java environment, which is more complicated to configure, install tomcat is recommended.

Configure tomcat Port 6000. the default port 8080 is occupied. The site root directory is webapps under tomcat.

2. download the php-java-bridge package.

3. install ireport3.0 (with updated version) copy all content in C: \ Program Files \ JasperSoft \ iReport-3.0.0 \ lib, copy to tomcat webapps/JavaBridge/WEB-INF/lib, these packages must be found by JavaBridge.

4. copy the java directory from the generated JavaBridge directory to the PHP site (or find php. in the ini file, change the allow_url_include parameter to on and directly reference java/java in JavaBridge. inc ). Download the report file http://www.rjohnson.id.au/download/jasper/test.jrxmland put it under the PHP site.

Create a PHP file under the PHP Site

Ireport. php (the port involved in the code should be changed according to your personal situation)

1

2

3 /**

4 * see if the java extension was loaded.

5 */

6 function checkJavaExtension ()

7 {

8 if (! Extension_loaded ('Java '))

9 {

10 $ sapi_type = php_sapi_name ();

11

12 // $ port = (isset ($ _ SERVER ['server _ port']) & ($ _ SERVER ['server _ port'])> 1024 ))? $ _ SERVER ['server _ port']: '123 ';

13 // echo $ port;

14 $ port = 6000;

15 if ($ sapi_type = "cgi" | $ sapi_type = "cgi-fcgi" | $ sapi_type = "cli ")

16 {

17 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 {

19 return "java extension not installed .";

20}

21}

22 else

23 {

24 if (! (@ Include_once ("java/Java. inc ")))

25 {

26

27 require_once ("http: // 127.0.0.1: $ port/JavaBridge/java/Java. inc ");

28}

29}

30}

31 if (! Function_exists ("java_get_server_name "))

32 {

33 return "The loaded java extension is not the PHP/Java Bridge ";

34}

35

36 return true;

37}

38

39 /**

40 * convert a php value to a java one...

41 * @ param string $ value

42 * @ param string $ className

43 * @ returns boolean success

44 */

45 function convertValue ($ value, $ className)

46 {

47 // if we are a string, just use the normal conversion

48 // methods from the java extension...

49 try

50 {

51 if ($ className = 'Java. lang. string ')

52 {

53 $ temp = new Java ('Java. lang. string', $ value );

54 return $ temp;

55}

56 else if ($ className = 'Java. lang. boolean' |

57 $ className = 'Java. lang. integer' |

58 $ className = 'Java. lang. long' |

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

60 $ className = 'Java. lang. double' |

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

62 {

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

64 return $ temp;

65}

66 else if ($ className = 'Java. SQL. timestamp' |

67 $ className = 'Java. SQL. Time ')

68 {

69 $ temp = new Java ($ className );

70 $ javaObject = $ temp-> valueOf ($ value );

71 return $ javaObject;

72}

73}

74 catch (Exception $ err)

75 {

76 echo ('unable to convert value, '. $ value.

77 'could not be converted to '. $ className );

78 return false;

79}

80

81 echo ('unable to convert value, class name'. $ className.

82 'not recognized ');

83 return false;

84}

85

86

87 checkJavaExtension ();

88

89 $ compileManager = new JavaClass ("net. sf. jasperreports. engine. JasperCompileManager ");

90 $ report = $ compileManager-> compileReport (realpath ("test. jrxml "));

91

92 $ fillManager = new JavaClass ("net. sf. jasperreports. engine. JasperFillManager ");

93

94 $ params = new Java ("java. util. HashMap ");

95 $ params-> put ("text", "This is a test string ");

96 $ params-> put ("number", 3.00 );

97 $ params-> put ("date", convertValue ("", "java. SQL. Timestamp "));

98

99 $ emptyDataSource = new Java ("net. sf. jasperreports. engine. JREmptyDataSource ");

100 $ jasperPrint = $ fillManager-> fillReport ($ report, $ params, $ emptyDataSource );

101

102 $ outputPath = realpath ("."). "/". "outputpipeline ";

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

110 unlink ($ outputPath );

111

112?>

5, visit PHP Site, http://www.BkJia.com: 8080/ireport. php, you can output PDF document.

Taken from what is done.

Outputs web reports. JasperReport (http://jasperforge.org/) is a powerful and flexible report generation tool that can demonstrate abundant...

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.