|
| 1 | +# JasperReports for PHP |
| 2 | + |
| 3 | +Package to generate reports with [JasperReports 6](http://community.jaspersoft.com/project/jasperreports-library) library through [JasperStarter v3](http://jasperstarter.sourceforge.net/) command-line tool. |
| 4 | + |
| 5 | +##Install |
| 6 | + |
| 7 | +Install [Composer](http://getcomposer.org) if you don't have it. |
| 8 | +``` |
| 9 | +composer require lavela/phpjasper |
| 10 | +``` |
| 11 | +Or in your 'composer.json' file add: |
| 12 | + |
| 13 | +```javascript |
| 14 | +{ |
| 15 | + "require": { |
| 16 | + "lavela/phpjasper": "1.0", |
| 17 | + } |
| 18 | +} |
| 19 | +``` |
| 20 | + |
| 21 | +And the just run: |
| 22 | + |
| 23 | + composer install |
| 24 | + |
| 25 | +and thats it. |
| 26 | +``` |
| 27 | +
|
| 28 | +##Introduction |
| 29 | +
|
| 30 | +This package aims to be a solution to compile and process JasperReports (.jrxml & .jasper files). |
| 31 | +
|
| 32 | +###Why? |
| 33 | +
|
| 34 | +Did you ever had to create a good looking Invoice with a lot of fields for your great web app? |
| 35 | +
|
| 36 | +I had to, and the solutions out there were not perfect. Generating *HTML* + *CSS* to make a *PDF*? WTF? That doesn't make any sense! :) |
| 37 | +
|
| 38 | +Then I found **JasperReports** the best open source solution for reporting. |
| 39 | +
|
| 40 | +###What can I do with this? |
| 41 | +
|
| 42 | +Well, everything. JasperReports is a powerful tool for **reporting** and **BI**. |
| 43 | +
|
| 44 | +**From their website:** |
| 45 | +
|
| 46 | +> 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. |
| 47 | +
|
| 48 | +I recommend using [Jaspersoft Studio](http://community.jaspersoft.com/project/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. |
| 49 | +
|
| 50 | +*Some examples of what you can do:* |
| 51 | +
|
| 52 | +* Invoices |
| 53 | +* Reports |
| 54 | +* Listings |
| 55 | +
|
| 56 | +##Examples |
| 57 | +
|
| 58 | +###The *Hello World* example. |
| 59 | +
|
| 60 | +Go to the examples directory in the root of the repository (`vendor/lavela/phpjasper/examples`). |
| 61 | +Open the `hello_world.jrxml` file with iReport or with your favorite text editor and take a look at the source code. |
| 62 | +
|
| 63 | +#### Compiling |
| 64 | +
|
| 65 | +First we need to compile our `JRXML` file into a `JASPER` binary file. We just have to do this one time. |
| 66 | +
|
| 67 | +**Note:** You don't need to do this step if you are using *Jaspersoft Studio*. You can compile directly within the program. |
| 68 | +
|
| 69 | +```php |
| 70 | +
|
| 71 | +require __DIR__ . '/vendor/autoload.php'; |
| 72 | +
|
| 73 | +use JasperPHP\JasperPHP; |
| 74 | +
|
| 75 | +$input = __DIR__ . '/vendor/lavela/phpjasper/examples/hello_world.jrxml'; |
| 76 | +
|
| 77 | +$jasper = new JasperPHP; |
| 78 | +$jasper->compile($input)->execute(); |
| 79 | +``` |
| 80 | + |
| 81 | +This commando will compile the `hello_world.jrxml` source file to a `hello_world.jasper` file. |
| 82 | + |
| 83 | +**Note:** If you are using Laravel 4 run `php artisan tinker` and copy & paste the command above. |
| 84 | + |
| 85 | +####Processing |
| 86 | + |
| 87 | +Now lets process the report that we compile before: |
| 88 | + |
| 89 | +```php |
| 90 | + |
| 91 | +require __DIR__ . '/vendor/autoload.php'; |
| 92 | + |
| 93 | +use JasperPHP\JasperPHP; |
| 94 | + |
| 95 | +$input = __DIR__ . '/vendor/lavela/phpjasper/examples/hello_world.jasper'; |
| 96 | +$output = __DIR__; |
| 97 | + |
| 98 | +$jasper = new JasperPHP; |
| 99 | + |
| 100 | +$jasper->process( |
| 101 | + $input, |
| 102 | + $output, |
| 103 | + array("pdf", "rtf") |
| 104 | +)->execute(); |
| 105 | +``` |
| 106 | + |
| 107 | +Now check the examples folder! :) Great right? You now have 2 files, `hello_world.pdf` and `hello_world.rtf`. |
| 108 | + |
| 109 | +Check the *API* of the `compile` and `process` functions in the file `src/JasperPHP/JasperPHP.php` file. |
| 110 | + |
| 111 | +####Listing Parameters |
| 112 | + |
| 113 | +Querying the jasper file to examine parameters available in the given jasper report file: |
| 114 | + |
| 115 | +```php |
| 116 | + |
| 117 | +require __DIR__ . '/vendor/autoload.php'; |
| 118 | + |
| 119 | +use JasperPHP\JasperPHP; |
| 120 | + |
| 121 | +$input = __DIR__ . '/vendor/lavela/phpjasper/examples/hello_world_params.jrxml'; |
| 122 | + |
| 123 | +$jasper = new JasperPHP; |
| 124 | +$output = $jasper->list_parameters($input)->execute(); |
| 125 | + |
| 126 | +foreach($output as $parameter_description) |
| 127 | + print $parameter_description . '<pre>'; |
| 128 | +``` |
| 129 | + |
| 130 | +###Advanced example |
| 131 | + |
| 132 | +We can also specify parameters for connecting to database: |
| 133 | + |
| 134 | +```php |
| 135 | + |
| 136 | +require __DIR__ . '/vendor/autoload.php'; |
| 137 | + |
| 138 | +use JasperPHP\JasperPHP; |
| 139 | + |
| 140 | +$input = __DIR__ . '/vendor/lavela/phpjasper/examples/hello_world.jrxml'; |
| 141 | +$output = __DIR__; |
| 142 | + |
| 143 | +$jasper = new JasperPHP; |
| 144 | +$jasper->process( |
| 145 | + $input, |
| 146 | + $output, |
| 147 | + array("pdf", "rtf"), |
| 148 | + array("php_version" => phpversion()), |
| 149 | + array( |
| 150 | + 'driver' => 'postgres', |
| 151 | + 'username' => 'vagrant', |
| 152 | + 'host' => 'localhost', |
| 153 | + 'database' => 'samples', |
| 154 | + 'port' => '5433', |
| 155 | + ) |
| 156 | +)->execute(); |
| 157 | +``` |
| 158 | + |
| 159 | +##Requirements |
| 160 | + |
| 161 | +* Java JDK 1.8 |
| 162 | +* PHP [exec()](http://php.net/manual/function.exec.php) function |
| 163 | +* [optional] [Mysql Connector](http://dev.mysql.com/downloads/connector/j/) (if you want to use database) |
| 164 | +* [optional] [PostgreSQL Connector](https://jdbc.postgresql.org/download.html) (if you want to use database) |
| 165 | +* [optional] [Jaspersoft Studio](http://community.jaspersoft.com/project/jaspersoft-studio) (to draw and compile your reports) |
| 166 | + |
| 167 | + |
| 168 | +##Installation |
| 169 | + |
| 170 | +###Java |
| 171 | + |
| 172 | +Check if you already have Java installed: |
| 173 | + |
| 174 | +``` |
| 175 | +$ java -version |
| 176 | +java version "1.8.0_65" |
| 177 | +Java(TM) SE Runtime Environment (build 1.8.0_65-b17) |
| 178 | +Java HotSpot(TM) Client VM (build 25.65-b01, mixed mode, sharing) |
| 179 | +``` |
| 180 | + |
| 181 | +If you get: |
| 182 | + |
| 183 | + command not found: java |
| 184 | + |
| 185 | +Then install it with: (Ubuntu/Debian) |
| 186 | + |
| 187 | + $ sudo apt-get install default-jdk |
| 188 | + |
| 189 | +Now run the `java -version` again and check if the output is ok. |
| 190 | + |
| 191 | +###Using in Laravel 5.1! |
| 192 | + |
| 193 | +```php |
| 194 | +use JasperPHP\JasperPHP as JasperPHP; |
| 195 | + |
| 196 | +Route::get('/', function () { |
| 197 | + |
| 198 | + $input = __DIR__ . '/vendor/lavela/phpjasper/examples/hello_world.jrxml'; |
| 199 | + $output = __DIR__; |
| 200 | + |
| 201 | + $jasper = new JasperPHP; |
| 202 | + |
| 203 | + // Process a Jasper file to PDF and RTF (you can use directly the .jrxml) |
| 204 | + $jasper->process( |
| 205 | + $input, |
| 206 | + $output, |
| 207 | + array("pdf", "rtf") |
| 208 | + )->execute(); |
| 209 | + |
| 210 | + return view('index'); |
| 211 | +}); |
| 212 | + |
| 213 | +###MySQL |
| 214 | + |
| 215 | +We ship the [MySQL connector](http://dev.mysql.com/downloads/connector/j/) (v5.1.34) in the `/src/JasperStarter/jdbc/` directory. |
| 216 | + |
| 217 | +###PostgreSQL |
| 218 | + |
| 219 | +We ship the [PostgreSQL](https://jdbc.postgresql.org/) (v9.4-1203) in the `/src/JasperStarter/jdbc/` directory. |
| 220 | + |
| 221 | +##Performance |
| 222 | + |
| 223 | +Depends on the complexity, amount of data and the resources of your machine (let me know your use case). |
| 224 | + |
| 225 | +I have a report that generates a *Invoice* with a DB connection, images and multiple pages and it takes about **3/4 seconds** to process. I suggest that you use a worker to generate the reports in the background. |
| 226 | + |
| 227 | +##Thanks |
| 228 | + |
| 229 | +Thanks to [Cenote GmbH](http://www.cenote.de/) for the [JasperStarter](http://jasperstarter.sourceforge.net/) tool. |
| 230 | + |
| 231 | +##Questions? |
| 232 | + |
| 233 | +Drop me a line on Skype [leandro.bittencourt16] |
| 234 | +Drop me a line on Skype [danielrodrigueslima] |
| 235 | + |
| 236 | +##License |
| 237 | + |
| 238 | +MIT |
0 commit comments