phpdocx使用wordfragment替换模板头变量无效
我正在使用phpdocx生成word文档 我动态地更改了Word中的标题。 所以我想友好地替换header变量。 他们说可以使用WordFragment方法来完成,但它不起作用。。 这是我的代码,我把$TESTTT$变量放在头文件中phpdocx使用wordfragment替换模板头变量无效,php,phpdocx,Php,Phpdocx,我正在使用phpdocx生成word文档 我动态地更改了Word中的标题。 所以我想友好地替换header变量。 他们说可以使用WordFragment方法来完成,但它不起作用。。 这是我的代码,我把$TESTTT$变量放在头文件中 //path to the CreateDocx class within your PHPDocX installation require_once '../../../classes/CreateDocx.inc'; $docx = new Create
//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');
从模板创建时,还需要添加:
需要_once'../../classes/CreateDocxFromTemplate.inc' 您真的需要使用单词片段吗 使用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);
您应该在phpdocx forumi中询问。我想快速修复此错误。我发现在线API文档中有一个错误,您可以从类中检查,不支持页眉和页脚:-(