多个replacevariablebyhtml不工作-PHPDocX

Multiple replacevariablebyhtml not working - PHPDocX

本文关键字:-PHPDocX 工作 replacevariablebyhtml 多个      更新时间:2023-09-26

我正在尝试使用replaceVariableByHTML函数替换word中的多个文件变量,还使用replaceVariableByText替换文本。如果我只使用replaceVariableByText,一切都可以,但添加html替换会导致问题——doc不会生成。我试图用HTML代码只替换变量,但我只能替换一个变量。我做错什么了吗?这是我的代码:

//$htmlData and $data are array('name' => 'value')
$docx = new 'CreateDocxFromTemplate($templateDir);
$docx->setTemplateSymbol('‡');
$docx->replaceVariableByText($data, array());
foreach($htmlData as $key => $value)
    $docx->replaceVariableByHTML($key, 'block', $value);
$filename = uniqid();
$uniqName = sys_get_temp_dir() . DIRECTORY_SEPARATOR . $filename.'';
$docx->createDocx($uniqName);
header('Content-Type: application/vnd.openxmlformats-officedocument.' .
  'wordprocessingml.document');
header('Content-Disposition: attachment; filename="' . $templateSymbol .
  '.' . $this->documentExtension . '"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($uniqName . '.' . $this->documentExtension));
readfile($uniqName . '.' . $this->documentExtension);

我认为您的陈述可能不完整。

根据文件,它应该形成为:

public replaceTemplateVariableByHTML (string $var, string $type, string $html [, array $options]) 

根据你想要实现的目标,你可以试试这个:

$docx->replaceVariableByHTML($key, 'block', $value, array('isFile' => false, 'parseDivsAsPs' => true, 'downloadImages' => true)););

您可以相应地更改isFile、parseDivsAsPs和downloadImages属性,但应该传递isFile属性。

以下是完整信息:http://www.phpdocx.com/api-documentation/templates/replace-variable-html-Word-document