使用 jasperreports Java 桥和 symfony 时出现的问题

Issues while using jasperreports Java bridge and symfony

我正在使用 symfony 2 php 构建 Web 应用程序。我想使用 jasperreports 生成一些报告。我使用了 Java Bridge,并且在我的 tomcat 7 中部署了 Java Bridge war,但是当我尝试生成报告时,我遇到了错误。

所以这是我控制器中的代码

 require_once("http://localhost:8080/JavaBridge/java/Java.inc");
 $reportsPath ="D:/jaspertest/";
        $reportFileName = "report";
        $jasperReportsLib = "D:/Programmes/jasperReport/lib";
        $java_library_path = '';
        $handle = @opendir($jasperReportsLib);
        while(($new_item = readdir($handle))!==false) {
            $java_library_path .= 'file:'.$jasperReportsLib.'/'.$new_item .';';
        }
        java_require($java_library_path);
       $sJcm = new JavaClass("net.sf.jasperreports.engine.JasperCompileManager");
        $report = $sJcm->compileReport($reportsPath .$reportFileName.".jrxml");

    $map = new Java("java.util.HashMap");
    $emptyDataSource = new Java("net.sf.jasperreports.engine.JREmptyDataSource");
    $sJfm = new JavaClass("net.sf.jasperreports.engine.JasperFillManager");
    $print = $sJfm->fillReport($report,$map,$emptyDataSource);
    $sJem = new JavaClass("net.sf.jasperreports.engine.JasperExportManager");
    $sJem->exportReportToPdfFile($print, $reportsPath .$reportFileName.".pdf");
    set_time_limit(0);
    @readfile($reportsPath .$reportFileName.".pdf");// or die("problem occurs.");
    $response = new Response($reportsPath .$reportFileName.".pdf");
    $response->headers->set('Content-type', 'application/pdf');
    $response->headers->set('Content-Disposition', 'attachement; filename=Report.pdf');
    $response->headers->set('Cache-Control', 'must-revalidate');
    return $response; 

这是我得到的异常:

Symfony\Component\Debug\Exception\ContextErrorException: Notice: fwrite(): 
    at n/a
        in http://localhost:8080/JavaBridge/java/Java.inc line 482

    at Symfony\Component\Debug\ErrorHandler->handleError('8', 'fwrite(): ', 'http://localhost:8080/JavaBridge/java/Java.inc', '482', array('data' => ''))
        in  line 

    at fwrite(resource, '')
        in http://localhost:8080/JavaBridge/java/Java.inc line 482

    at java_SocketChannel->fwrite('')
        in http://localhost:8080/JavaBridge/java/Java.inc line 528

    at java_SocketHandler->write('')
        in http://localhost:8080/JavaBridge/java/Java.inc line 671

    at java_HttpHandler->redirect()
        in http://localhost:8080/JavaBridge/java/Java.inc line 743

    at java_Protocol->redirect()
        in http://localhost:8080/JavaBridge/java/Java.inc line 755

    at java_Protocol->finish()
        in http://localhost:8080/JavaBridge/java/Java.inc line 794

    at java_Protocol->invokeEnd()
        in http://localhost:8080/JavaBridge/java/Java.inc line 359

    at java_Client->invokeMethod('0', 'updateJarLibraryPath', array('file:D:/Programmes/jasperReport/lib/.;file:D:/Programmes/jasperReport/lib/..;file:D:/Programmes/jasperReport/lib/jasperreports-5.6.0.jar;file:D:/Programmes/jasperReport/lib/jasperreports-applet-5.6.0.jar;file:D:/Programmes/jasperReport/lib/jasperreports-fonts-5.6.0.jar;file:D:/Programmes/jasperReport/lib/jasperreports-javaflow-5.6.0.jar;', 'c:/wamp/bin/php/php5.5.12/ext/', 'C:\wamp\www\syslife_v1\web', '.;C:\php\pear'))
        in http://localhost:8080/JavaBridge/java/Java.inc line 986

    at java_require('file:D:/Programmes/jasperReport/lib/.;file:D:/Programmes/jasperReport/lib/..;file:D:/Programmes/jasperReport/lib/jasperreports-5.6.0.jar;file:D:/Programmes/jasperReport/lib/jasperreports-applet-5.6.0.jar;file:D:/Programmes/jasperReport/lib/jasperreports-fonts-5.6.0.jar;file:D:/Programmes/jasperReport/lib/jasperreports-javaflow-5.6.0.jar;')
        in C:\wamp\www\syslife_v1\src\MamdaIT\ComptabiliteBundle\Controller\BordereauController.php line 372

    at MamdaIT\ComptabiliteBundle\Controller\BordereauController->editionDetailBordereau(object(Request), '66')
        in  line 

    at call_user_func_array(array(object(BordereauController), 'editionDetailBordereau'), array(object(Request), '66'))
        in C:\wamp\www\syslife_v1\vendor\symfony\symfony\src\Symfony\Component\HttpKernel\HttpKernel.php line 144

    at Symfony\Component\HttpKernel\HttpKernel->handleRaw(object(Request), '1')
        in C:\wamp\www\syslife_v1\vendor\symfony\symfony\src\Symfony\Component\HttpKernel\HttpKernel.php line 64

    at Symfony\Component\HttpKernel\HttpKernel->handle(object(Request), '1', true)
        in C:\wamp\www\syslife_v1\vendor\symfony\symfony\src\Symfony\Component\HttpKernel\DependencyInjection\ContainerAwareHttpKernel.php line 69

    at Symfony\Component\HttpKernel\DependencyInjection\ContainerAwareHttpKernel->handle(object(Request), '1', true)
        in C:\wamp\www\syslife_v1\vendor\symfony\symfony\src\Symfony\Component\HttpKernel\Kernel.php line 185

    at Symfony\Component\HttpKernel\Kernel->handle(object(Request))
        in C:\wamp\www\syslife_v1\web\app_dev.php line 30

我到处都找了,但找不到解决办法,我是不是漏掉了什么?

这里有很多问题,但我会尝试逐步解释:

1.代码部分.

您不应使用 java_require 从 PHP 动态添加库依赖项(已从 v6 中删除)。

2。报错

正如您所注意到的,symfony 错误处理程序无法提供非常有用的信息(没有明确的消息)。不是 symfony 问题,而是更多关于 exceptions/errors 由 Java.inc 客户端处理的方式。必须使用 getLastException 显式检索桥接异常(java 桥接端)( 不是 100% 正确,但让我们假设它使事情变得简单 )。当然没有错误处理程序(symfony,whoops ...)支持这个。


那怎么办?

1.修复 java libs

的注册问题

您的依赖项必须在 war 包(通常称为 JavaBridgeTemplate.war)或 tomcat 全局包中提供lib 目录。

就我个人而言,我更喜欢第一种方法来防止可能发生的碰撞,并且可以通过以下任一方法实现:

  • 将 jar 添加到 war 文件内的 WEB-INF/lib 目录并重新部署(大多数 php 开发者)。

  • 或者另一种选择(完全固执己见并且基于我几个月前发起的 javabridge fork),是构建您自己的 jar。作为 jasper 和 mysql 的快速示例,您可以下载、解压缩并构建您自己的 java 桥 war,其中:

    wget https://github.com/belgattitude/php-java-bridge/archive/7.0.0.zip -O pjb.zip
    unzip pjb.zip && cd php-java-bridge-7.0.0
    ./gradlew war -I init-scripts/init.jasperreports.gradle -I init-scripts/init.mysql.gradle
    

The builded war file will be available in the ./build/libs folder, ready to deploy.

更完整的安装位于:http://docs.soluble.io/soluble-japha/install_server/#build-and-customize

2。报错问题。

symfony 错误处理程序(或 whoops...)不能很好地与 Java.inc 客户端一起使用,调试真的很麻烦。我建议改为使用 soluble-japha 客户端:https://github.com/belgattitude/soluble-japha 其中,除其他事项外,将以更标准的方式抛出异常,请参阅 here.

soluble-japha只是客户端部分,它取代了Java.inc并兼容官方php-java-bridge 6/7和可溶性php-java-桥接服务器实现。您必须稍微更改一下 PHP 代码,但不是很难。参见documentation website

从那里您可能会更好地了解问题。

希望对您有所帮助。