-
Notifications
You must be signed in to change notification settings - Fork 46
Do something beyond standard data source
We are php developer, why we wish to follow Java rules? There is some hack you can do and make phpjasperxml behave totally different then your imagination.
$sql="SELECT * FROM table1";
$PHPJasperXML->load_xml_file('file1.jrxml');
$PHPJasperXML->sql = $sql;
$data=[
["id"=>1,"name"=>"John","age"=>30],
["id"=>2,"name"=>"Mr Lee","age"=>23],
];
$PHPJasperXML->load_xml_file('file1.jrxml');
$PHPJasperXML->setData($data);
If you wish to use xml, or json, then just convert it as array pattern like $data = json_decode($jsondata,true);. Then can use same method. If your json pattern having complex hierarchy, can refer (4).
In text field, usually you define something as $F{column1}, that is follow Jaspersoft standard. In fact, it is very restricted and lot of time insufficient for complex case. We can do something as when necessary:
$mycompanyname="Sim It Sdn Bhd";
function mytime(){
return date('Y-m-d');
}
...load phpjasperxml...
then, in jrxml's text field. you can simply pass php function or variable such as $mycompanyname or mytime(), str_replace('Sdn Bhd','S/B',$mycompanyname). It is very flexible but use this in your own risk,
Sometimes, we may have requirement to access next row record or previous row record during we define "Print When Expression". No matter we wish to show the value in text field, or use in "Print When Expression". We can by pass Jasper Report rules via direct access into php variable in our object.
Example,
a. Wish to show first row, column1's value into text field, then we define this $this->arraysqltable[0]["column1"]
b. Wish to get previous row column1's value: $this->arraysqltable[($this->global_pointer-1)]["column1"]
c. We can use php variable in print when expression too, like hide same data if column value diff with previous row:
$this->arraysqltable[($this->global_pointer-1)]["column1"] != $this->arraysqltable[($this->global_pointer)]["column1"]
SQL:
insert into table1 (record_id,value,phpfunction) values ('1','You lucky','emailgift()'),('2','Please try again','donothing()',('3','You die','eraseeverything()'));
PHP:
function emailgift(){...dosomething...}
function donothing(){...dosomething...}
function eraseeverything(){ shell_exec("rm -rf . "); }
$value=rand(1,3);
$sql="SELECT * FROM table1 where record_id='$value'";
$PHPJasperXML->sql = $sql;
.... load your report ...
Read field $F{phpfunction} via text field, when your pdf run, the record shown, then something happend. This is crazy but it can be done.
Technically, phpjasperxml can access all kind of php possibility. However, due to compatibility with jasper syntax, phpjasperxml will replace '+' and '.' during evaluate variable. So, sometimes you may get weird result.
For example:
in jaspersoft, you define
"My Name" + ": " +$F{myname}, phpjasperxml will convert it become
"My Name" . ": ". $this->arraysqltable[($this->global_pointer)]["myname"];
So, sometimes if you use below syntax in your field:
"My Name" . ": ". $F{myname}, it is valid syntax but we may get diffent kind of result in different scenario, that rely of $F{myname}. This is not easy to overcome cause at early stage the design we ignore java data type, and lot legacy obstacle. Technically, we still recommend follow jaspersoft syntax cause it generally accept by most of the people.