PHP通過PHP/JAVA Bridge調用JasperReport報表_PHP教程

來源:互聯網
上載者:User
本文說到的是用PHP-Java-Bridge技術實現JasperReport web報表的輸出。

JasperReport(http://jasperforge.org/),是一個強大、靈活的報表產生工具,能夠展示豐富的頁面內容,並將之轉換成PDF,HTML,或者XML格式。該庫完全由Java寫成,可以用於在各種Java應用程式,包括J2EE,Web應用程式中產生動態內容。

附帶報表設計工具是iReport(免費的),該工具可以實現可視化報表設計,可以輸出PDF,HTML,WORD的常用格式報表,儲存後的檔案為.jrxml尾碼,需要Java環境才可以正常運行,PHP不能直接調用。

既然PHP不能直接調用,這就不得不藉助於PHP-Java-Bridge技術。具體可以參考http://php-java-bridge.sourceforge.net/pjb/index.php

1、安裝tomcat,如果是選擇exe安裝版,安裝的時候會自動安裝jre環境,如果是壓縮版tomcat,需要另外安裝java環境,配置也更繁瑣,推薦用安裝版的tomcat。

把tomcat的連接埠配置6000,預設的8080連接埠被佔用,網站根目錄為tomcat下面的webapps

2、下載php-java-bridge包,地址http://php-java-bridge.sourceforge.net/pjb/download.php,下載後解壓,裡面有一個JavaBridge.war的檔案,將這個檔案拷貝到tomcat的webapps,運行http://localhost:6000/JavaBridge/之後,會在webapps產生一個JavaBridge的目錄。

3、安裝ireport3.0(有更新的版本) 拷貝C:\Program Files\JasperSoft\iReport-3.0.0\lib 中的所有內容,拷貝到tomcat的webapps/JavaBridge/WEB-INF/lib/下,這些包需要能被JavaBridge找得到才行。

4、從產生的JavaBridge目錄下拷貝java目錄到PHP網站下(或者找到php.ini這個檔案,將裡面的allow_url_include參數改為on,直接引用JavaBridge下的java/java.inc)。下載報表檔案http://www.rjohnson.id.au/download/jasper/test.jrxml放在PHP網站下。

然後在PHP網站下建立一個PHP檔案

ireport.php(代碼中涉及到連接埠的,需要根據個人情況更改)

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'] : '6000';

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 recognised');

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("2007-12-31 0:0:0", "java.sql.Timestamp"));

98

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

100 $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

110 unlink($outputPath);

111

112 ?>

5、訪問PHP網站,http://www.BkJia.com :8080/ireport.php,就可以輸出PDF文檔。

摘自 有所為,有所不為

http://www.bkjia.com/PHPjc/478560.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/478560.htmlTechArticle本文說到的是用PHP-Java-Bridge技術實現JasperReport web報表的輸出。 JasperReport(http://jasperforge.org/),是一個強大、靈活的報表產生工具,能夠展示豐...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.