Jorgeska Jorgeska - 1 month ago 17
PHP Question

yii2 jasper on basic template

I've followed step by step instructions from JasperReports for yii2.


  1. Installed JDK 1.8 on my Debian 8

  2. Setted up mysql connector's classpath in /etc/profile

  3. Added chrmorandi/yii2-jasper to my composer and updated



I guess php exec() function is enabled because the following test in any view resolves successfuly ...

<? echo exec('whoami'); ?>


chromandi is now under /vendors

java -version says 1.8.0_111

$CLASHPATH points to /usr/share/mysql-connector-java/mysql-connector-java-5.1.40-bin.jar

configuration is so ...

'components' => [
'jasper' => [
'class' => 'chrmorandi\jasper',
'db' => [
'host' => 'localhost',
'port' => 3306,
'driver' => 'mysql',
'dbname' => 'acme',
'username' => 'acme',
'password' => 'acme'
]
],
...
]


I added a controller like this ...

<?php

namespace app\controllers;

use Yii;
use chrmorandi\Jasper;

class EstadisticasController extends \yii\web\Controller {

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

}
}


and tested http://www.acme.es/estadisticas/index that is supposed to be a built-in example.

Now it comes the problem. It complains about

$jasper = Yii::$app->jasper;


line. The output says


ReflectionException Class chrmorandi\jasper does not exist


Anyone facing this issue? There is no much info about jasper on Yii. Any help would be welcome.
Thank you.

Answer

Finally the solution was changing

$jasper = Yii::$app->jasper;

to

$jasper = new \chrmorandi\jasper\Jasper();

Don't know why in the yii2-jasper documentation is setted so if it does not work. Anyway you can make it work following my above compilation.

I hope this helps somebody.