Jump to content
Changes to the Jaspersoft community edition download ×

Jasper Report with PHP


mahagana

Recommended Posts

Hi,

 

Could anybody provide me some leads on how to use jasper report API's with a PHP application.

 

I want to call a jasper report from a PHP application.The call should return a report in PDF format using jasper report.

 

Appropriate solution with an example would be of immense help.

 

Thanks,

Mahesh

Link to comment
Share on other sites

  • Replies 2
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

Hello,

 

You can use php / java integration to call JR directly from php.

 

Look in the forum for php or ask google with keywords php + java + jasperreports

 

here is a sample for internet (http://charly-clairmont.developpez.com/tutoriels/php-jasper-reports/)

Code:

<?php
$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: $tracen";
}
}


?>
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...