2015-02-05 66 views
0

我想用replaceVariableByHTML函數替換word中的多重變量,並且我用replaceVariableByText替換文本。如果我只使用replaceVariableByText,一切都很好,但添加html替換會導致麻煩 - doc不會生成。我試圖用HTML代碼替換變量,但我只能替換一個變量。難道我做錯了什麼?這是我的代碼:多個replacevariablebyhtml不工作 - PHPDocX

//$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); 

回答

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