JasperReports for PHP
, (*1)
Package to generate reports with JasperReports 6 library through JasperStarter v3 command-line tool., (*2)
Install
composer require chrmorandi/jasper
Introduction
This package aims to be a solution to compile and process JasperReports (.jrxml & .jasper files)., (*3)
Why?
JasperReports is the best open source solution for reporting., (*4)
Generating HTML + CSS to make a PDF. Never think about it, that doesn't make any sense! :p, (*5)
What can I do with this?
Well, everything. JasperReports is a powerful tool for reporting and BI., (*6)
From their website:, (*7)
The JasperReports Library is the world's most popular open source reporting engine. It is entirely written in Java and it is able to use data coming from any kind of data source and produce pixel-perfect documents that can be viewed, printed or exported in a variety of document formats including HTML, PDF, Excel, OpenOffice and Word., (*8)
I recommend using Jaspersoft Studio to build your reports, connect it to your datasource (ex: MySQL), loop thru the results and output it to PDF, XLS, DOC, RTF, ODF, etc., (*9)
What you can do with Jaspersoft:, (*10)
- Graphical design environment
- Pixel-perfect report generation
- Output to PDF, HTML, CSV, XLS, TXT, RTF and more
Examples
The Hello World example.
Go to the examples directory in the root of the repository (vendor/chrmorandi/yii2-jasper/examples
).
Open the hello_world.jrxml
file with iReport or with your favorite text editor and take a look at the source code., (*11)
Requirements
Installation
Java
Check if you already have Java installed:, (*12)
$ java -version
java version "1.8.0_91"
Java(TM) SE Runtime Environment (build 1.8.0_91-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.91-b14, mixed mode)
If you get:, (*13)
command not found: java
Then install it with: (Ubuntu/Debian), (*14)
$ sudo apt-get install default-jdk
Now run the java -version
again and check if the output is ok., (*15)
Composer
Install Composer if you don't have it., (*16)
composer require chrmorandi/yii2-jasper
Or in your composer.json
file add:, (*17)
{
"require": {
"chrmorandi/yii2-jasper": "*"
}
}
And the just run:, (*18)
composer update
and thats it., (*19)
Add the component to the configuration
Use Data Source Name (DSN) for connection information with the database in format like Driver invocation.
See http://php.net/manual/pt_BR/pdo.construct.php, (*20)
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',
]
]
...
],
...
];
Using
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');
}
MySQL
We ship the MySQL connector (v5.1.39) in the /src/JasperStarter/jdbc/
directory., (*21)
PostgreSQL
We ship the PostgreSQL (v9.4-1208) in the /src/JasperStarter/jdbc/
directory., (*22)
Depends on the complexity, amount of data and the resources of your machine., (*23)
Is possible generate reports in the background., (*24)
License
MIT, (*25)