Forum


Replies: 6   Views: 3636
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

Hi

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();

while(....){
$docx->addtemplate(....)
.
.
.
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+
/Peter








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

hi

can anyone please enlight me?

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

Hello,

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

Regards.

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

Hi

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.

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

$docx = new CreateDocx();
$docx->addTemplate('/tmp/template.docx');

for($i=0;$i<$num_pages;$i++){
// $docx->addTemplate('/tmp/template.docx');
print "<pre>";
print_r($docx->getTemplateVariables());
print "</pre>";
foreach ($info[$i] as $key => $value) {
$docx->addTemplateVariable($key,"{$value}");
}
$filename="/tmp/tmp-{$i}";
$docx->createdocx($filename);
$docx->mergeDOCX($filename.".docx",'/tmp/template.docx');
}
[/code]

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

/Peter

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

Hello,



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.



Regards.