将数组数据从 PHP 传递到 JasperReports(使用 PHP/JavaBridge)

发布于 2024-10-29 18:25:29 字数 4107 浏览 1 评论 0原文

我目前正在尝试从 php 脚本创建 pdf。我有 JasperReports 和PHP/JavaBridge 已启动并运行,并且可以在发送字符串和文件时创建 pdf 文件。整数作为参数。

我使用了 Jasper Reports 和PHP & 防弹 Jasper 报告和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 文件,但我无法让它工作。

另一方面,这可能是非常错误的,因为这对我来说都是全新的:\ 我感谢我能得到的所有帮助! :)

I'm currently trying to create a pdf from a php script. I have JasperReports & PHP/JavaBridge up and running, and it works creating pdf files when sending strings & integers as parameters.

I've used the guides from Jasper Reports and PHP & Bullet-Proof Jasper Reports and PHP to help set up PHP/JavaBridge and Jasper reports.

This is how my php script currently looks like (very similar to the example from the previously mentioned guide):

<?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);

?>

The code above works, but I want to use a PHP array as parameter for my jrxml file. What would be the best solution for this?

I'm thinking i probably have to create a java.util.List class, set the data from my php array as the list items, and send this as a paramater to my jrxml file, but i can't get this to work.

On the other hand, this might be very wrong since this is all quite new for me :\ I appreciate all the help i can get! :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

叫嚣ゝ 2024-11-05 18:25:33

我不确定我是否理解,但如果您有诸如 {id =>; 之类的值数组1、姓名=> 'fubar',某事=> ‘布拉拉’。试试这个:

$map =  new Java("java.util.HashMap");
foreach ($array as $key=>$value){
    $map->put($key,$value);
}
$Jfm = new Java("net.sf.jasperreports.engine.JasperFillManager");
$this->FillReport = $Jfm->fillReport($this->CompileReport, $map, $this->DbConnect);

Im not sure i understand but if you have array of values such as {id => 1 , name => 'fubar', something => 'blala'. Try this:

$map =  new Java("java.util.HashMap");
foreach ($array as $key=>$value){
    $map->put($key,$value);
}
$Jfm = new Java("net.sf.jasperreports.engine.JasperFillManager");
$this->FillReport = $Jfm->fillReport($this->CompileReport, $map, $this->DbConnect);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文