Home Backend Development PHP Tutorial PHP calls JasperReport report through PHP/JAVA Bridge_PHP tutorial

PHP calls JasperReport report through PHP/JAVA Bridge_PHP tutorial

Jul 13, 2016 pm 05:46 PM
java php Report yes use of transfer pass

This article talks about using PHP-Java-Bridge technology to realize the output of JasperReport web reports.

JasperReport (http://jasperforge.org/) is a powerful and flexible report generation tool that can display rich page content and convert it into PDF, HTML, or XML formats. This 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 attached report design tool is iReport (free). This tool can realize visual report design and can output reports in common formats of PDF, HTML, and WORD. The saved files have a .jrxml suffix and require a Java environment to run normally. PHP cannot Call directly.

Since PHP cannot be called directly, it has to resort to PHP-Java-Bridge technology. For details, please refer to http://php-java-bridge.sourceforge.net/pjb/index.php

1. Install tomcat. If you choose the exe installation version, the jre environment will be automatically installed during installation. If it is the compressed version of tomcat, you need to install the java environment separately, and the configuration is more complicated. It is recommended to use the installation version of tomcat.

Configure tomcat port 6000, the default port 8080 is occupied, and the site root directory is webapps under tomcat

2. Download the php-java-bridge package at the address http://php-java-bridge.sourceforge.net/pjb/download.php. After downloading, unzip it. There is a JavaBridge.war file in it. Copy this file to tomcat. For webapps, after running http://localhost:6000/JavaBridge/, a JavaBridge directory will be generated in the webapps.

3. Install ireport3.0 (there is an updated version). Copy all the contents in C:Program FilesJasperSoftiReport-3.0.0lib to tomcat's webapps/JavaBridge/WEB-INF/lib/. These packages need to be found by JavaBridge. Just fine.

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

Then create a PHP file under the PHP site

ireport.php (the code involves ports and needs to be changed according to personal circumstances)

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/),是一个强大、灵活的报表生成工具,能够展示丰...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1269
29
C# Tutorial
1249
24
PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP: Handling Databases and Server-Side Logic PHP: Handling Databases and Server-Side Logic Apr 15, 2025 am 12:15 AM

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.

See all articles