Heim  >  Artikel  >  Backend-Entwicklung  >  PHP通过PHP/JAVA Bridge调用JasperReport报表_PHP教程

PHP通过PHP/JAVA Bridge调用JasperReport报表_PHP教程

WBOY
WBOYOriginal
2016-07-13 17:46:46925Durchsuche

 

本文说到的是用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文档。

 

 

摘自 有所为,有所不为

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/478560.htmlTechArticle本文说到的是用PHP-Java-Bridge技术实现JasperReport web报表的输出。 JasperReport(http://jasperforge.org/),是一个强大、灵活的报表生成工具,能够展示丰...
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn