Connect PHP with Jasper Reports
Posted by admin on March 5th, 2008 filed in ReportingThis is an interesting piece of code that I found on the web. I’ve not yet tested this but it sure does look promising.
$reportsPath ="/home/ccharly/publichtml/utils/reports/";
$reportFileName = "CommandesClients1";
$jasperReportsLib = "/home/ccharly/publichtml/utils/jasperlib";if(extension_loaded('java')) {
// lecture du répertoire où sont rengés les librairies utiles à JasperReports
$handle = @opendir($jasperReportsLib);// ajout de tous les fichier jar au chemin de classe (Class Path)
while(($new_item = readdir($handle))!==false) {$java_library_path .= 'file:'.$jasperReportsLib.'/'.$new_item .';';
}try {
// chargement des librairies au classpath
java_require($java_library_path);// création de la connexion JDBC
$Conn = new Java("org.altic.jasperReports.JdbcConnection");
// driver
$Conn->setDriver(“com.mysql.jdbc.Driver”);
// url de connexion
$Conn->setConnectString(“jdbc:mysql://localhost/erpmart”);
// utilisateur
$Conn->setUser(“root”);
// mot de passe
$Conn->setPassword(null);// Compilation du fichier JRXML en fichier Jasper
$sJcm = new JavaClass(“net.sf.jasperreports.engine.JasperCompileManager”);
$report = $sJcm->compileReport($reportsPath .$reportFileName.”.jrxml”);// Remplir le modèle avec les données
$sJfm = new JavaClass(“net.sf.jasperreports.engine.JasperFillManager”);
$print = $sJfm->fillReport(
$report,
new Java(“java.util.HashMap”),
$Conn->getConnection()
);// Export du fichier au format pdf
$sJem = new JavaClass(“net.sf.jasperreports.engine.JasperExportManager”);
$sJem->exportReportToPdfFile($print, $reportsPath .$reportFileName.”.pdf”);if (file_exists($reportsPath .$reportFileName.”.pdf”)){
header(‘Content-disposition: attachment; filename=”‘.$reportFileName.’.pdf”‘);
header(‘Content-Type: application/pdf’);
header(‘Content-Transfer-Encoding: binary’);
header(‘Content-Length: ‘. @filesize($reportsPath . $reportFileName.”.pdf”));
header(‘Pragma: no-cache’);
header(‘Cache-Control: must-revalidate, post-check=0, pre-check=0′);
header(‘Expires: 0′);
set_time_limit(0);
@readfile($reportsPath .$reportFileName.”.pdf”) or die(“problem occurs.”);
}} catch (JavaException $ex) {
$trace = new Java(“java.io.ByteArrayOutputStream”);
$ex->printStackTrace(new Java(“java.io.PrintStream”, $trace));
print “java stack trace: $trace\n”;
}
}?>
Leave a Comment