using JasperReports with php
Installation
- install JasperServer - bundles java based applications (tomcat, ant) but also mySQL and iReports (don't need to install those on the server)
- to work with the reports, you'll need to install it locally as well
- to generate scheduled reports from php, enable SOAP extension for php (php.ini)
- install SOAP and Net_Dime PEAR libraries for php
- open IReports: add/create the SQL query, design & build the report
- start jasper server (tomcat)
- open jasper application (e.g. http://localhost:8080/jasperserver/)
- under reports, right click > Add resource > JasperServer Report
- fallow the wizard to upload the jrxml file
// add connection credentials in session
session_start();
$_SESSION['username'] = 'jasperadmin';
$_SESSION['username'] = 'jasperadmin';
$uri = '/reports/samples/report1'; // this is the report - the path is being used by jasperserver to id the report
$format = 'HTML '; // HTML|XLS|PDF
require_once 'client.php';
$output_params[RUN_OUTPUT_FORMAT] = $format;
ws_runReport($uri, $report_params, $output_params, $attachments);
// display the reports
if (is_array($attachments)) { // set in ws_runReport by reference
if ( $format == 'PDF' ) {
header ( "Content-type: application/pdf" );
} else if ( $format == 'XLS' ) {
header ( 'Content-type: application/xls' );
header ( 'Content-Disposition: attachment; filename="report.xls"');
} else if ( $format == 'HTML' ) {
//header ( "Content-type: text/html");
// optional: save the report on disc
// save any image
foreach (array_keys($attachments) as $key){
if ($key != "cid:report"){
$f = fopen("images/".substr($key,4),"w");
fwrite($f, $attachments[$key]);
fclose($f);
}
}
}
echo $attachments["cid:report"];
}
Issues
- is java based, and steep learning curve. I'm sure it has features that I didn't even scratched the surface off.
- might have bugs (for example, I could not render a piechart with iReports; the barchart worked)
- reports are not very interactive (difficult to create rich html or javascript). Reports are more like PDFs.
- report compiling can be done using jasper app interface, and (probably) using Apache ant but is more difficult.
- difficult to integrate in a project. Output is a whole html document and you can't add anything else inside (e.g. menus, pagination)
- images (resources) - difficult to handle: upload each one manually, fatal errors if missing
- I don't recommend it as a fast or flexible solution.
- It might be a solution when dealing with a large number of reports that would take longer to build with classical tools (i.e. php / html)
- admin's blog
- Login or register to post comments


thanks man, but, where is the
thanks man, but, where is the client.php file?
within the jasper
within the jasper application. Look in the php-sample folder