Search code examples
javascriptphpms-wordphpdocx

Multiple replacevariablebyhtml not working - PHPDocX


I'm trying to replace multpile variable in word using replaceVariableByHTML function and also i'm replacing text using replaceVariableByText . If i'm only using replaceVariableByText everything is ok but adding html replacement causes troubles - doc is not generating. I was trying to replace variables only with HTML code but i can replace only one variable. Am i doing something wrong? This is my code:

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

Solution

  • I believe your statement might not be complete.

    According to the documentation, it should be formed as:

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

    Depending on what exactly you're trying to achieve, you can try this:

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

    You can change the isFile, parseDivsAsPs and downloadImages properties accordingly, but the isFile property should be passed.

    Here's the full info: http://www.phpdocx.com/api-documentation/templates/replace-variable-html-Word-document