1. Go to this page and download the library: Download dhenyson/jasper-report-php 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/ */
dhenyson / jasper-report-php example snippets
use Dhenyson\JasperReportPHP\JasperReport;
...
$fileOutputDir = __DIR__ . '/../../../storage/app/public';
$fileName = "example" // or "example.jrxml"
$jasperReport = new JasperReport($fileOutputDir, $fileName);
$jasperReport->setParameter('MyParameter', 'Hello world!');
$filePath = $jasperReport->process("xls"); // default = "pdf"
echo($filePath); // example output -> /app/storage/app/public/example.xls
...
use Dhenyson\JasperReportPHP\JasperReport;
use Illuminate\Http\Request;
class JasperReportsController extends Controller
{
public function generateReport(Request $request)
{
// get file from api request
$jrxmlFile = $request->file('jrxmlFile');
// set default path and names
$fileOutputDir = __DIR__ . '/../../../../storage/app/public';
$newFileName = 'my_report';
$jrxmlFilePath = "$fileOutputDir/$newFileName.jrxml";
$jasperFilePath = "$fileOutputDir/$newFileName.jasper";
// save the file somewhere
$jrxmlFile->storeAs('public', "$newFileName.jrxml");
// Create jasper object and process
$jasperReport = new JasperReport($fileOutputDir, $newFileName);
$parameters = $jasperReport->getParameters(); // do something with the parameters
$jasperReport->setParameter('MyParameter', 'Parameter value');
$jasperReport->setParameter('MySecondParameter', 'Hello World');
$outputFilePath = $jasperReport->process();
// If necessary, remove the files created in the process to free up space
if (file_exists($jrxmlFilePath)) {
unlink($jrxmlFilePath);
}
if (file_exists($jasperReport->getJasperFilePath())) {
unlink($jasperReport->getJasperFilePath());
}
// Return file to API and delete file
return response()->download($outputFilePath)->deleteFileAfterSend(true);
}
bash
composer
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.