Forum


Replies: 6   Views: 1518
How to selectively add to multimerge
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 cem  · 03-11-2020 - 21:55

Ah, versions 9.5 and 10.  I'm sorry, I failed to mention that we are/were using 9.0.  It definitely does not work in 9.0 (or this specific test didn't).  I updated to 9.5 just to test the sample method 2 and it worked just fine!  Sorry again, i know how important versioning is, i didn't realize i hadn't mentioned it.

If it helps anyone else, using a tweak to the sample code where $document1 is actually updated conditionally and the new object (doc3 or doc2 or whatever) is actually used first and $document1 is the array to add to it does work.

Basically since the order is vital and we don't always have those documents/objects, updating $document1 to be used in front OR as the array (in back position) does seem to work.  I've added the tweaked sample to show the difference using $document3 as the front page which does solve our issue of positioning within the array of documents. And there are other ways to do it i'm sure using the merge() method.  the empty temporary array method also would work and it's what we were doing before but everything got pushed to the back doing that. 

Thank you again for your help and patience!  this seems to have completely solved a problem with positioning the pages we've had since we first signed up for version 7.0!  It's great that it is now so flexible!

require_once 'classes/MultiMerge.php';

// enable in-memory DOCX
CreateDocx::$returnDocxStructure = true;

$merge = new MultiMerge();
  
// first document
$docx_a = new CreateDocx();

$text = 'Text 1';
$paragraphOptions = array('bold' => true, 'font' => 'Arial');
$docx_a->addText($text, $paragraphOptions);

$document1 = $docx_a->createDocx();

// second document
$docx_b = new CreateDocx();

$text = 'Text 2';
$paragraphOptions = array('font' => 'Arial');
$docx_b->addText($text, $paragraphOptions);

$document2 = $docx_b->createDocx();

$document1 = $merge->mergeDocx($document1, array($document2), null, array());

// third document - this is actually the FRONT COVER- First page
$docx_c = new CreateDocx();

$text = 'Text 3';
$docx_c->addText($text);

$document3 = $docx_c->createDocx();

// below is the key part - merge the object you want in front and your built array will work.
$document1 = $merge->mergeDocx($document3, array($document1), null, array());

// disable in-memory DOCX
CreateDocx::$returnDocxStructure = false;

$document1->saveDocx('output.docx');