azhai2023/phpjasperxml

PHP PDF renderer of open source jasper report studio.

v1.0.1 2023-08-22 06:29 UTC

This package is auto-updated.

Last update: 2024-08-31 00:36:01 UTC


README

This is php library read jasper report designed file (.jrxml) and generate pdf file.

The goal of this project is to allow php developer design reasonable good printable pdf easily with concept WYSIWYG. However, since the .jrxml file design for java project, phpjasperxml not able to make it 100% compatible in php environment. Refer compatibility description to know what you can do and what you cannot do.

It completely rewrite since version 1.x, if you use version 1.x before please verify your output carefully since it is more compatible to jasper studio, but may not work perfectly at last version.

Install

Latest phpjasperxml require php 7.4, and php extension like php-curl, php-intl, php-simplexml.

composer require simitgroup/phpjasperxml

How to use

<?php
require __DIR__."/vendor/autoload.php";

use simitsdk\phpjasperxml\PHPJasperXML;
$filename = __DIR__.'/sample.jrxml';

$data=[ ['user_id'=>0, 'fullname' => 'name1','email'=>'email1@a.com','gender'=>'M' ], 
        ['user_id'=>1, 'fullname' => 'name2','email'=>'email2@a.com','gender'=>'F' ], 
        ['user_id'=>2, 'fullname' => 'name3','email'=>'email3@a.com','gender'=>'M' ], ];

$config = ['driver'=>'array','data'=>$data];

$report = new PHPJasperXML();
$report->load_xml_file($filename)    
    ->setDataSource($config)
    ->export('Pdf'); 

Refer https://github.com/SIMITGROUP/phpjasperxml/blob/master/examples/databasesample.php if you want to use database driver instead of prepare array.

Samples

Refer sample: https://github.com/SIMITGROUP/phpjasperxml/wiki/Sample-output

Compatibility:

Generally, phpjasperxml provide below compatiblity result of:

Bands

Band support both print order:

  • vertical
  • horizontal

❗ According try & error, there is some band like page header, column footer, page footer not allow grow according textField "stretchHeight". To make life easier phpjasperxml rules:

  1. only detail band will grow
  2. position type (default "Fix relative to top")
  3. stretch type (default "use Not stretch")

TextField and StaticText

TextField and Static Text is most important element in report. Below is the compatibility detail.

Line

Rectangle

Ellipse

Outputs

PHPJasperxml going to output report into several format.

Expressions

jrxml use a lot of expression which is defined as java(groovy) syntax. It not fit into php environment perfectly. Sometimes the report look nice in jasperstudio, but not exactly same in php. It is important to know how PHPJasperxml evaluate the expression, and the flow. Below is the flow:

  1. phpjasperxml extract expression string from specific element
  2. analyse expression using preg_match, and replace desire value into $F{},$V{},$P{}.
  3. If value data type is text/string kinds (Such as java.lang.String), it will apply quote/escape the string
  4. if quote exists, it will replace '+' become '.', cause php combine string using '.'
  5. then use eval() to evaluate it, get the final value. (Since eval() is not secure, you shall not allow untrusted developer define expression).

Expression used at many places, included present the value, set hyperlink, set image location, show/hide specific element or band. It is To make report present as expected, you shall define expression according below rules:

  1. Use more php style syntax: $F{fieldname} == "COMPAREME", instead of $F{fieldname}.equal("COMPAREME")
  2. If you perform some operation/comparison with expression, make sure you double check, compare result from jasperstudio and generated pdf from phpjasperxml.
  3. There is plenty of effort to make expression accurate, but I still recommend you perform calculation within sql, php level. Example: use sql calculate is more guarantee : SELECT a+b+c as result1 from mytable (assume a=1,b=2,c=3, then result1=6) then $F{a}+$F{b}+$F{c} // the result1 most probably = 6, but also possible become 123 (concate 3 string)

Variables

Variable is important, but very language dependent. Below is unsupported features:

  • Increment Type

Calculation Function

Reset Types

Sort Fields

SortField support fields ASC and DESC. Variables/Function is not support

Scriptlet

Scriptlet is a method to allow report fetch specific value from existing functions. To compatible with jasperstudio as much as possible, we use expression method to define php code in Scriptlet description so in jasperstudio not complain. Then in phpjasperxml we will execute and put the value into scriptlet parameter. Refer script from jasperreport to know more.

How to use:

  1. Create scriptlet: "replace_as_alias"
  2. Define description in scriptlet: str_replace("@",'alias',$F{email})
  3. textField define value from scriptlet's parameter "$P{replace_as_alias_SCRIPTLET}"

Refer https://github.com/SIMITGROUP/phpjasperxml/blob/master/examples/groups.jrxml

❌ Styles

Style template is ignore, and not effect element at the moment.

Supported Datasource:

samples

  1. Postgresql
  2. Mysql
  3. PDO (the rest of database)
  4. Array (prepare associate array outside of lib)