Skip to content

Instantly share code, notes, and snippets.

@cossou
Created January 11, 2014 11:37
Show Gist options
  • Save cossou/8369900 to your computer and use it in GitHub Desktop.
Save cossou/8369900 to your computer and use it in GitHub Desktop.
In this gist I will cover how you can create a PDF report with JasperReports and Laravel 4.

JasperReports in Laravel 4

##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!

@rodrigoescom
Copy link

I neew}d help, i follow the instructctions, and nothing

@linares82
Copy link

It works, great. Does it work in laravel 5?

@CuellarMarco
Copy link

thanks. But i got some trouble at the very moment in wich i have to compile the code. it seems that it is not recognizeing the service provider route.

@NazarAli
Copy link

syntax error, unexpected ')'

@interludic
Copy link

+1 Laravel 5

@NazarAli
Copy link

how can work in web services ???

@devgood85
Copy link

How to use with JSON ? I would like to use json data from the Laravel model in my Jasper Reports, so that i dont need to make all the queries all over again in mysql. Does any one know how to do this?

@coreser
Copy link

coreser commented Sep 21, 2015

For FreeBSD I had to modify the java command in cossou\jasperphp\src\JasperStarter\bin\jasperstarter
from java to /usr/local/bin/java

@danielleitcs
Copy link

Thanks for the great tips. Would love to add your review of JasperReports to IT Central Station.

Users in our community interested in solutions that generate PDF reports, like JasperReports, also read reviews for Windward. You can see what they have to say about this tool here.

@savankaneriya
Copy link

#need-help
Is there any WAY to embed the Jasper Server Reports to the Laravel App ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment