【问题标题】:phpdocx replace template header variable using wordfragment not workingphpdocx使用wordfragment替换模板头变量不起作用
【发布时间】:2014-09-30 09:03:21
【问题描述】:

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

我已经动态地改变了文字的标题。 所以我想用 dnamically 替换标题变量。 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');

【问题讨论】:

  • 你应该在phpdocx论坛询问
  • 我也完成了。我想快速解决这个问题

标签: php phpdocx


【解决方案1】:

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

require_once '../../../classes/CreateDocxFromTemplate.inc';

【讨论】:

  • 我发现错误在线 API 文档中有一个错误,您可以从不支持类页眉和页脚中检查:-(
【解决方案2】:

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

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

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

【讨论】:

    【解决方案3】:

    目前 phpdocx 不支持通过 wordfragment 改变变量,无论是页眉还是页脚。

    【讨论】:

      【解决方案4】:

      这段代码对我有用

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-07-06
        • 1970-01-01
        • 1970-01-01
        • 2019-06-24
        • 2012-10-15
        • 2020-09-29
        相关资源
        最近更新 更多