PHP code example of chrmorandi / yii2-jasper

1. Go to this page and download the library: Download chrmorandi/yii2-jasper 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/ */

    

chrmorandi / yii2-jasper example snippets


return [
    ...
    'components'          => [
        'jasper' => [
            'class' => 'chrmorandi\jasper\Jasper::className()',
            'redirect_output' => false, //optional
            'resource_directory' => false, //optional
            'locale' => 'pt_BR', //optional
            'db' => [
                'dsn' =>'psql:host=localhost;port=5432;dbname=myDatabase',
                'username' => 'username',
                'password' => 'password',
                //'jdbcDir' => './jdbc', **Defaults to ./jdbc
                //'jdbcUrl' => 'jdbc:postgresql://"+host+":"+port+"/"+dbname',
            ]
        ]
        ...
    ],
    ...
];

use chrmorandi\jasper\Jasper;

public function actionIndex()
{
    // Set alias for sample directory
    Yii::setAlias('example', '@vendor/chrmorandi/yii2-jasper/examples');

    /* @var $jasper Jasper */
    $jasper = Yii::$app->jasper;

    // Compile a JRXML to Jasper
    $jasper->compile(Yii::getAlias('@example') . '/hello_world.jrxml')->execute();

    // Process a Jasper file to PDF and RTF (you can use directly the .jrxml)
    $jasper->process(
        Yii::getAlias('@example') . '/hello_world.jasper', 
        ['php_version' => 'xxx'],
        ['pdf', 'rtf'],
        false, 
        false 
    )->execute();

    // List the parameters from a Jasper file.
    $array = $jasper->listParameters(Yii::getAlias('@example') . '/hello_world.jasper')->execute();

    // return pdf file
    Yii::$app->response->sendFile(Yii::getAlias('@example') . '/hello_world.pdf');

}