PHP code example of dhenyson / jasper-report-php

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


...
$config = [
  'enableLog' => false,
  'dbConnection' => [
    'driver' => 'mysql',
    'host' => '127.0.0.1',
    'port' => '3306',
    'database' => 'test',
    'username' => 'root',
    'password' => '123456',
  ]
];
$jasperReport = new JasperReport($fileOutputDir, $fileName, $config);
$filePath = $jasperReport->process();
...


...
$jasperReport = new JasperReport($fileOutputDir, $fileName);
$jasperReport->setDbDriver('mysql');
$jasperReport->setDbHost('127.0.0.1');
$jasperReport->setDbPort('3306');
$jasperReport->setDbDatabase('test');
$jasperReport->setDbUsername('root');
$jasperReport->setDbPassword('123456');

$jasperReport->setParameter('UserId', '123');

$filePath = $jasperReport->process();
...

$defaultConfig = [
  'jasperStarterPath' => __DIR__ . '/../bin/jasperstarter/bin/jasperstarter',
  'enableLog' => true,
  'dbConnection' => [
    'driver' => 'mysql',
    'host' => null,
    'port' => null,
    'database' => null,
    'username' => null,
    'password' => null,
  ],
  'jdbcDir' => __DIR__ . '/../jdbc',
];



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 
bash
docker exec dhenyson_jasper_report_php php tests/ManualTest.php
bash
docker exec dhenyson_jasper_report_php vendor/bin/phpunit tests/JasperReportTest.php