starlight93 / html-pdf-excel
轻量级且快速的库,用于轻松将表格格式的模板渲染为xlsx、pdf和html。非常简单!
v1.0.3
2024-07-26 22:34 UTC
Requires
- mossadal/math-parser: ^1.3
- phpoffice/phpspreadsheet: ^1.21
- tecnickcom/tcpdf: ^6.4
README
感谢TCPDF 和 PHPSpreadsheet。此库是一个辅助工具,用于从基于Excel的模板和数组数据生成html、pdf和excel。
安装
composer require starlight93/html-pdf-excel
使用方法
// 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
基本语法
循环语法内部
附加功能
您还可以使用任何数学公式来获取动态值,如图像上方所示,例如摘要等。
动态列(表格)
以下展示了如何生成动态模板。注意图像中的.dynamic字符串
// 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", ] ];
使用上面的数据数组,模板中的动态头部(.dynamic)将被数据数组中的以下动态键替换。因此,最终模板结果将如下所示