PHP code example of stesa / phpjasper

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'));



namespace App\Http\Controllers;

use Stesa\PHPJasper\Facades\PHPJasper;
use Illuminate\Http\Request;

class ReportController extends Controller
{
    public function generateInvoice($invoiceId)
    {
        $input = storage_path('app/reports/templates/invoice.jrxml');
        $output = storage_path('app/reports/output/invoice_' . $invoiceId);

        PHPJasper::process($input, $output, [
            'format' => ['pdf'],
            'params' => [
                'invoice_id' => $invoiceId
            ],
            'db_connection' => [
                'driver' => config('database.default'),
                'host' => config('database.connections.mysql.host'),
                'database' => config('database.connections.mysql.database'),
                'username' => config('database.connections.mysql.username'),
                'password' => config('database.connections.mysql.password'),
            ]
        ])->execute();

        return response()->download($output . '.pdf')
            ->deleteFileAfterSend(true);
    }
}

PHPJasper::compile($inputFile, $outputFile = null)->execute();

PHPJasper::compile(storage_path('app/reports/templates/report.jrxml'))
    ->execute();

PHPJasper::process($inputFile, $outputFile, $options = [])->execute();

[
    'format' => ['pdf', 'xlsx'], // Single format or array of formats
    'params' => [                 // Report parameters
        'my_param' => 'value'
    ],
    'db_connection' => [          // Database connection
        'driver' => 'mysql',      // mysql or postgresql
        'host' => 'localhost',
        'database' => 'mydb',
        'username' => 'user',
        'password' => 'pass'
    ],
    'resources' => '/path/to/resources', // Resource directory for images, etc.
    'locale' => 'en_US'                  // Locale for number/date formatting
]

PHPJasper::process(
    storage_path('app/reports/templates/sales.jrxml'),
    storage_path('app/reports/output/sales_report'),
    [
        'format' => ['pdf', 'xlsx'],
        'params' => [
            'start_date' => '2024-01-01',
            'end_date' => '2024-12-31'
        ]
    ]
)->execute();

$result = PHPJasper::listParameters($inputFile)->execute();

$params = PHPJasper::listParameters(
    storage_path('app/reports/templates/report.jasper')
)->execute();

print_r($params);

PHPJasper::process($input, $output, [
    'format' => ['pdf', 'xlsx', 'csv', 'html']
])->execute();

// Results in:
// - output.pdf
// - output.xlsx
// - output.csv
// - output.html

PHPJasper::process($input, $output, [
    'format' => 'pdf',
    'db_connection' => [
        'driver' => config('database.default'),
        'host' => config('database.connections.mysql.host'),
        'database' => config('database.connections.mysql.database'),
        'username' => config('database.connections.mysql.username'),
        'password' => config('database.connections.mysql.password'),
    ]
])->execute();

PHPJasper::process($input, $output, [
    'format' => 'pdf',
    'db_connection' => [
        'driver' => 'postgresql',
        'host' => 'localhost:5432',
        'database' => 'mydb',
        'username' => 'postgres',
        'password' => 'secret',
    ]
])->execute();

PHPJasper::process($input, $output, [
    'format' => 'pdf',
    'params' => [
        'company_name' => 'Acme Corp',
        'report_date' => date('Y-m-d'),
        'user_id' => auth()->id(),
        'show_details' => true
    ]
])->execute();

return [
    // Path to Java executable (defaults to 'java' command)
    'java_path' => env('PHPJASPER_JAVA_PATH', null),

    // Path to JasperStarter JAR (auto-detected if null)
    'jar_path' => env('PHPJASPER_JAR_PATH', null),

    // Default database connection
    'db_connection' => [
        'driver' => env('PHPJASPER_DB_DRIVER', 'mysql'),
        'host' => env('PHPJASPER_DB_HOST', 'localhost'),
        'database' => env('PHPJASPER_DB_DATABASE', 'forge'),
        'username' => env('PHPJASPER_DB_USERNAME', 'forge'),
        'password' => env('PHPJASPER_DB_PASSWORD', ''),
    ],

    // Directory paths
    'template_dir' => storage_path('app/reports/templates'),
    'output_dir' => storage_path('app/reports/output'),
    'resource_dir' => storage_path('app/reports/resources'),

    // Default locale
    'locale' => 'en_US',
];

// config/phpjasper.php
'java_path' => '/usr/lib/jvm/java-11-openjdk/bin/java',

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();

$formats = PHPJasper::getSupportedFormats();
// ['pdf', 'csv', 'xls', 'xlsx', 'xml', 'html', 'jrprint']

$isSupported = PHPJasper::isFormatSupported('pdf'); // true

try {
    PHPJasper::process($input, $output, $options)->execute();
} catch (\Exception $e) {
    // Handle error
    Log::error('Report generation failed: ' . $e->getMessage());
    return back()->with('error', 'Failed to generate report');
}

$command = PHPJasper::process($input, $output, $options)
    ->execute(true); // Returns command string

echo $command;

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();
    }
}
bash
php artisan vendor:publish --provider="Stesa\PHPJasper\PHPJasperServiceProvider" --tag="config"
bash
php artisan vendor:publish --provider="Stesa\PHPJasper\PHPJasperServiceProvider" --tag="examples"