progicharles/doc-template
Composer 安装命令:
composer require progicharles/doc-template
包简介
Convert docx and xlsx files templates into PDF.
README 文档
README
Document Designer is a Laravel package allowing to generate documents from a template and filling it with some user data. The current version of PHPWord supports Microsoft Office Open XML (OOXML or OpenXML)
Document Designer is based on PHPWord. LibreOffce need to be installed on the server for PDF export.
Features
- Template processing from .docx files
- Template processing from .xslx files
- Export in .docx, .xlsx & .pdf fomat
- Text variable replacement
- Table row repetition
- Recursive blocks repetition
Installation
Package
composer require uccello/document-designer-core
LibreOffice
You can refer to the oficial documentation from LibreOffice for the installation on your server OS.
Getting Started
You just need to specify a template file, a out file name and the dataset to be used to parse and populate the template:
DocumentIO::process($templateFile, $outFile, $data);
The $data should be an associative array containing all the variables and the corresponding data to be replaced.
Depending the extension (.docx or .pdf) given in the $outFile, the export format will be DOCX or PDF.
Variables
$data = [ 'variableName' => 'Content of the variable', 'otherVariable' => 'Other content', ]
And in the template document, you need to declare the variables with the syntax : ${variableName}
Tables
Table keys in the associative array needs a t: prefix.
$data = [ 't:variableName' => [ [ 'variableName' => 'dolor', 'otherVariable' => 'elit', ], [ 'variableName' => 'amet', 'otherVariable' => 'elit', ], ], ]
In the template document, you need to declare the variables with the syntax : ${variableName}
The first row of the all template document containing a variable with the the same name as the table key will be repeated and the content of the other variables will be replaced.
Images
Image keys in the associative array needs a i: prefix.
$data = [ 'i:imgVariable' => 'path/image.jpg', ]
And in the template document, you need to declare the variables with the syntax : ${imgVariable:[width]:[height]:[ratio]}
Blocks
Block keys in the associative array needs a b: prefix and with capital letters.
$data = [ 'b:BLOCK_NAME' => [ [ 'variableName' => 'dolor', 'otherVariable' => 'elit', ], [ 'variableName' => 'amet', 'otherVariable' => 'elit', ], ] ]
In the template document, you need to declare the blocks start and end with flags :
${BLOCK_NAME}
Block content...
${/BLOCK_NAME}
The blocks behave recursively, witch means they can contain variables, tables and others block.
If a block contain a variable with a name in conflict with another variable in a parent block, the deeper block variables will be replaced in priority.
Exemple:
Php
use Uccello\DocumentDesignerCore\Support\DocumentIO; $templateFile = "path/template.docx"; $outFile = "path/out.pdf"; $data = [ 'var1' => 'lorem', 'var2' => 'ipsum', 'img1' => 'path/image.jpg', 't:tVar1' => [ [ 'tVar1' => 'dolor', 'tVar2' => 'sit', ], [ 'tVar1' => 'amet', 'tVar2' => 'consectetur', ], ], 'b:BLOCK' => [ [ 'var1' => 'adipiscing', 'var2' => 'elit', ], [ 'var1' => 'sed', 'var2' => 'do', ], [ 'var1' => 'eiusmod', 'var2' => 'tempor', ], ] ]; DocumentIO::process($templateFile, $outFile, $data);
Template.docx
This is a simple test template content with two variables ${var1} and ${var2}
With an image:
${img1:300:200}
With a table:
| Table variable 1 | Table variable 2 |
|---|---|
| ${tVar1} | ${tVar2} |
And a block:
${BLOCK}
This is a block content with two variables ${var1} and ${var2}
${/BLOCK}
progicharles/doc-template 适用场景与选型建议
progicharles/doc-template 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 31 次下载、GitHub Stars 达 0, 最近一次更新时间为 2022 年 06 月 08 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 progicharles/doc-template 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 progicharles/doc-template 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 31
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-06-08