Replies: 6   Views: 3518
Addtemplate to each page?
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 peter  · 30-10-2012 - 11:17


i'm trying to create a document which will have more than one page.

to each page I've want to use the same template

something like this: (pseudocode)

$docx = new createdocx();

last in the while loop:
$docx -> addBreak('page');

I tried different ways and one way was to addtemplate outside the loop.

This doesn't work; it only creates the first page.

So to make sure that the pages was correctly created I redid the loop and created seperated files for each page and they are correct created.

since I was sitting with all these files with one page each I decided to see if you had a merge function which you had. so using the example from your site I tried to merge two docx files but that ends up with two pages where the second page is empty.

This might be due to the fact (reported in another thread) that there are two undeclared vars in merge function.

My question is simply how to make this work, the creating of seperated files for each page and then merge the files simply doesn't appeal me, seems very bad way to solve this especially since it's going to be used a lot each day.

edit: version 2.6 Pro+

Posted by peter  · 11-04-2013 - 12:13


can anyone please enlight me?

Posted by admin  · 11-04-2013 - 12:13


You only can use a template in each DOCX, so you can't use more than one addTemplate method for each DOCX.


Posted by peter  · 11-04-2013 - 12:13


I was told by support (via an email) that there are examples on howto solve this problem and the only reasonable example I found was a merging of two templates so I began to try to find a way and came up with this solution:

My template has variables in both header and document.

$info[0] = array('key1'=>'value1','key2'=>'value2',.....);
$info[1] = array('key1'=>'value1','key2'=>'value2',.....);

$docx = new CreateDocx();

// $docx->addTemplate('/tmp/template.docx');
print "<pre>";
print "</pre>";
foreach ($info[$i] as $key => $value) {

The first printout of templatevariables shows an array containing both 'document' and 'header' variables
while the second and so on the printout only shows 'document' variables

The number of pages in the created document is correct, each page document variables are correctly replaced.
The header variables are only replaced on first page with correct values (the ones in $info[0]), on next page and so on it is still
the values in $info[0].

any suggestions or directing me to the example that handles this kind of case


Posted by peter  · 17-04-2013 - 13:21

any suggestions or directing me to the example that handles this kind of case?

Posted by jorgelj  · 18-04-2013 - 09:19


There's no example that do exactly what you need, so you'll need to think how to do that reading the examples related to templates. An idea is to work with as may documents as you need as templates, change them replacing placeholders and other content, and merging them using mergeDOCX or addDOCX.