2
我正在嘗試從php腳本創建pdf。我有JasperReports & PHP/JavaBridge啓動並運行,它在發送字符串&整數作爲參數時創建pdf文件。從PHP傳遞數組數據到JasperReports(使用PHP/JavaBridge)
我使用了Jasper Reports and PHP & Bullet-Proof Jasper Reports and PHP的指南來幫助設置PHP/JavaBridge和Jasper報告。
這是我的PHP腳本現在的樣子(非常相似,從前面提到的指南中的例子):
<?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://127.0.0.1:$port/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;
}
if (true == checkJavaExtension()) {
echo 'java extension is loaded!!';
}
$compileManager = new JavaClass("net.sf.jasperreports.engine.JasperCompileManager");
$report = $compileManager->compileReport(realpath("test.jrxml"));
$fillManager = new JavaClass("net.sf.jasperreports.engine.JasperFillManager");
$params = new Java("java.util.HashMap");
$params->put("text", "This is a text");
$params->put("report_id", 2);
$emptyDataSource = new Java("net.sf.jasperreports.engine.JREmptyDataSource");
$jasperPrint = $fillManager->fillReport($report, $params, $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數組作爲參數爲我的jrxml文件。什麼是最好的解決方案呢?
我想我可能必須創建一個java.util.List類,從我的PHP數組中設置數據作爲列表項,並將其作爲參數發送給我的jrxml文件,但我無法獲取這工作。
另一方面,這可能是非常錯誤的,因爲這對我來說都很新穎:\我感謝所有我能得到的幫助! :)
@Casper:這不回答如何將值數組傳遞給JasperReports。 PHP數組必須轉換爲java.util.List實例。 – 2011-04-07 21:18:02