Cookbook

Merge documents

To work with documents with individual contents allows more flexibility than doing it with a big size file. To divide a document in templates of smaller size improves performance for the replacement of placeholders and allows to reuse documents as templates for other documents.

For example, instead of working with a template of twenty pages with a cover, a TOC and the content separated in sections of paragraphs, lists, tables and charts, it is preferable to divide it in several independent DOCX: one for the cover, another for the TOC and one for each section of the content.

Besides this separation that makes possible to obtain a single DOCX with all of the content, the merging functionality covers many use cases such as fusing the same document many times (like school cards or Cvs), obtaining a final report from several ones by different departments or information sources, automating document generation tasks from software outputs and many more.

The MultiMerge class, available for Advanced and Premium licenses, lets you merge documents while preserving its original sections as well as making a unique section with all of the content.

DOCX (Advanced and Premium licenses only)

The mergeDocx method can merge two or more DOCX while keeping all of its contents. The most remarkable option available in this method is 'mergeType', that admits two values: 0, that keeps the sections of the documents, and 1, that deletes the original sections and adds the content in the section of the first document.

For example, let's consider a DOCX for a cover, another DOCX with the TOC and three more DOCX with independent content. You could merge them with value 0 for the first two documents in order to keep their pages intact and merge with value 1 the other three DOCX in order to fuse them and get a continuous flow of content without page breaks.

As an example, the following code merges three DOCX documents and maintains the sections:

PDF (Advanced and Premium licenses only)

Advanced and Premium licenses of phpdocx allows too to fuse documents in PDF format. Unlike the mergeDocx method, you can only fuse PDF documents by keeping its original sizes and sections.

As an example, to merge three PDF documents, execute: