One of the most remarkable features of phpdocx is the possibility of merging two or more documents. You can merge them to obtain a single document, in the same way as with PDF files.
Besides these merging options, you can also add DOCX, HTML, MHT and RTF files in new documents and replace them for placeholders in templates.
In order to merge documents, choose the class MultiMerge, available for the Advanced and Premium licenses.
Its use is very simple. First, include this class:
Then, instance it:
Finally, call the mergeDocx method, with the following signature:
The available parameters are:
- $firstDocument: Name of the first document to merge.
- $documentArray: Array of documents you want to add, by order of insertion.
- $finalDocument: Name of the resulting DOCX document.
- $options: Array of additional options. These are the ones available:
- mergeType: 0 or 1, to preserve or discard the sections of each document you want to add.
- numbering, continue or restart: It keeps the page numbering or resets it.
- lineBreaks: To insert as much page breaks as indicated with an integer.
- preserveStyleDefaults, true or false: Forces the styles of the first document to be the main style over the remaining documents.
- enforceSectionPageBreak, true or false: Enforces a page section break between documents
- forceLatestStyles, true as false: Default as false. If true uses the latest document as the base styles
For example, for merging the documents first.docx, second.docx and third.docx, write this line of code:
Merging PDF files is as simple as with DOCX files. First, instance the class:
Then, call mergePdf:
These are the available parameters:
- $documentArray: Array of documents by order of insertion
- $finalDocument: Name of the final PDF file
For example, write this for merging three PDF files:
If you need to add documents that already exist or replace placeholders by files, phpdocx provides two methods.
For adding documents to other new ones, call addExternalFile:
To replace a placeholder in a template for a document, use replaceVariableByExternalFile:
Thanks to these methods you can easily add DOCX, HTML, MHT, and RTF files.
The most important of the available options is matchSource. It is a boolean var that forces Word to respect as much as possible the original styles and structures of the inserted document.
In the same options parameter, indicate with the src key the document to insert:
Here's a simple example for adding a DOCX in a new document:
On the other hand, to replace a placeholder in a template, run this code:
The altChunk tag, used by the addExternalFile and replaceVariableByExternalFile methods, is only compatible with MS Word 2007 and later versions on Windows. Other office suites like LibreOffice, OpenOffice or MS Word for macOS are not compatible with altChunk.
The MultiMerge class doesn't use altChunk tags to merge documents, so it's compatible with all DOCX readers.
Premium licenses support In-memory DOCX merging, that is, merging that doesn't require creating files.
mergeDocx combines documents sequentially. In order to merge in specific positions it is possible to call mergeDocxAt.