##Introduction
In this protip I will cover how you can create a PDF report with JasperReports and Laravel 4.
###Create a L4 project
Create a new Laravel 4 project.
$ composer create-project laravel/laravel --prefer-dist
Add JasperPHP to your composer.json
file:
{
"require": {
"cossou/jasperphp": "dev-master"
}
}
and then composer update
.
Add to your app/config/app.php
providers array:
'JasperPHP\JasperPHPServiceProvider',
Now you will have the JasperPHP
alias available.
Go to your app/config/database.php
and edit your database connection (we will use MySQL for this tutorial).
Let's create a new migration.
$ php artisan migrate:make create_users_table --table=users --create
Created Migration: 2013_10_16_151940_create_users_table
Generating optimized class loader
Go to app/database/migrations/XXX_create_users_table.php
file and add the following fields:
$table->increments('id');
$table->string('name');
$table->string('address');
$table->string('country');
$table->timestamps();
Save it and run:
$ php artisan migrate
check your database for the users
table and add some data.
Now we have our Laravel 4 project with JasperPHP package ready.
###Java
JasperReports is Java library. In order to make it work we need to have Java installed and in the PATH
. If you are using Mac OS X it should be already installed.
Check if you already have Java installed:
$ java -version
java version "1.6.0_51"
Java(TM) SE Runtime Environment (build 1.6.0_51-b11-457-11M4509)
Java HotSpot(TM) 64-Bit Server VM (build 20.51-b01-457, mixed mode)
If you get:
command not found: java
In Ubuntu/Debian install it with:
$ sudo apt-get install default-jdk
In Fedora/CentOs/RedHat:
$ sudo yum install java
Now run java -version
again and check if the output is ok.
###Create the report
We need to get iReport Designer from JasperSoft website (it's free). Install it.
Open iReport Designer and create a blank (File > New > Blank). Save it to app/storage/report.jrxml
.
Click on the icon that looks like a DB and add a new report Datasource. Click "new", select "Database JDBC Connection", select your JDBC Driver (use MySQL) fill your connection details (username, password, JDBC url). If you don't have the MySQL driver installed just ask google.
Test the connection and save it.
Now lets create our SQL Query.
SELECT * FROM users
and click "Ok". iReport automatically creates all the fields from the table. Check the "Fields" menu in the "Report Inspector".
Now just drag & drop the id, name, address and country fields to the "Detail 1" band.
Click "Preview"! Et voilá!!! We have a report! Well, almost…
###Back to L4 project.
Check your app/storage
directory and you should have two files: report.jrxml
and report.jasper
.
Open your routes file app/routes.php
and write this simple commands:
Route::get('/', function()
{
JasperPHP::process(
storage_path() . '/report.jasper', //Input file
storage_path() . '/report', //Output file without extension
array("pdf"), //Output format
array("php_version" => phpversion()), //Parameters array
Config::get('database.connections.mysql'), //DB connection array
)->execute();
});
Now you will have the PDF file (report.pdf) in the storage path.
That's it!
I neew}d help, i follow the instructctions, and nothing