2017-04-13 10:05
浏览 72

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

I'm building a web application using symfony 2 php. And I want to generate some reports using jasperreports. I used Java Bridge and I have the Java bridge war deployed in my tomcat 7 but when I try to generate my reports I face an error.

So here is my code in my controller

 $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 .';';
       $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");
    @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; 

And here is the exception I get:

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

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

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

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

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

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

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

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

    at java_Protocol->invokeEnd()
        in http://localhost:8080/JavaBridge/java/ 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/ 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

I looked everywhere but I couldn't find a solution, am I missing something?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douju1968
    douju1968 2017-04-14 09:10

    Multiple problems here, but I'll try to explain step by step:

    1. The code part.

    You should not use java_require to dynamically add library dependencies from PHP (has been removed from v6).

    2. Error reporting

    As you noticed the symfony error handler couldn't provide very helpful information (no clear message). Not a symfony problem, but more about the way exceptions/errors are handled by the client. The bridge exceptions (java bridge side) must be retrieved explicitly using getLastException (not 100% correct, but let's assume it to make things simple). Of course no error handler (symfony, whoops...) has support for this.

    So what to do ?

    1. Fix registration of your java libs

    Your dependencies must be either provided in the war bundle (what is often called the JavaBridgeTemplate.war) or in the tomcat global lib directory.

    Personnally I prefer the first method to prevent possible collisions and it can be achieved by either:

    • adding the jar's into your WEB-INF/lib directory inside the war file and redeploy (this is generally how it was done by most php devs).

    • Or another option (totally opinionated and based on a javabridge fork I initiated few months ago), is to build your own jar. As a quick example with jasper and mysql, you can download, unzip and build your own java bridge war with:

      wget -O
      unzip && 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.

    A more complete installation is located:

    2. The error reporting problem.

    The symfony error handler (or whoops...) will not play well with the client and debugging can really be a mess. I suggest to use the soluble-japha client instead: which, amongst other things, will throw exceptions in a more standard way, see here.

    The soluble-japha is just the client part, it replaces the and is compatible with the official php-java-bridge 6/7 and the soluble php-java-bridge server implementations. You'll have to slighty change you PHP code but nothing very hard. See the documentation website.

    From there you might have a better view of the problem.

    Hope it helps.

    点赞 评论