Использование финала Centos 5.5, Apache Tomcat 6, Java 1.6.0_24, PHP / Java Bridge 6.2.1 и JasperReports 4.0.1.
PHP не может добавить текстовое значение в java.util.HashMap
.
Есть ли у кого-нибудь какие-либо предложения о том, как выполнить эту задачу?
Если я прокомментирую строку $map->put("text", "This is a test string");
, отчет компилируется и запускается.
<?php /** * see if the java extension was loaded. */ function checkJavaExtension() { if(!extension_loaded('java')) { $sapi_type = php_sapi_name(); $port = (isset($_SERVER['SERVER_PORT']) && (($_SERVER['SERVER_PORT'])>1024)) ? $_SERVER['SERVER_PORT'] : '8080'; if ($sapi_type == "cgi" || $sapi_type == "cgi-fcgi" || $sapi_type == "cli") { 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/java/Java.inc"))) { return "java extension not installed."; } } else { if(!(@include_once("java/Java.inc"))) { require_once("http://www.webserver.net:$port/JavaBridge/java/Java.inc"); } } } if(!function_exists("java_get_server_name")) { return "The loaded java extension is not the PHP/Java Bridge"; } return true; } /** * convert a php value to a java one... * @param string $value * @param string $className * @returns boolean success */ function convertValue($value, $className) { // if we are a string, just use the normal conversion // methods from the java extension... try { if ($className == 'java.lang.String') { $temp = new Java('java.lang.String', $value); return $temp; } else if ($className == 'java.lang.Boolean' || $className == 'java.lang.Integer' || $className == 'java.lang.Long' || $className == 'java.lang.Short' || $className == 'java.lang.Double' || $className == 'java.math.BigDecimal') { $temp = new Java($className, $value); return $temp; } else if ($className == 'java.sql.Timestamp' || $className == 'java.sql.Time') { $temp = new Java($className); $javaObject = $temp->valueOf($value); return $javaObject; } } catch (Exception $err) { echo ( 'unable to convert value, ' . $value . ' could not be converted to ' . $className); return false; } echo ( 'unable to convert value, class name '.$className. ' not recognised'); return false; } checkJavaExtension(); $map = new Java("java.util.HashMap"); $map->put("text", "This is a test string"); $compileManager = new JavaClass("net.sf.jasperreports.engine.JasperCompileManager"); $report = $compileManager->compileReport(realpath("test.jrxml")); $fillManager = new JavaClass("net.sf.jasperreports.engine.JasperFillManager"); $emptyDataSource = new Java("net.sf.jasperreports.engine.JREmptyDataSource"); $jasperPrint = $fillManager->fillReport($report, $map, $emptyDataSource); $outputPath = realpath(".")."/"."output.pdf"; $exportManager = new JavaClass("net.sf.jasperreports.engine.JasperExportManager"); $exportManager->exportReportToPdfFile($jasperPrint, $outputPath); header("Content-type: application/pdf"); readfile($outputPath); unlink($outputPath); ?>
Ниже приведен код для передачи параметров с PHP на JasperReports автоматически на основе соглашения об именах для полей ввода пользователя.
<?php include_once( 'db-account.php' ); checkjavaExtension(); function report_parse_post_parameters() { # Automatically extract report parameters (data types converted in report). $params = new java('java.util.HashMap'); # Pass the remaining POST "report_TYP" variables as report parameters. foreach( $_POST as $name => $value ) { if( strpos( $name, 'report_' ) === 0 ) { $length = strlen( 'report_' ); if( strpos( $name, 'report_int_' ) === 0 ) { $value = intval( $value ); $length = strlen( 'report_int_' ); $value = convertValue( $value, 'java.lang.Integer' ); $params->put( substr( $name, $length ), $value ); } else if( strpos( $name, 'report_arr_' ) === 0 ) { $length = strlen( 'report_arr_' ); $arrays = array_filter( explode( ',', $_POST[ $name ] ) ); # Map the values of the array form parameter to a java.util.ArrayList. $arrayList = new java( 'java.util.ArrayList' ); foreach( $arrays as $value ) { $arrayList->add( $value ); } # Pass values into the report (without the "report_arr_" prefix). $params->put( substr( $name, $length ), $arrayList ); } else { $params->put( substr( $name, $length ), $value ); } } } return $params; } function report_execute( $filename = 'FILENAME' ) { global $dbhost; global $dbname; global $dbuser; global $dbpass; $conn = null; $report = realpath( '/PATH/TO/REPORT.jasper' ); try { $params = report_parse_post_parameters(); # Load the PostgreSQL database driver. java( 'java.lang.Class' )->forName( 'org.postgresql.Driver' ); # Attempt a database connection. $conn = java( 'java.sql.DriverManager' )->getConnection( "jdbc:postgresql://$dbhost/$dbname?user=$dbuser&password=$dbpass" ); # Use the fill manager to produce the report. $fm = java('net.sf.jasperreports.engine.JasperFillManager'); $pm = $fm->fillReport($report, $params, $conn); header('Cache-Control: private'); header('Content-Description: File Transfer'); header("Content-Disposition: attachment, filename=$filename.pdf"); header('Content-Type: application/pdf'); header('Content-Transfer-Encoding: binary'); java_set_file_encoding('ISO-8859-1'); $em = java('net.sf.jasperreports.engine.JasperExportManager'); $result = $em->exportReportToPdf($pm); $conn->close(); header('Content-Length: ' . strlen( $result ) ); echo $result; } catch( Exception $ex ) { if( $conn != null ) { $conn->close(); } throw $ex; } } ?>
Измените эту строку:
function report_execute( $filename = 'FILENAME' ) {
Измените эту строку:
$report = realpath( '/PATH/TO/REPORT.jasper' );
Измените эти строки в соответствии с вашим программным обеспечением базы данных:
java( 'java.lang.Class' )->forName( 'org.postgresql.Driver' ); $conn = java( 'java.sql.DriverManager' )->getConnection( "jdbc:postgresql://$dbhost/$dbname?user=$dbuser&password=$dbpass" );
Изменить db-account.php
:
<?php $dbhost = 'HOSTNAME'; $dbname = 'DATABASE'; $dbuser = 'USERNAME'; $dbpass = 'PASSWORD'; ?>
Используйте префикс report_
для пользовательских входов, например:
<form method="post" action='report.dhtml' class="climate" id="report-form"> <input type="hidden" name="report_int_ReportId" value="1" /> <input type="hidden" name="report_int_Radius" value="35" /> <input type="hidden" name="report_int_Trend" value="0" /> <input type="hidden" name="report_int_Relevance" value="1" /> <select id="category" name="report_int_CategoryId" value="2"></select> <input type="text" id="city" name="report_int_CityId" /> <input class="submit-button" id="submit" type="submit" name="submit" value="Report" /> </form>
Эти значения затем автоматически передаются в отчет.
Вам не нужно менять код для checkjavaExtension()
.