Forum


Replies: 1   Views: 2746
Dynamic variables with mergedocx
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 gothamdigitalscience  · 07-06-2017 - 18:51

Is it possible to pass in dynamic variables to the mergeDocx function?  Whenever I do (without adding an actual filename) I get an error.  PHP Fatal error:  Uncaught exception 'Exception' with message 'Error while trying to open the (base) template as a zip file'

I'd like to do something like this, where I control the actual documents (with conditionals that I will add later).

require_once ('../phpdocx-advanced-7.0/classes/MultiMerge.inc');
$intro = new CreateDocxFromTemplate('base.docx');

$test1 = new CreateDocxFromTemplate('test1.docx');
$test2 = new CreateDocxFromTemplate('test2.docx');

$merge = new MultiMerge();
$merge->mergeDocx($intro, array($test1,$test2), 'sample.docx', array('mergeType' => '1'));

 

Posted by admin  · 08-06-2017 - 06:17

Hello,

Since phpdocx 7, Premium licenses allow in-memory merging, but the Advanced license only allow to merge files. Using an Advanced license, you need to create the DOCX files and then merge them.
 
An in-memory merging sample using phpdocx 7 Premium is the following:
<?php

require_once 'classes/MultiMerge.inc';

CreateDocx::$returnDocxStructure = true;

// create the first document to be merged and return it as DOCX structure
$docx_a = new CreateDocx();

$text = 'Lorem ipsum dolor sit amet';

$docx_a->addText($text);

$document1 = $docx_a->createDocx();

// create the second document to be merged and return it as DOCX structure
$docx_b = new CreateDocx();

$text = 'Excepteur sint occaecat cupidatat non proident.';

$docx_b->addText($text);

$document2 = $docx_b->createDocx();

CreateDocx::$returnDocxStructure = false;

$merge = new MultiMerge();
$merge->mergeDocx($document1, array($document2), 'example_merge_docx_2.docx', array());

It can use CreateDocx and CreateDocxFromTemplate.

Regards.