Oh well… my first blog entry in English, because I think this article is interesting for all the PHP developers out there – including non-German people 😉

Every developer will reach the point generating reports for the running project. Making reports with Java oder .NET is relatively easy. Products like Crystal Reports (commercial) or BIRT (open source) support developers in designing and creating pleasant reports.

PHP can be extended with the function of BIRT with buying the Zend Platform. But in most cases the minority of people is able to buy this product. Zend Platform costs around 1000$ – 1500$ for a one year license.

Our development team had the problem of generating reports with PHP more than one time. In our last project we used FOP + XSL/XSLT from the Apache Foundation and triggered the report building by using exec() and command line.
The problem with FOP is that it is highly memory intensive and the building of XSLT templates is time consuming – if you do not have nice tools for that.
Last week I investigated and evaluated BIRT for using it in a J2EE/GWT-based application we are programming. The BIRT Designer is a nice tool for creating reports in HTML and/or PDF and we will (hopeley) include it in our project.
I demonstrated the results Christoph who was really amazed of it. Christoph said “Well… it would be so nice using BIRT with PHP for ZABOS (ZABOS is our SMS Alert System). But Zend Platform is sooo expensive.” (mimimi)
It was friday afternoon and nothing more was to do so I decided to look for reporting mechanisms in PHP. The results were… let’s say: poor 😉

The next idea was triggering BIRT from command line via PHP exec()’s method. I wrote a wrapper class in Java, which could be used to start BIRT from command line (using java -jar de.ecw.birt.BIRTRunner –help).
After some minutes of coding and testing this class worked like a charme. I was happy, but I thought “Woah… It would be nice using Java directly from PHP”. I remembered that there was a PHP module called php_java which could access Java through the JNI (Java Native Interface). But all the comments on php.net said the old php_java module is more than deprecated and does not work. The newer commentator said that the php/Java-Bridge was a worth checking at. Hm… I read the documentation of the php/Java-Bridge and was enthusiastic: This was exactly what I needed.

I downloaded the latest release and was a little bit confused about the different files and JARs.
The solution is easy: Unpack the JavaBridge.war archive. The folder /java is needed in your PHP project path and includes all classes for connecting to the JavaBridge-server.
The file /WEB-INF/lib/JavaBridge.jar must be extracted to a directory of your choice (= $path-to-java-bridge). Please create inside this directory another directory called ‘lib’.
Your folder must look like this:

/java-bridge
JavaBridge.jar
/lib

Afterwards I started the server:

cd $path-to-java-bridge
java.exe -Dphp.java.bridge.base=$path-to-java-bridge -jar JavaBridge.jar SERVLET:8080 6 php-java-brige.log

The log file did not show any error so I tried a simple “Hello world” in Java and PHP:

<?php
  // index.php
  require_once("java/Java.inc");
  $aString = new Java("java.lang.String", "Hello world");
  print $aString;
?>

Really nice: PHP connected to the local JavaBridge server. The JavaBridge instantiates a new String-object an called the toString() method. This was exactly what I needed for using php/Java-bridge with BIRT.

I refactored my own BIRTRunner class so that I just needed to call 4 methods and I got the desired result.
The appended .jar file must be extracted to the $path-to-java-bridge/lib, and you must restart the JavaBridge AND Apache. There seems to be an issue with UTF-8/JavaBridge/Apache.

My index.php had to be refactored too:

<?php
  // index.php
  // require JavaBridge interface
  require_once("java/Java.inc");
  // set encoding to UTF-8
  java_set_file_encoding("UTF-8");

  // remove try-/catch-block if not using PHP5
  try
  {
    // where our reports are stored
    $targetFile = "d:/result.html";
    // ConcreteTask encapsulate the logic
    $birtTask = new Java("de.ecw.birt.task.ConcreteTask");
    // the home of your BIRT-runtime directory
    $birtTask->setEngineHome('D:DevelopmentBIRT_RuntimeReportEngine');
    // the path of your BIRT report design
    $birtTask->setReportPath('D:DevelopmentBIRTworkspaceCAPlanuser.rptdesign');
    // create new render options
    $htmlRenderOptions = new Java("org.eclipse.birt.report.engine.api.HTMLRenderOption");
    $htmlRenderOptions->setOutputFileName($targetFile);
    $birtTask->setRenderOption($htmlRenderOptions);
    // run the BIRT task
    $birtTask->run();

    // get file content and print to browser
    $content = file_get_contents($targetFile);
    echo $content;
  }
  catch (JavaException $e)
  {
    print $e->getMessage();
  }
?>

To my surprise it worked. I had not really expected it – but I was really able to use PHP and BIRT without buying Zend Platform.

There are so many possibilities and TODOs I want to mention:
* You can create the .rptdesign on-the-fly using Smarty
* You cold write a wrapper class on PHP side so it is much easier to create reports
* … to be continued

I am asking you for a donation.

You liked the content or this article has helped and reduced the amount of time you have struggled with this issue? Please donate a few bucks so I can keep going with solving challenges.


8 Comments

Christoph · April 24, 2007 at 5:36 pm

Looks very nice. Is it tested with large reports? 30 to 50 Pages?

schakko · April 24, 2007 at 10:31 pm

not yet, I will do this test tomorrow.

Matthias · February 27, 2008 at 2:23 pm

Hi there,

I read that you’re able to use BIRT/PHP without ZEND…

That’s very interesting and I’d like to do it as well…

My Problem is, I installed the bridge, apache, php, java but I don’t know how to start…
I like to print a simple report, but I don’t know how…

Can you plese give a hint?

Greetings, matthias

Schakko · February 27, 2008 at 8:47 pm

Hello Mathias,
What problem do you exactly have?

I will publish some wrapper classes in PHP next week.

Matthias · March 4, 2008 at 10:22 am

Dear Schakko,

My problem is I don’t find a manual how to use the birt runtime engine.

I have:
the sample report Hello_World.rptdesign and the birt runtime engine…

If I try to use your code and changethe locations of the files, nothing happens…

I really only want to create the pdf or html or anything else for using birt…

Greetings and thanks a lot for your help!!!
Matthias

Schakko · March 4, 2008 at 9:22 pm

Do you want to use BIRT in conjunction with PHP or Java?

If you want to produce BIRT reports with help of Java, I advice you to read [url]http://www.eclipse.org/birt/phoenix/deploy/reportEngineAPI.php[/url] headline “Report Engine”.

For using BIRT from command line, you should give the genReport.sh / genReport.bat in your BIRT installation directory a try. With help of this command line scripts you are able to generate reports from the command line. The script takes the name of the .rptdesign as an argument. The parameter [url]–format pdf[/i].

matthias · March 5, 2008 at 10:30 am

hi schakko,

nun multilingual(oder wie man das schreibt ;o))

GERMAN:
Ich hab die JavaBridge korrekt installiert[JavaBridge.war entpacken und JavaBridge.jar doppelklicken um zu starten]
–> Kann man die bridge eigentlich auch ohne das nervende dosfenster starten? ohne tomcat, nur apache und php…

nun hab ich hello_world.rptdesign und ein php script und möchte dieses laufen lassen, dass ist schon schlicht “alles” ;o)

Vielen Dank für die BIRT-Seite, hab aber da schon alles mehrfach durchgesehen :o( Ich hab es einfach nicht verstanden…

Viele grüße aus Augsburg, matthias

ENGLISCH:
I thinkl I have installed the javabridge correctly[unzip the javabridge.war, copy javabridge.jar an doubleklick it in order to start]
Btw:Is it possible to start the bridge in kind of silentmode, without that “very nice” dosboxwindow?

My problem:
I have a hello_world.rptdesign and a php script. I’d only like to generate that report, that’s “simply” everything

Thanks a lot for the BIRT-page, but I have already read all them more than once, but I don’t get things working…

Greetings from augsburg, matthias

Schakko · March 6, 2008 at 4:45 pm

Have you tried [url=http://wap.ecw.de/modules/blog/?action=show&id=336]this tutorial[/url]? It explains in detail how to start with pJb, PHP and BIRT.

You can run the pJb as a service with help of “sc” or SrvAny.exe.

Leave a Reply