PHP calls JasperReport report through PHP/JAVABridge

Source: Internet
Author: User
PHP uses PHPJAVABridge to call the JasperReport report. This article describes how to use the PHP-Java-Bridge technology to output the JasperReport web report.

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)

View Code

  1 
 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. visit the PHP Site, http: // localhost: 8080/ireport. php to output the PDF file.

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.