Passing array data from PHP to JasperReports (with PHP/JavaBridge)

admin

Administrator
Staff member
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 <a href="http://www.rjohnson.id.au/wordpress/2007/02/04/jasper-reports-and-php/" rel="nofollow">Jasper Reports and PHP</a> &amp; <a href="http://www.rjohnson.id.au/wordpress/2007/10/27/bullet-proof-jasper-reports-and-php/" rel="nofollow">Bullet-Proof Jasper Reports and PHP</a> to help set up PHP/JavaBridge and Jasper reports.

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

Code:
&lt;?php

/**
 * see if the java extension was loaded.
 */
function checkJavaExtension()
{
    if(!extension_loaded('java'))
    {
        $sapi_type = php_sapi_name();
        $port = (isset($_SERVER['SERVER_PORT']) &amp;&amp; (($_SERVER['SERVER_PORT'])&gt;1024)) ? $_SERVER['SERVER_PORT'] : '8080';
        if ($sapi_type == "cgi" || $sapi_type == "cgi-fcgi" || $sapi_type == "cli") 
        {
            if(!(PHP_SHLIB_SUFFIX=="so" &amp;&amp; @dl('java.so'))&amp;&amp;!(PHP_SHLIB_SUFFIX=="dll" &amp;&amp; @dl('php_java.dll'))&amp;&amp;!(@include_once("java/Java.inc"))&amp;&amp;!(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-&gt;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-&gt;compileReport(realpath("test.jrxml"));

$fillManager = new JavaClass("net.sf.jasperreports.engine.JasperFillManager");

$params = new Java("java.util.HashMap");
$params-&gt;put("text", "This is a text");
$params-&gt;put("report_id", 2);

$emptyDataSource = new Java("net.sf.jasperreports.engine.JREmptyDataSource");
$jasperPrint = $fillManager-&gt;fillReport($report, $params, $emptyDataSource);

$outputPath = realpath(".")."/"."output.pdf";

$exportManager = new JavaClass("net.sf.jasperreports.engine.JasperExportManager");
$exportManager-&gt;exportReportToPdfFile($jasperPrint, $outputPath);

header("Content-type: application/pdf");
readfile($outputPath);

unlink($outputPath);

?&gt;

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! :)