simitgroup/phpjasperxml
Composer 安装命令:
composer require simitgroup/phpjasperxml
包简介
PHP PDF renderer of open source jasper report studio.
README 文档
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.
Change Log
Track change log since 2.1.0 here
Install
Latest phpjasperxml require php 7.4-8.2, 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
Try examples
cd phpjasperxml php -S 0.0.0.0:9999 -t .
then browse to http://localhost:9999/examples
Compatibility:
Generally, phpjasperxml provide below compatiblity result of:
Bands
Band support both print order:
- vertical
- horizontal
| Band Name | Status | Description |
|---|---|---|
| title | ✅ | First page only |
| page header | ✅ | |
| column header | ✅ | multiple column supported |
| detail(s) | ✅ | multiple band supported |
| column footer | ✅ | |
| page footer | ✅ | |
| last page footer | ✅ | |
| summary | ✅ | |
| no data | ✅ | |
| groups | ✅ | multiple group supported, in both vertical/horizontal print order |
❗ 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:
- only detail band will grow
- position type (default "Fix relative to top")
- stretch type (default "use Not stretch")
| Element | Status | Description |
|---|---|---|
| textField | ✅ | |
| staticText | ✅ | |
| line | ✅ | Double line not supported |
| rectangle | ✅ | |
| circle | ✅ | |
| image | ✅ | ❗ Some scaleImage is not supported (Clip,RealHeight,RealSize). You can define image expression with base64 string too |
| barcode | ✅ | ❗ some standard is not supported, refer barcode example. |
| break | ✅ | ❗ column break not work nicely, in single page multple column also may error |
| subreport | ✅ | ❗ support basic fixed height sub report (the subreport will simply draw at current location and expand according data without consider band limits) |
| frame | ✅ | |
| chart | ❌ | |
| spiderchart | ❌ | |
| table | ✅ | it rely on additional datasource, at background it run as subreport. |
| list | ❌ | |
| generic | ❌ | |
| custom visualzation | ❌ | |
| map | ❌ |
TextField and StaticText
TextField and Static Text is most important element in report. Below is the compatibility detail.
| Setting | Status | Description |
|---|---|---|
| x | ✅ | |
| y | ✅ | |
| w | ✅ | |
| h | ✅ | |
| Forecolor | ✅ | |
| Backcolor | ✅ | |
| Font | ✅ | ❗ Changing font is configurable, but upstream (tcpdf) not support lot of fonts. Developer shall manually add font into yourproject/vendor/tecnickcom/tcpdf/fonts. Unicode character for Chinese, Japanese, Korean detected will replace as fixed font(So it display the content instead of show '?'. However, you have no way to change the their font ). |
| Transparent | ✅ | |
| Print Repeated Value | ❌ | Default = True |
| Label | ❌ | |
| Key | ❌ | |
| Remove Line When Blank | ❌ | |
| Print First Whole Band | ❌ | |
| Detail Overflow | ❌ | |
| Group Changes | ❌ | |
| Print When Expression | ✅ | |
| Paddings | ✅ | |
| Borders | ✅ | |
| Expressions | ✅ | |
| Text Adjust | ✅ | ScaleFont look differently compare to jasperreport |
| Text Align Horizontal | ✅ | |
| Text Align Vertical | ✅ | not work when Stretch Type = StretchHeight |
| Text Rotation | ✅ | |
| Pattern | ✅ | ❗ only support number |
| Pattern Expression | ✅ | ❗ only support number |
| Markup | ✅ | ❗ No markup, or html only |
| Hyperlink Reference Expression | ✅ | Link Type = Reference, will convert become html cell with hyperlink. ❗ Some format may lose |
Line
| Setting | Status | Description |
|---|---|---|
| x | ✅ | |
| y | ✅ | |
| w | ✅ | |
| h | ✅ | |
| Width | ✅ | |
| Color | ✅ | |
| Style | ✅ | Double line is not supported |
| Print When Expression | ✅ |
Rectangle
| Setting | Status | Description |
|---|---|---|
| x | ✅ | |
| y | ✅ | |
| w | ✅ | |
| h | ✅ | |
| Print When Expression | ✅ | |
| Forecolor | ✅ | Line color override Forecolor |
| Backcolor | ✅ | |
| Transparent | ✅ | |
| Line Color | ✅ | Line color override Forecolor |
| Line Style | ✅ | |
| Line Width | ✅ | |
| Border Radius | ✅ | ❗ radius will cause line style/color/width weird due to bugs in tcpdf. dont use radius if you wish to change line style. |
Ellipse
| Setting | Status | Description |
|---|---|---|
| x | ✅ | |
| y | ✅ | |
| w | ✅ | |
| h | ✅ | |
| Print When Expression | ✅ | |
| Forecolor | ✅ | Line color override Forecolor |
| Backcolor | ✅ | |
| Transparent | ✅ | |
| Line Color | ✅ | Line color override Forecolor |
| Line Style | ✅ | |
| Line Width | ✅ |
Outputs
PHPJasperxml going to output report into several format.
| Output | Status | Description |
|---|---|---|
| ✅ | done, not stable yet | |
| XLSX | ✅ | Only support staticText and TextField |
| HTML | ❌ | coming future |
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:
- phpjasperxml extract expression string from specific element
- analyse expression using preg_match, and replace desire value into $F{},$V{},$P{}.
- If value data type is text/string kinds (Such as java.lang.String), it will apply quote/escape the string
- if quote exists, it will replace '+' become '.', cause php combine string using '.'
- 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:
- Use more php style syntax: $F{fieldname} == "COMPAREME", instead of $F{fieldname}.equal("COMPAREME")
- If you perform some operation/comparison with expression, make sure you double check, compare result from jasperstudio and generated pdf from phpjasperxml.
- 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
| Calculation | Status | Description |
|---|---|---|
| No Calculation Function | ✅ | |
| Sum | ✅ | |
| Average | ✅ | |
| Highest | ✅ | |
| Lowest | ✅ | |
| First | ✅ | |
| Variance | ❌ | coming future |
| Standard Deviation | ❌ | coming future |
| Count | ❌ | coming future |
| Distinct Count | ❌ | coming future |
Reset Types
| Reset Type | Status | Description |
|---|---|---|
| Report | ✅ | |
| Page | ✅ | |
| Column | ✅ | |
| Groupxxx | ✅ | |
| None | ✅ | |
| Master | ❌ | No plan |
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:
- Create scriptlet: "replace_as_alias"
- Define description in scriptlet: str_replace("@",'alias',$F{email})
- 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:
- Postgresql
- Mysql
- PDO (the rest of database)
- Array (prepare associate array outside of lib)
- mongodb (experimental, use mongodb-ql)
Mongodb usage
Mongodb very different with others dbms due to mongodb using hierarchy document structure. PHPJasperXML implement
similar syntax with jaspersoft mongodbql.
Refer mongodbql here
It only support 2 method of queries, find and aggregate:
- Find (findQuery)
- findQuery : { Details },
- findFields : { Details },
- sort : { Details },
- limit : int Details,
- Aggregate (aggregate)
- Unsupported function: mapReduce rowsToProcess batchSize maxTime collation
simitgroup/phpjasperxml 适用场景与选型建议
simitgroup/phpjasperxml 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3.04k 次下载、GitHub Stars 达 49, 最近一次更新时间为 2018 年 11 月 09 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「pdf」 「jasper」 「print format」 「print preview」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 simitgroup/phpjasperxml 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 simitgroup/phpjasperxml 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 simitgroup/phpjasperxml 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
非官方云小票机SDK,支持飞鹅云,芯烨云,易联云,快递100,映美云,中午云,佳博云,优声云,365智能云打印等
JasperStarter is an opensource command line launcher and batch compiler for JasperReports
Jasper reports in PHP
Debug2 is a function developed to debug PHP code of form easy, simple, pretty print, fast and safe.
JasperServer Client and utils.
Provides TCPDF integration for Symfony
统计信息
- 总下载量: 3.04k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 49
- 点击次数: 32
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2018-11-09