Use template to create sequential pages


I have rewritten the template.php code so that it can create a new page from a single page/document, for when a user has to create a page for each loop. My code is not great, but it seems functional. Please feel free to use it, if it helps in anyway

require_once '../classes/PHPWord/PHPWord.php';
$PHPWord = new PHPWord();
$document = $PHPWord->loadTemplate($template_path);

$j = 0;
foreach( $tenant_arr as $value)
if($j > 0){$document->AddPage();}
$document->setValue('name', $tenant_arr[$j]['name']);
$document->setValue('email', $tenant_arr[$j]['email']);
$document->setValue('phone', $tenant_arr[$j]['phone']);

file attachments


bakarys wrote Sep 5, 2013 at 2:19 PM

I refactored a bit your function AddPage, because each time we loop it was also adding new empty page.
So, here is how the new method looks like :

  • Adds new page in the document
    public function addPage() {
    $this->_documentXMLFINAL .= $this->_documentXML;
    $this->_documentXML = $this->_documentXMLSEQ;
For those who wanna use it, override the old function Addpage by this new one and you will be able to add page properly using one single template.
You will notice that the function is called addPage instead of AddPage, the rename it also everywhere it's called.

Disctiller wrote Dec 17, 2015 at 9:25 PM

Hiiii, thank you very much for this code, i spent a long time searching a solution for "clone part or clone block", this is a better solution... thanksss again :)