Forum


Replies: 1   Views: 3572
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.

Posted by admin  · 26-05-2016 - 17:23

Hello,

Please send your template to contact[at]phpdocx.com and we'll check it. We have done some tests and everything is working fine, maybe there's some problem with the template that could be easily fixed.

UPDATE:

Thanks for the document. The problem is that paragraphs contain strings with the same name than the placeholders. Please use other placeholder names, such as:
 
$values = [
    'templateId' => 16,
    'templateName' => 'foobar',
    'templateOrder' => 1,
];
 
and it'll work fine.
 
Regards.