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 - 20:06

Hello,

Both samples posted in our previous reply have been fully tested with phpdocx 9.5 and phpdocx 10 and they work correctly. Please run them standalone without changing anything but the path to MultiMerge.php; please note that although our previous codes use in-memory DOCX, they also work with DOCX files.

If after changing the second sample you get Fatal error: Call to a member function saveDocx() on null is because you are not using the correct variable to call saveDocx (a null variable instead of a correct one) or in-memory DOCX (CreateDocx::$returnDocxStructure) is not set as true when doing the merge, so no DOCXStructure object is returned or you are using an old Premium version.

You can use any target path when calling saveDocx in a DOCXStructure, for example:

$document1->saveDocx('../output_2.docx');

About your question, saveDocx works with DOCXStructure objects (https://www.phpdocx.com/documentation/cookbook/in-memory-docx-documents) and you just need to set the path to the output DOCX. CreateDocx and CreateDocxFromTemplate objects use the createDocx method to save the DOCX (that call internally saveDocx of DOCXStructure).

About it seems like you can only do the merge w/o a conditional, of course you can use mergeDocx inside a conditional, but you need to generate an array that contains valid (existing) DOCX files or DOCXStructure objects. For example, from your code:

if ( (!empty($first_doc)) || (!empty($last_doc)) ) {
  $merge = new MultiMerge();
  $merge->mergeDocx($first_doc, $doc_arr, $final_path.$doc_title, array());
}

As you are using an OR (||) condition, if $last_doc is not empty but $first_doc is empty the condition is true, so mergeDocx is called with an empty variable ($first_doc), so the method will return an error because $first_doc isn't a valid ZIP file (or DOCXStructure object).

If you get a ZIP error is because you are trying to merge not existing DOCX files (or not valid DOCXStructure objects).

We recommend you to generate valid conditions checking that all inputs used in mergeDocx are valid DOCX files (Advanced and Premium licenses) or DOCXStructure objects (Premium licenses).

Regards.