2

我有一个示例测试 PHP 页面和 Jasper 报告。我在导出到 PDF 步骤时遇到错误:

<pre>[[o:Exception]:"java.lang.Exception: Invoke failed: 
[[c:JasperFillManager]]->fillReport((o:String)[o:String], (i:Map)[o:HashMap], 
(i:Connection)[o:JDBC4Connection]). Cause: java.lang.ClassNotFoundException: 
Unresolved external reference: java.lang.NoClassDefFoundError: Could not initialize class 
net.sf.jasperreports.engine.util.JRStyledTextParser. -- Unable to call the method, see the 
README section "Java platform issues" for details and DO NOT REPORT THIS PROBLEM TO THE 
PHP/Java Bridge MAILING LIST! VM: 1.7.0_06@http://java.oracle.com/" at:

#-6 php.java.bridge.JavaBridge.getUnresolvedExternalReferenceException(JavaBridge.java:422)

#-5 php.java.bridge.JavaBridge.Invoke(JavaBridge.java:1055)

#-4 php.java.bridge.Request.handleRequest(Request.java:417)

#-3 php.java.bridge.Request.handleRequests(Request.java:500)

#-2 php.java.bridge.http.ContextRunner.run(ContextRunner.java:145)

#-1 php.java.bridge.ThreadPool$Delegate.run(ThreadPool.java:60)

#0 http://127.0.0.1:8080//JavaBridge/java/Java.inc(232): 
java_ThrowExceptionProxyFactory->getProxy(10, 'java.lang.Class', 'T', true)

#1 http://127.0.0.1:8080//JavaBridge/java/Java.inc(360): java_Arg->getResult(true)

#2 http://127.0.0.1:8080//JavaBridge/java/Java.inc(366): java_Client->getWrappedResult(true)

#3 http://127.0.0.1:8080//JavaBridge/java/Java.inc(560): java_Client->getResult()

#4 http://127.0.0.1:8080//JavaBridge/java/Java.inc(1752): java_Client->invokeMethod(7, 'fillReport', Array)

#5 http://127.0.0.1:8080//JavaBridge/java/Java.inc(1851): java_JavaProxy->__call('fillReport', Array)

#6 http://127.0.0.1:8080//JavaBridge/java/Java.inc(1999): java_AbstractJava->__call('fillReport', Array)

服务器设置:

java version "1.7.0_06"
Java(TM) SE Runtime Environment (build 1.7.0_06-b24)
Java HotSpot(TM) 64-Bit Server VM (build 23.2-b09, mixed mode)
php PDF library  PDFlib-Lite-7.0.5p3
php-java-bridge_6.2.1
jdk-7u6-linux-x64.rpm
apache-tomcat-7.0.29

我试过了:

CATALINA_OPTS="-Djava.awt.Headless=true

我有一个类似的生产服务器,具有相同的上述设置,我可以从我的 PHP 网站运行碧玉报告。

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';
        echo $port."<BR><BR>";
        if ($sapi_type == "cgi" || $sapi_type == "cgi-fcgi" || $sapi_type == "cli")
        {
            echo "2 <BR><BR>";
            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")))
            {echo "3 <BR><BR>";
                return "java extension not installed.";
            }
        }
        else
        {echo "4 <BR><BR>";
            if(!(@include_once("http://127.0.0.1:8080//JavaBridge/java/Java.inc")))
            {echo "5 <BR><BR>";
                require_once("http://127.0.0.1:$port/java/Java.inc");
            }
        }
    }
    if(!function_exists("java_get_server_name"))
    {echo "6 <BR><BR>";
        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();

$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", "Hello World!");
$params->put("number", 3.00);
$params->put("date", convertValue("2010-01-01 0:0:0", "java.sql.Timestamp"));

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

?>

贾斯珀报告:

<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="test" pageWidth="595" pageHeight="842" columnWidth="535" leftMargin="30" rightMargin="30" topMargin="20" bottomMargin="20">
    <property name="ireport.scriptlethandling" value="0"/>
    <property name="ireport.encoding" value="UTF-8"/>
    <import value="net.sf.jasperreports.engine.*"/>
    <import value="java.util.*"/>
    <import value="net.sf.jasperreports.engine.data.*"/>
    <parameter name="text" class="java.lang.String" isForPrompting="false">
        <defaultValueExpression><![CDATA[""]]></defaultValueExpression>
    </parameter>
    <parameter name="number" class="java.lang.Double" isForPrompting="false">
        <defaultValueExpression><![CDATA[new java.lang.Double(0)]]></defaultValueExpression>
    </parameter>
    <parameter name="date" class="java.util.Date" isForPrompting="false"/>
    <background>
        <band splitType="Stretch"/>
    </background>
    <title>
        <band height="50" splitType="Stretch"/>
    </title>
    <pageHeader>
        <band height="50" splitType="Stretch"/>
    </pageHeader>
    <columnHeader>
        <band height="30" splitType="Stretch"/>
    </columnHeader>
    <detail>
        <band height="223" splitType="Stretch">
            <textField isBlankWhenNull="false">
                <reportElement key="textField-1" x="75" y="17" width="312" height="100"/>
                <box>
                    <topPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                    <leftPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                    <bottomPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                    <rightPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                </box>
                <textElement>
                    <font size="18"/>
                </textElement>
                <textFieldExpression class="java.lang.String"><![CDATA["Passed Text: "+$P{text}]]></textFieldExpression>
            </textField>
            <textField isBlankWhenNull="false">
                <reportElement key="textField-2" x="78" y="130" width="316" height="32"/>
                <box>
                    <topPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                    <leftPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                    <bottomPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                    <rightPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                </box>
                <textElement>
                    <font size="12"/>
                </textElement>
                <textFieldExpression class="java.lang.String"><![CDATA["And a number: "+$P{number}]]></textFieldExpression>
            </textField>
            <textField isBlankWhenNull="false">
                <reportElement key="textField-3" x="77" y="173" width="316" height="32"/>
                <box>
                    <topPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                    <leftPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                    <bottomPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                    <rightPen lineWidth="0.0" lineStyle="Solid" lineColor="#000000"/>
                </box>
                <textElement>
                    <font size="12"/>
                </textElement>
                <textFieldExpression class="java.lang.String"><![CDATA["And a date: "+$P{date}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
    <columnFooter>
        <band height="30" splitType="Stretch"/>
    </columnFooter>
    <pageFooter>
        <band height="50" splitType="Stretch"/>
    </pageFooter>
    <lastPageFooter>
        <band height="50" splitType="Stretch"/>
    </lastPageFooter>
    <summary>
        <band height="50" splitType="Stretch"/>
    </summary>
</jasperReport>
4

0 回答 0