Skip to content
This repository has been archived by the owner on Oct 25, 2022. It is now read-only.

Commit

Permalink
Merge pull request #24 from cossou/development
Browse files Browse the repository at this point in the history
New version 2.0
  • Loading branch information
cossou committed Oct 13, 2015
2 parents 04fe89d + 47da133 commit 9d30d0b
Show file tree
Hide file tree
Showing 82 changed files with 4,694 additions and 1,912 deletions.
119 changes: 86 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
# JasperReports for PHP
# JasperReports for PHP

Package to generate reports with [JasperReports](http://community.jaspersoft.com/project/jasperreports-library) library through [JasperStarter](http://jasperstarter.sourceforge.net/) command-line tool.
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.

##Introduction

This package aims to be a solution to compile and process JasperReports (.jrxml & .jasper files).
This package aims to be a solution to compile and process JasperReports (.jrxml & .jasper files).

###Why?

Did you ever had to create a good looking Invoice with a lot of fields for your great web app?
Did you ever had to create a good looking Invoice with a lot of fields for your great web app?

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! :)

Then I found **JasperReports** the best open source solution for reporting.

###What can I do with this?

Well, everything. JasperReports is a powerful tool for **reporting** and **BI**.
Well, everything. JasperReports is a powerful tool for **reporting** and **BI**.

**From their website:**

> 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.
I recommend you to use [iReports designer](http://community.jaspersoft.com/project/ireport-designer) 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.
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.

*Some examples of what you can do:*

Expand All @@ -39,31 +39,48 @@ Open the `hello_world.jrxml` file with iReport or with your favorite text editor

#### Compiling

First we need to compile our `JRXML` file into a `JASPER` binary file. We just have to do this one time.
First we need to compile our `JRXML` file into a `JASPER` binary file. We just have to do this one time.

**Note:** You don't need to do this step if you are using *iReport Designer*. You can compile directly within the program.
**Note:** You don't need to do this step if you are using *Jaspersoft Studio*. You can compile directly within the program.

JasperPHP::compile(base_path() . '/vendor/cossou/jasperphp/examples/hello_world.jrxml')->execute();
```php
JasperPHP::compile(base_path() . '/vendor/cossou/jasperphp/examples/hello_world.jrxml')->execute();
```

This commando will compile the `hello_world.jrxml` source file to a `hello_world.jasper` file.

**Note:** If you are using Laravel 4 run `php artisan tinker` and copy & paste the command above.

####Processing

Now lets process the report that we compile before:
Now lets process the report that we compile before:

JasperPHP::process(
base_path() . '/vendor/cossou/jasperphp/examples/hello_world.jasper',
false,
array("pdf", "rtf"),
array("php_version" => phpversion())
)->execute();
```php
JasperPHP::process(
base_path() . '/vendor/cossou/jasperphp/examples/hello_world.jasper',
false,
array("pdf", "rtf"),
array("php_version" => phpversion())
)->execute();
```

Now check the examples folder! :) Great right? You now have 2 files, `hello_world.pdf` and `hello_world.rtf`.

Check the *API* of the `compile` and `process` functions in the file `src/JasperPHP/JasperPHP.php` file.

####Listing Parameters

Querying the jasper file to examine parameters available in the given jasper report file:

```php
$output = JasperPHP::list_parameters(
base_path() . '/vendor/cossou/jasperphp/examples/hello_world.jasper'
)->execute();

foreach($output as $parameter_description)
echo $parameter_description;
```

###Advanced example

We can also specify parameters for connecting to database:
Expand All @@ -88,24 +105,26 @@ JasperPHP::process(

* Java JDK 1.6
* PHP [exec()](http://php.net/manual/function.exec.php) function
* [optional] [Mysql Connector](http://dev.mysql.com/downloads/connector/j/) (if you want to use database)
* [optional] [iReports](http://community.jaspersoft.com/project/ireport-designer) (to draw and compile your reports)
* [optional] [Mysql Connector](http://dev.mysql.com/downloads/connector/j/) (if you want to use database)
* [optional] [Jaspersoft Studio](http://community.jaspersoft.com/project/jaspersoft-studio) (to draw and compile your reports)


##Installation

###Java

Check if you already have Java installed:
```java
$ 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)

```
$ 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

command not found: java

Then install it with: (Ubuntu/Debian)

Expand All @@ -121,35 +140,70 @@ Now in your `composer.json` file add:
```javascript
{
"require": {
"cossou/jasperphp": "dev-master",
"cossou/jasperphp": "dev-master",
}
}
```

And the just run:

composer update

and thats it.
and thats it.

###Using Laravel 5?

We don't provide a specific package for L5 but you can easily use JasperPHP.

```php
use JasperPHP\JasperPHP as JasperPHP;

Route::get('/', function () {

$jasper = new JasperPHP;

// Compile a JRXML to Jasper
$jasper->compile(__DIR__ . '/../../vendor/cossou/jasperphp/examples/hello_world.jrxml')->execute();

// Process a Jasper file to PDF and RTF (you can use directly the .jrxml)
$jasper->process(
__DIR__ . '/../../vendor/cossou/jasperphp/examples/hello_world.jasper',
false,
array("pdf", "rtf"),
array("php_version" => "xxx")
)->execute();

// List the parameters from a Jasper file.
$array = $jasper->list_parameters(
__DIR__ . '/../../vendor/cossou/jasperphp/examples/hello_world.jasper'
)->execute();

return view('welcome');
});
```

###Using Laravel 4?

Add to your `app/config/app.php` providers array:

```php
'JasperPHP\JasperPHPServiceProvider',
```
```
Now you will have the `JasperPHP` alias available.

###MySQL

If you want to use MySQL in your report datasource, please add the `JAR` to the `/src/JasperStarter/jdbc/` directory. Download it [here](http://dev.mysql.com/downloads/connector/j/).
We ship the [MySQL connector](http://dev.mysql.com/downloads/connector/j/) (v5.1.34) in the `/src/JasperStarter/jdbc/` directory.

###PostgreSQL

We ship the [PostgreSQL](https://jdbc.postgresql.org/) (v9.4-1203) in the `/src/JasperStarter/jdbc/` directory.

##Performance

Depends on the complexity, amount of data and the resources of your machine (let me know your use case).

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 the [Laravel 4 Queue](#) feature.

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.

##Thanks

Expand All @@ -159,7 +213,6 @@ Thanks to [Cenote GmbH](http://www.cenote.de/) for the [JasperStarter](http://ja

Drop me a line on Twitter [@cossou](https://twitter.com/cossou).


##License

MIT
66 changes: 46 additions & 20 deletions src/JasperPHP/JasperPHP.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,21 @@ class JasperPHP
protected $background;
protected $windows = false;
protected $formats = array('pdf', 'rtf', 'xls', 'xlsx', 'docx', 'odt', 'ods', 'pptx', 'csv', 'html', 'xhtml', 'xml', 'jrprint');
protected $resource_directory; // Path to report resource dir or jar file

function __construct()
function __construct($resource_dir = false)
{
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN')
$this->windows = true;

if (!$resource_dir) {
$this->resource_directory = __DIR__ . "/../../../../../";
} else {
if (!file_exists($resource_dir))
throw new \Exception("Invalid resource directory", 1);

$this->resource_directory = $resource_dir;
}
}

public static function __callStatic($method, $parameters)
Expand All @@ -31,8 +41,8 @@ public function compile($input_file, $output_file = false, $background = true, $
throw new \Exception("No input file", 1);

$command = __DIR__ . $this->executable;
$command .= " cp ";

$command .= " compile ";

$command .= $input_file;

Expand All @@ -41,7 +51,7 @@ public function compile($input_file, $output_file = false, $background = true, $

$this->redirect_output = $redirect_output;
$this->background = $background;
$this->the_command = $command;
$this->the_command = escapeshellcmd($command);

return $this;
}
Expand All @@ -53,19 +63,19 @@ public function process($input_file, $output_file = false, $format = array("pdf"

if( is_array($format) )
{
foreach ($format as $key)
foreach ($format as $key)
{
if( !in_array($key, $this->formats))
throw new \Exception("Invalid format!", 1);
}
} else {
if( !in_array($format, $this->formats))
throw new \Exception("Invalid format!", 1);
}
}

$command = __DIR__ . $this->executable;
$command .= " pr ";

$command .= " process ";

$command .= $input_file;

Expand All @@ -78,44 +88,60 @@ public function process($input_file, $output_file = false, $format = array("pdf"
$command .= " -f " . $format;

// Resources dir
$command .= " -r " . __DIR__ . "/../../../../../";
$command .= " -r " . $this->resource_directory;

if( count($parameters) > 0 )
{
$command .= " -P";
foreach ($parameters as $key => $value)
foreach ($parameters as $key => $value)
{
$command .= " " . $key . "=" . $value;
}
}
}

if( count($db_connection) > 0 )
{
$command .= " -t " . $db_connection['driver'];
$command .= " -u " . $db_connection['username'];

if( isset($db_connection['password']) && !empty($db_connection['password']) )
$command .= " -p " . $db_connection['password'];

if( isset($db_connection['host']) && !empty($db_connection['host']) )
$command .= " -H " . $db_connection['host'];

if( isset($db_connection['database']) && !empty($db_connection['database']) )
$command .= " -n " . $db_connection['database'];

if( isset($db_connection['port']) && !empty($db_connection['port']) )
$command .= " --db-port " . $db_connection['port'];

if( isset($db_connection['jdbc_driver']) && !empty($db_connection['jdbc_driver']) )
$command .= " --db-driver " . $db_connection['jdbc_driver'];

if( isset($db_connection['jdbc_url']) && !empty($db_connection['jdbc_url']) )
$command .= " --db-url " . $db_connection['jdbc_url'];
}

$this->redirect_output = $redirect_output;
$this->background = $background;
$this->the_command = $command;
$this->the_command = escapeshellcmd($command);

return $this;
}

public function list_parameters($input_file)
{
if(is_null($input_file) || empty($input_file))
throw new \Exception("No input file", 1);

$command = __DIR__ . $this->executable;

$command .= " list_parameters ";

$command .= $input_file;

$this->the_command = escapeshellcmd($command);

return $this;
}
Expand All @@ -129,7 +155,7 @@ public function execute($run_as_user = false)
{
if( $this->redirect_output && !$this->windows)
$this->the_command .= " > /dev/null 2>&1";

if( $this->background && !$this->windows )
$this->the_command .= " &";

Expand All @@ -141,7 +167,7 @@ public function execute($run_as_user = false)

exec($this->the_command, $output, $return_var);

if($return_var != 0)
if($return_var != 0)
throw new \Exception("There was and error executing the report! Time to check the logs!", 1);

return $output;
Expand Down
Loading

0 comments on commit 9d30d0b

Please sign in to comment.