Background
Using Centos 5.5 final, Apache Tomcat 6, Java 1.6.0_24, PHP/Java Bridge 6.2.1, and JasperReports 4.0.1.
Problem
PHP fails to add a text value to a java.util.HashMap
.
Question
Does any one have any suggestions on how to accomplish this task?
Source
If I comment out the line $map->put("text", "This is a test string");
, the report compiles and runs.
<?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);
?>
Source
Here is some code to pass parameters from PHP to JasperReports automatically, based on a naming convention for user input fields.
Usage
Change this line:
Change this line:
Change these lines to match your database software:
Edit
db-account.php
:Example Form Input
Use a
report_
prefix for user inputs, such as:These values are then passed automatically into the report.
Note
You should not have to change the code for
checkjavaExtension()
.