Jump to content
Changes to the Jaspersoft community edition download ×

JasperReports and PHP


laceja

Recommended Posts

I have seen this question posted in very old posts, but never an answer.  Does Jasper Reports (and/or iReport) include the ability to call a report from a PHP script?  I can imagine this would be very useful for anyone developing in PHP.  I did see something posted by Richard Johnson, but it requires one to create and compile some Java classes.  I can spell Java, but that's about all I know about it.  I'm certain there are many PHP programmers in the same boat.  It would really be nice to have such an interface for using JasperReports.

Any assistance would be greatly appreciated.

Thanks,

Merrill

I managed to get an interface working, using the methods detailed by Richard Johnson.  However, the example included there only uses an empty datasource.  Is there any documentation on the API to pass in other datasources, like a jdbc datasource?

Thanks,

Merrill



Post Edited by laceja at 2010-09-13 17:52
Link to comment
Share on other sites

  • Replies 1
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

Hi,

You will need something like the code below.

The code maps input parameters with a "report_" prefix (e.g., "report_arr" for arrays, "report_int" for numbers, etc.). So you can do something like:

<input type="hidden" name="report_int_Age" value="42" />

This will pass the "Age" parameter into the report with a value of 42.

You must define (inside 'login.php') some global PHP variables:

    * $dbhost - The database server name (e.g., 'localhost').
    * $dbname - The name of the database (e.g., 'corporate_data').
    * $dbuser - The name of the user who can access the database (e.g., 'postgres').
    * $dbpass - The password for the $dbuser (e.g., 'secretcatfoodformulae').

You will want to change:

    * realpath( './reports/report-filename.jasper' ); -- point this line to the relative path where the report filename can be found.
    * function report_execute( $filename = 'output-filename' ) -- give this line a proper file name.
    * java( 'java.lang.Class' )->forName( 'org.postgresql.Driver' ); -- change the driver to the JDBC driver for your database (Oracle and MySQL use a different driver class name).

The code below only generates PDF files. You will have to read up on the JasperReports API to output other formats.

Hope it helps.
 

Code:
<?phpinclude_once( 'login.php' );checkjavaExtension();function report_parse_post_parameters() {  # Automatically extract report parameters (data types converted in report).  $params = new java('java.util.HashMap');  # Pass the remaining POST "report_TYP" variables as report parameters.  foreach( $_POST as $name => $value ) {    if( strpos( $name, 'report_' ) === 0 ) {      $length = strlen( 'report_' );      if( strpos( $name, 'report_int_' ) === 0 ) {        $value = intval( $value );        $length = strlen( 'report_int_' );        $value = convertValue( $value, 'java.lang.Integer' );        $params->put( substr( $name, $length ), $value );      }      else if( strpos( $name, 'report_arr_' ) === 0 ) {        $length = strlen( 'report_arr_' );        $arrays = array_filter( explode( ',', $_POST[ $name ] ) );        # Map the values of the array form parameter to a java.util.ArrayList.        $arrayList = new java( 'java.util.ArrayList' );        foreach( $arrays as $value ) {          $arrayList->add( $value );        }        # Pass values into the report (without the "report_arr_" prefix).        $params->put( substr( $name, $length ), $arrayList );      }      else {        $params->put( substr( $name, $length ), $value );      }    }  }  return $params;}function report_execute( $filename = 'output-filename' ) {  global $dbhost;  global $dbname;  global $dbuser;  global $dbpass;  $PERSIST = 'jdbc';  $conn = null;  $report = realpath( './reports/report-filename.jasper' );  try {    $params = report_parse_post_parameters();    # Load the PostgreSQL database driver.    java( 'java.lang.Class' )->forName( 'org.postgresql.Driver' );    # Attempt a database connection.    $conn = java( 'java.sql.DriverManager' )->getConnection(      "jdbc:postgresql://$dbhost/$dbname?user=$dbuser&password=$dbpass" );    # Use the fill manager to produce the report.    $fm = java('net.sf.jasperreports.engine.JasperFillManager');    $pm = $fm->fillReport($report, $params, $conn);    header('Cache-Control: private');    header('Content-Description: File Transfer');    header("Content-Disposition: attachment, filename=$filename.pdf");    header('Content-Type: application/pdf');    header('Content-Transfer-Encoding: binary');    java_set_file_encoding('ISO-8859-1');    $em = java('net.sf.jasperreports.engine.JasperExportManager');    $result = $em->exportReportToPdf($pm);    $conn->close();    header('Content-Length: ' . strlen( $result ) );    echo $result;  }  catch( Exception $ex ) {    if( $conn != null ) {      $conn->close();    }    throw $ex;  }}?>
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...