Click here to Skip to main content
Click here to Skip to main content
Go to top

Integrating BIRT reports with PHP Web sites

, 1 Sep 2011
Rate this:
Please Sign up or sign in to vote.
How to consume reports created on BIRT via PHP

 

Introduction 

Considering the nowadays difficulties during the crysis, and given the evolution of open source programming languages, techniques and tools, it is important to start considering these options when choosing the technology for a given solution. 

This article introduces Eclipse BIRT and explains how you can integrate its reports with a PHP web site, so you can work it out easily and no cost.

Background

Eclipse BIRT - Business Intelligence and Reporting Tools (BIRT) is a reporting system that you can integrate with the well known Eclipse IDE. You can generate several solutions, however on this article the focus is using the produced reports on PHP web sites (a common requirement that usually is made using other technologies). If you are aware of other technologies such as Microsoft SQL Reporting Services, you'll find this tool very simple and straight forward. 

More about BIRT: http://www.eclipse.org/birt/phoenix/

As sometimes happens, you want to call a report on a web site. If you had ever use a RDLC file on an ASP.Net web site, or a Crystal Reports report, here we want to do the same, however, this requires some skills and specific sequence of actions to make it work.

In the first place, I made this article using XAMPP, which wraps Apache and MySql (as well as other servers) in an easy way that you can start and stop whenever you like. You can do this without it, however I highly recommend this tool. Download at: http://www.apachefriends.org/en/xampp.html 

This seems to be a very simple approach, however it's not! There are two main challenges here:

a) Follow a very specific list of steps to make things work (install programs, SDK, configurations...)

b) Since BIRT runs on Java and you want to consume it in PHP, there must be something to make them "talk" with each other. Here enters PHP JavaBridge, which connects a native script language such as PHP or Python to a Java service such as Tomcat. This will be our translator.

The first step is to assemble your environment. Here you will install some software, SDK, make configurations, copy and paste folders... This might take a few hours to make it right.

1. Pre-requirements
1.1. Java runtime and SDK


2. Software to Install
2.1. You need to activate Tomcat. XAMPP contains it out of the box installed, however, before starting, set the environment variable
JAVA_HOME on your Windows XP to the java SDK directory. This requires restart to the machine. After that, go to <Xampp folder>\tomcat and press Catalina_start.bat to start tomcat
2.2. Download PHP/Java bridge (At: http://php-java-bridge.sourceforge.net/pjb/download.php - version "Documentation". Paste JavaBridge.war file at <Xampp folder>\tomcat\webapps ahd check installation at http://127.0.0.1:8081/JavaBridge/
2.3. Install Commons Logging (Download at: http://commons.apache.org/logging/download_logging.cgi - Binaries, version.zip). Copy .jar files into <Xampp folder>\tomcat\webapps\birt-viewer\WEB-INF\lib\
2.4. Install BIRT Runtime (Download at: http://download.eclipse.org/birt/downloads/ where says "Latest BIRT runtime Release Build") - copy WebViewerExample folder to <Xampp Folder>\tomcat\webapps\ and rename it to Birt-Viewer.
2.5. Start Apache at XAMPP;
2.6. Start Tomcat at<Xampp Folder>, executing catalina_start.bat
2.7. Test tomcat: http://localhost:8080/ (if something saying tomcat shows, it's ok)
2.8. Test JavaBridge: http://localhost:8080/JavaBridge (if something saying javabridge shows, it's ok)
 

 

At this point you should have the environment working, with both BirtViewer and JavaBridge installed and running. Now you're ready to use the PHP code.

Using the code

There are two ways to consume the code - one via Birt Viewer, other directly via JavaBridge. Via Birt Viewer you consume the report via http, and the result is the report and the options to print, export...however you must be aware of security concerns since you will have to be able to access the URL of the report from the web. Via Javabridge, you access the file directly (not via http), so what is rendered is simple HTML. However, you don't have the print and export options...

 

Now just create a new web site on Apache (not on tomcat). Remember to have tomcat service started!!

Calling via Birt Viewer, I've used the TopNPercent report that BirtViewer brings out of the box:

<?php
		
   //	1. Http request
			
  $fname = "TopNPercent.rptdesign"; // name of the report
  $topCount = 5;	//	Top Count parameter
  $topPercentage = 25;	//	Top Percentage parameter
				
  // Redirect browser
  $dest = "http://localhost:8081/birt-viewer/frameset?__report=";
  $dest .= urlencode( realpath( $fname ) );
  $dest .= "&Top Count=" . urlencode( $topCount );
  $dest .= "&Top Percentage=" . urlencode( $topPercentage );
				
  header("location: ".$dest);
			

?>
		

Calling via JavaBridge directly, using the same report:

 

	
<?php		
  //	2.	Local file using BIRT runtime engine
			
  if (!(get_cfg_var('java.web_inf_dir'))) {
    define ("JAVA_HOSTS", "127.0.0.1:8081");
    define ("JAVA_SERVLET", "../../tomcat/webapps/JavaBridge/JavaBridge.phpjavabridge");
  }
			
  $pth = "http://" . $_SERVER["HTTP_HOST"] . $_SERVER["PHP_SELF"];
  $path_parts = pathinfo($pth);
  $imageURLPrefix = $path_parts['dirname'] ."/images/";
  require_once("../../tomcat/webapps/JavaBridge/java/Java.inc");

  session_start(); 
  $here = getcwd();

  $ctx = java_context()->getServletContext();
  $birtReportEngine =        java("org.eclipse.birt.php.birtengine.BirtEngine")->getBirtEngine($ctx);
  java_context()->onShutdown(java("org.eclipse.birt.php.birtengine.BirtEngine")->getShutdownHook());

  try{
        $report = $birtReportEngine->openReportDesign("${here}/TopNPercent.rptdesign");
        $task = $birtReportEngine->createRunAndRenderTask($report);
        $task->setParameterValue("Top Count", new java("java.lang.Integer", 6));
	$task->setParameterValue("Top Count", new java("java.lang.Integer", 6));

	$taskOptions = new java("org.eclipse.birt.report.engine.api.HTMLRenderOption");
	$outputStream = new java("java.io.ByteArrayOutputStream");
	$taskOptions->setOutputStream($outputStream);
	$taskOptions->setOutputFormat("html");
	$ih = new java( "org.eclipse.birt.report.engine.api.HTMLServerImageHandler");
	$taskOptions->setImageHandler($ih);
	$taskOptions->setEnableAgentStyleEngine(true);
	$taskOptions->setBaseImageURL($imageURLPrefix . session_id());
	$taskOptions->setImageDirectory($here . "/images/" . session_id());
	$task->setRenderOption( $taskOptions );
	$task->run();
	$task->close();  } catch (JavaException $e) {
	echo $e; //"Error Calling BIRT";			
  }    echo $outputStream;
?>
 

 

Points of Interest

The problem here is assemble everything. It's very easy, without a guiding line, to install it wrong. The above steps took me more than one week and googling a lot (really a lot) but seems to me that they will save you a lot of trouble and time (so I hope).

As a 5 year experience consultant using Microsoft technologies, I find myself working with these solutions without many problems. The "first time" takes longer, but after having things assembled, is as easy as other technologies.

Try this out, it's worth. If you want to make the same with less money, here is your answer.

 

NOTE: if you want to integrate BIRT with MySql, there is no driver installed. Follow these instructions: http://www.slideshare.net/vazi/birt-installation-and-configuration

History 


 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

david.ribeiro

Portugal Portugal
No Biography provided

Comments and Discussions

 
GeneralMy vote of 5 Pinmembersimemon28-Aug-13 23:40 
Questionhosting solution [modified] Pinmembervivek modi30-Jan-13 20:57 
GeneralMy vote of 5 Pinmembermanoj kumar choubey23-Feb-12 19:37 
QuestionAdditional Links PinmemberJason Weathersby24-Aug-11 13:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.140922.1 | Last Updated 1 Sep 2011
Article Copyright 2011 by david.ribeiro
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid