snicholson / docxtemplates
此包最新版本(1.1)没有提供许可证信息。
以类似mailmerge的方式将数据合并到.docx模板中
1.1
2015-08-08 09:42 UTC
Requires
- php: >=5.4.0
Requires (Dev)
- phpunit/phpunit: 4.5.*
This package is auto-updated.
Last update: 2024-09-28 16:10:19 UTC
README
一个用于将Docx文件用作可填模板的PHP库
安装
可以通过composer安装,只需添加以下依赖项
require: "snicholson/docxtemplates": "dev-master"
用法
使用PHPDocxTemplates很简单,要对文档进行合并,您首先需要定义一个规则集合。这是通过以下方式完成的,规则是目标/数据组合。目标是待替换的字符串,而数据是用于替换的值。您可以使用下面的类(在命名空间SNicholson/PHPDocXTemplate)执行简单合并,您需要提供模板的文件路径和输出文件的文件路径,它将为您保存文件。以下是一个使用简单规则进行简单合并的示例
$ruleCollection = new RuleCollection(); $ruleTarget = '#texttoreplace#'; $ruleData = function(){ return 'a test value from a closure'; }; $ruleCollection->addSimpleRule($ruleTarget,$ruleData); $ruleCollection->addSimpleRule('#someMoreTextToReplace#','Some text that needs replacing!'); DocXTemplate::merge('input.docx','output.docx',$ruleCollection);
简单规则
简单规则是一种简单的字符串替换,目标是待替换的字符串,而数据是要替换的字符串或闭包,SimpleRules可以像以下示例那样添加到规则集合中
$ruleCollection = new RuleCollection(); $ruleTarget = '#texttoreplace#'; $ruleData = function(){ return 'a test value from a closure'; }; $ruleCollection->addSimpleRule($ruleTarget,$ruleData); $ruleCollection->addSimpleRule( '#someMoreTextToReplace#', 'Some text that needs replacing!' );
正则表达式规则
正则表达式规则稍微复杂一些,它允许您指定一个正则表达式进行匹配,在闭包中接收匹配项并相应地对其值进行操作
$ruleCollection = new RuleCollection(); $ruleTarget = '/ARegularExpression/'; $ruleData = function($match){ //I can perform logic on the $match value in here return substr($match,0,3); };o $ruleCollection->addRegexpRule($ruleTarget,$ruleData);
将RuleCollections合并到Word文档中
可以使用Static simpleMerge类简单地进行合并,它打算在未来添加更多高级合并类。您必须提供输入文件路径、输出文件路径以及RuleCollection。输出文件路径需要可由PHP写入。
DocXTemplate::merge('input.docx','output.docx',$ruleCollection);
待办事项
未完成
- 能够用图片替换文本
- 能够在文本中插入表格
- 支持基本格式(粗体、斜体、换行)从HTML输入到XML格式