0

我正在尝试使用 replaceVariableByHTML 函数替换 word 中的多桩变量,并且我正在使用 replaceVariableByText 替换文本。如果我只使用 replaceVariableByText 一切正常,但添加 html 替换会导致麻烦 - 文档不会生成。我试图只用 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);
4

1 回答 1

0

我相信你的陈述可能不完整。

根据文档,它应形成为:

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

于 2015-02-05T13:42:58.237 回答