1. Go to this page and download the library: Download stesa/phpjasper 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/ */
stesa / phpjasper example snippets
use Stesa\PHPJasper\Facades\PHPJasper;
// Compile a JRXML template
PHPJasper::compile(storage_path('app/reports/templates/invoice.jrxml'))
->execute();
// Generate a PDF report
PHPJasper::process(
storage_path('app/reports/templates/invoice.jrxml'),
storage_path('app/reports/output/invoice'),
[
'format' => 'pdf',
'db_connection' => [
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'mydb',
'username' => 'root',
'password' => 'secret'
]
]
)->execute();
// Download the report
return response()->download(storage_path('app/reports/output/invoice.pdf'));
use Stesa\PHPJasper\PHPJasper;
// Option 1: Set via constructor
$jasper = new PHPJasper(null, '/usr/lib/jvm/java-11-openjdk/bin/java');
$jasper->compile($input)->execute();
// Option 2: Set via method
PHPJasper::setJavaPath('/usr/lib/jvm/java-17-openjdk/bin/java')
->process($input, $output, ['format' => 'pdf'])
->execute();
// Get current Java path
$javaPath = PHPJasper::getJavaPath();
use Stesa\PHPJasper\PHPJasper;
$jasper = new PHPJasper('/custom/path/to/jasperstarter.jar');
$jasper->compile($input)->execute();
use Stesa\PHPJasper\PHPJasper;
class ReportService
{
protected $jasper;
public function __construct(PHPJasper $jasper)
{
$this->jasper = $jasper;
}
public function generate($template, $output)
{
$this->jasper->process($template, $output, [
'format' => 'pdf'
])->execute();
}
}