phpdocx使用wordfragment替换模板头变量不起作用


phpdocx replace template header variable using wordfragment not working

我正在使用phpdocx生成word文档。

我动态地更改了单词中的标题。所以我想用dnamially替换头变量。phpdocx论坛他们说可以使用WordFragment方法,但不起作用。。http://www.zxcv.phpdocx.com/en/forum/default/topic/734

这是我在头中放入$TESTTT$变量的代码

//path to  the CreateDocx class within your PHPDocX installation
require_once '../../../classes/CreateDocx.inc';
$docx = new CreateDocxFromTemplate('Doc.docx');
//create the Word fragment that is going to replace the variable
$wf = new WordFragment($docx, 'document');
$text='try to add head';
$wf->addText($text);
$docx->replaceVariableByWordFragment(array('TESTTT' => $wf), array('type' => 'block'));

$docx->createDocx('tessst');

从模板创建时,还需要添加:

require_one'../..//classes/CreateDocxFromTemplate.inc';

您真的需要使用Word片段吗?

使用PHPDocx,您可以轻松地替换文本变量和占位符图像,这在大多数情况下应该已经足够了。

如果不是这样,您可以使用导入页眉和页脚方法,或者用添加页眉方法替换整个方法。

当前phpdocx不支持通过页眉或页脚的wordfragment来更改变量。

此代码适用于我的

$docx = new CreateDocxFromTemplate('docTemplate/HeaderTest.docx');
$docx->setTemplateSymbol('#');
$variables = array('VAR_NAME' => "SOME TEXT");
$options = array('target' => 'header');
$docx->replaceVariableByText($variables, $options);