PHP code example of julfiker / php-jaspersoft

1. Go to this page and download the library: Download julfiker/php-jaspersoft library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

julfiker / php-jaspersoft example snippets


 /** 
 * From controller action
 *
 * @param $path
 * @param string $type  pdf|xlsx|docx
 * @param string $name name of the report
 * @param array $params its optional, parameter of report in array if 
 
Route::get('/testing', function (Redirect $redirect) {
    return render_report('{REPORT_PATH}', 'pdf', 'name_of_the_report');
});


echo route('julfiker.jasper.report.render', ['path' => "{report_path}", 'type' => 'pdf', "name" => "{name_of_report}", "param" => 1]);

use Julfiker\Jasper\Manager\JasperReport;

$jasper =  new JasperReport();
$jasper->setServerUrl('jasper_server_url')
       ->setUsername('jasper_user')
       ->setPassword('jasper_password')
       ->make();
     
try {
    $reportContent = $jasper
        ->setPath("{JASPER_REPORT_PATH}")
        ->setType("pdf")
        ->setParams(["param1"=> 1, "param2" => 2])
        ->setRequestTimeout(30)  #30 seconds
        ->generate();
  
  

   //If pdf render
   echo $reportContent;
   header('Content-Type: pdf');
   header('Content-Disposition: inline; filename=report.pdf');
   exit;
   
   //if download in excel file with xlsx
    echo $reportContent;
    header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
    header('Content-Disposition: attachment; filename==report.xlsx');
    exit;
    
    //Note: If you response header framework specific where you are working on.
}
catch (\Exception $e) {
    echo $e->getMessage();
}