Replies: 2   Views: 3213
Multi template with html
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 interfacerestauration  · 19-08-2011 - 07:39


There is a bug in the "PRO library" when I want to generate documents one after the other using a template and the method "addTemplateVariable" with the "html" option.
The first file can be open but the others are corrupted and Word send an error message.

However, I corrected the problem in my own version of the library, here it is :
The class "CreateDocx" is, as it should, created again for each new document, but because of his own nature, the singleton "CreateTemplate" is only created once.
The array "$embedFiles" continues to be filed with the new HTML documents, and the DOCX documents generated possess all the old files of the previous documents.
On the other hand, only the relations between the new variables of "html" type and the newly html files created in the folder "word/" of the DOCX are set.
This is where the problem comes.

To resolve it, I don’t use the class "CreateTemplate" as a singleton, but instead I create a new instance for each new DOCX document.
I create a variable "private $_template" in the class "CreateDocx" and in the method "addTemplateVariable" I changed the code :
$this->_template = new CreateTemplate;
'Assign and replace text variable ' . $var . ' in template.'
$this->_template->replaceVariable($var, $value, $settings);

Now, every documents are generated and can be opened.
I hope this will help you to improve the library.