Forum


Replies: 6   Views: 1335
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 admin  · 30-10-2020 - 09:34

Hello,

Using a Premium license, mergeDocx allows DOCX files and DOCXStructure objects as inputs, and the first parameter is always a single document (file or DOCXStructure) and the second parameter is an array of the documents to be merge (file or DOCXStructure).

This is the signature:

public function mergeDocx($firstDocument, $documentArray, $finalDocument, $options)

You can generate an array of the DOCX to be merged (Advanced licenses can't use in-memory DOCX, only Premium licenses):

require_once 'classes/MultiMerge.php';

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

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

// third document
$docx_c = new CreateDocx();

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

$document3 = $docx_c->createDocx();

// array of the DOCX to be merged
$array_docx = array();
array_push($array_docx, $document2);
array_push($array_docx, $document3);

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

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

Or, maybe this is is the easiest solution for your case, you can call mergeDocx for each DOCX to be merged:

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
$docx_c = new CreateDocx();

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

$document3 = $docx_c->createDocx();

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

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

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

In both cases you'll get the same output, but maybe the second approach is the easiest one as you can merge DOCX when needed instead of creating a temp array.

Regards.