如何将参数从PHP传递到JasperReports?

时间:2011-03-14 21:52:11

标签: php parameters jasper-reports

背景

使用Centos 5.5 final,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);

?>

1 个答案:

答案 0 :(得分:2)

来源

以下是根据用户输入字段的命名约定,自动将参数从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()的代码。