Forum


Replies: 1   Views: 3585
Createdocxfromtemplate() with replacevariablebytext() generate invalid file
Topic closed:
Please note this is an old forum thread. Information in this post may be out-to-date and/or erroneous.
Every phpdocx version includes new features and improvements. Previously unsupported features may have been added to newer releases, or past issues may have been corrected.
We encourage you to download the current phpdocx version and check the Documentation available.

Posted by aflaus  · 26-05-2016 - 14:55

Hello,

I have a problem with replaceVariableByText method of phpdocx enterprise 5.1 (same bug with trial 5.5).

I have a docx template with this content:

Clef    Description Resultat
template.id     $template.id$
template.name       $template.name$
template.order      $template.order$
template.documentName       $template.documentName$
template.createdAt      $template.createdAt$

This is a short version of my code which reproduce the bug:

<?php

require __DIR__ . '/vendor/autoload.php';

function createDocx()
{
    $templatePath = __DIR__.'/templates/template.docx';
    $docx = new \CreateDocxFromTemplate($templatePath);

    $values = [
        'template.id' => 16,
        'template.name' => 'foobar',
        'template.order' => 1,
    ];

    $docx->replaceVariableByText($values);

    $tmp = __DIR__.'/out/'.uniqid();
    $docx->createDocx($tmp);

    echo $tmp;
}

createDocx();

The content of generated docx is corrupted:

Clef    Description Resultat
template.id     16template.namefoobartemplate.order1
template.documentName       $template.documentName$
template.createdAt      $template.createdAt$

The problem seems to come from the parsing of docx file when creating the CreateDocxFromTemplate.

I dump $loadContent in replaceVariableByText() l.796 > variable2Text() l.1453 in debug file:

file_put_contents(__DIR__.'/../../../../out/'.uniqid().'.xml', $loadContent);

The content is already corrupted:

<w:t>template.id</w:t></w:r><w:r w:rsidRPr="00AA567C"><w:rPr><w:rFonts w:asciiTheme="minorHAnsi" w:hAnsiTheme="minorHAnsi" w:cs="Courier New"/><w:lang w:val="fr-FR"/></w:rPr><w:tab/></w:r><w:r w:rsidRPr="00AA567C"><w:rPr><w:rFonts w:asciiTheme="minorHAnsi" w:hAnsiTheme="minorHAnsi" w:cs="Courier New"/><w:lang w:val="fr-FR"/></w:rPr><w:tab/></w:r><w:r w:rsidR="00432CC9"><w:rPr><w:rFonts w:asciiTheme="minorHAnsi" w:hAnsiTheme="minorHAnsi" w:cs="Courier New"/><w:lang w:val="fr-FR"/></w:rPr><w:t>$template.id$template.name$template.name$template.order$template.order$</w:t>

Regards.