jhon / word_replacer_wrapper
在Word模板中替换文字,并将最终文件转换为所需的pdf等格式
2.4.1
2021-07-13 14:14 UTC
Requires
- ext-zip: *
- ncjoes/office-converter: ^1.0
- phpoffice/phpword: ^0.17.0
README
在.docx文件上替换文字并将它们转换为pdf
使用以下命令安装:
composer require jhon/word_replacer_wrapper
依赖项
You need to install LibreOffice.
用法
您必须包含 vendor/autoload.php
require __DIR__ . '/vendor/autoload.php';
类命名空间
use Jsvptf\WordReplacerWrapper\WordReplacerWrapper;
默认配置
//template to process
$template = 'templateDirectory/test.docx';
定义要转换的数据
//array of ITypeTableChild elements
$tableData = [
[
new Text('a'),
new Text('b'),
],
[
new Text('d'),
new Table($nestedTableData),
]
];
//default configuration, array of IType elements
$data = [
'field1' => new Text('xxxxxxxxxxx'),
'field2' => new Table($tableData),
'field3' => new Image('./images/test.png', 80, 80),
'field4' => new Pagination('Page {PAGE} of {NUMPAGES}.'),
'styleText' => new Text('some with style', [
'size' => 20,
'bold' => true,
'name' => 'Courier New'
])
];
//folder for create files
$temporalDirectory = 'testOne';
$WordReplacerWrapper = new WordReplacerWrapper(
'templateDirectory/test.docx', //docx template
$data, //data to replace
'prueba1', //destination folder
'libreoffice' //libreoffice binary
);
//execute replace and convert to pdf
$routes = $WordReplacerWrapper->replaceData();
//routes
["template"]=>
string(17) "testOne/test.docx"
["document"]=>
string(26) "testOne/document_test.docx"
["pdf"]=>
string(25) "testOne/document_test.pdf"
动态配置
//if you want to know the required variables you can use
$fields = $WordReplacerWrapper->getRequiredFields()
//$fields
array(7) {
[0]=>
string(6) "field1"
[1]=>
string(6) "field3"
[5]=>
string(6) "header"
[6]=>
string(6) "footer"
}
$WordReplacerWrapper->setData(['field1' => new Text('sebastian')]);
$WordReplacerWrapper->setTemporalDir('testTwo');
$routes = $WordReplacerWrapper->replaceData();
//routes
["template"]=>
string(17) "testTwo/test.docx"
["document"]=>
string(26) "testTwo/document_test.docx"
["pdf"]=>
string(25) "testTwo/document_test.pdf"
页眉和页脚
您必须在数据中添加页眉和页脚关键字,实现一个 Jsvptf\WordReplacerWrapper\types\Table 类,并且模板文件必须在页眉和页脚空间有内容,您可以在 example/convertDocx.php 中看到示例。
$data = [
'header' => new Table($tableData),
'footer' => new Table($tableData)
...
];
您可以在 example 目录中找到完整的示例。