starlight93/html-pdf-excel
Composer 安装命令:
composer require starlight93/html-pdf-excel
包简介
lightweight and fast library to render sheet-formatted-template to xlsx, pdf, and html easily. Really easy!
README 文档
README
Thanks to TCPDF and PHPSpreadsheet. This library is a helper to generate html, pdf, and excel from excel-based template and array data.
Installation
composer require starlight93/html-pdf-excel
Usage
// your array of data, see: /testing/1data.json $data = [ 'key'=>'value', 'detail' => [ [ 'key_detail1' => 'value1', 'amount' => 2000 ],[ 'key_detail1' => 'value2', 'amount' => 1000 ] ] ]; // example: function to get string from a text file public function getFromFile( $path ){ $file = fopen( $path, "r" ); $dt = fread( $file ,filesize($path) ) ; fclose($file); return $dt; } // your template string, see: /testing/1template.txt $template = getFromFile( "your text file path" ); $renderer = new \Starlight93\HtmlPdfExcel\Renderer; // if you want to try with /testing data dan template just pass true to Renderer Construct // $renderer = new \Starlight93\HtmlPdfExcel\Renderer( true ); // with parameter true will take the data and template from /testing dir, so you can focus on config only // ======================= example rendering PDF start $renderer->renderPDF(data: $data, template: $template, config: [ 'title' =>'testing', 'break' => true, 'left' => 10, 'top' => 10, 'right' => 12, 'orientation' =>'L', 'size' => [210, 297], // can be A4, F4, etc 'fontSize' => 10, 'callback' => function( $pdf ){ // see tcpdf documentation for any available property and function }, 'header_callback' => function( $hd ){ // see tcpdf documentation for any available property and function }, 'footer_callback' => function( $ft ){ // see tcpdf documentation for any available property and function }, ]); // ======================= end // ======================= example rendering XLS start $renderer->renderXls(data: $data, template: $template, config: [ 'title' => 'testing', 'break' => true, // to separate into 'orientation' =>'L', 'size' => 9, // see https://github.com/PHPOffice/PhpSpreadsheet/blob/master/src/PhpSpreadsheet/Worksheet/PageSetup.php 'fontSize' => 10 ]); // ======================= end // ======================= example rendering XLS start $renderer->renderHtml(data: $data, template: $template, config: [ 'title' =>'testing', // 'orientation' =>'L', // 'size' => 'A4', 'fontSize' => 10 ]); // ======================= end
Basic Syntax
Inside Looping Syntax
| Code | Description |
|---|---|
| $dataIndex | key data index |
| $detail.dataIndex | data index di detail |
| ? | space or empty cell |
| ! | grouping column and take the first cell value |
| _number | autonumberer starts from 1 |
| ${looped_array_key}.{single_array_key} | Set value given from key |
Additional Features
You can also use any mathematic formula to get dynamic value as the image above such as summary or etc.
Dynamic Columns (table)
Below shows how to generate dynamic templates. Focus to .dynamic string which is shown in the picture
// your array of data for dynamic columns $data = [ 'key'=>'value', 'details' => [ [ 'key_detail1' => 'value1', 'amount' => 2000, 'another1' => 5000, 'another2' => 1000, ],[ 'key_detail1' => 'value2', 'amount' => 1000, 'amount' => 2000, 'another1' => 2000, 'another2' => 4000 ],[ 'key_detail1' => 'value2', 'amount' => 1000, 'amount' => 2000, 'another1' => 3000, 'another2' => 2000 ] ], // key below is to generate dynamic columns 'dynamic' => [ "Col Dynamic 1"=>"\$details.another1", "Col Dynamic 2"=>"\$details.another2", ] ];
By using data array above, the .dynamic header in the template will be replaced with the following dynamic key in the data array. So the final template result will be like this picture shown below:
starlight93/html-pdf-excel 适用场景与选型建议
starlight93/html-pdf-excel 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 166 次下载、GitHub Stars 达 3, 最近一次更新时间为 2022 年 03 月 17 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 starlight93/html-pdf-excel 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 starlight93/html-pdf-excel 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 166
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 6
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-03-17


