Bulk processing

Bulk processing

Introduction

Bulk Processing is a functionality available since phpdocx 10 in Premium licenses. Its multiple and comprehensive features allow to generate many documents from a template with maximum performance.

Thus, it is possible to open a DOCX as a template and then cache it in memory. Then replace its variables and create from one to several documents with the replaced variables.

Thanks to its internal cache and to being compatible with in-memory documents, replacement and creation of multiple variables and documents is done quickly and efficiently.

Available methods

Bulk Processing comes with the following set of methods to replace the different placeholders:

  • getDocuments: return the generated documents
  • replaceImage: replaces image placeholders
  • replaceList: replaces list placeholders
  • replaceTable: replaces table placeholders
  • replaceText: replaces text placeholders
  • replaceWordFragment: replaces placeholders by WordFragments

These methods can be called on the template individually or else combined in order to obtain the desired result. The chosen template is cached in memory, and so, its original content is reused to generate as many documents as necessary.

Generate and save documents

Working with Bulk Processing is really simple. You just have to create a BulkProcessing object, specifying the chosen template, call the desired replacement methods and save the generated document(s).

After using the placeholder replacement methods, getDocuments returns an array with the final documents. Every position of the returned array is a DOCXStructure object that can be saved as a file or serialized.

Data structure

Use a multidimensional array as structure to create documents and define its values. The first level shows the documents to be created, the sublevels contain the new values.

E.g., the following structure generates three documents with specified values:

Examples

Create and save three documents by replacing text placeholders:

Generate two documents by replacing image and table placeholders:

Create three documents by replacing text placeholders on all documents and replacing placeholders for WordFragments only on the first two documents:

Tips
  • Generated documents can be opened from phpdocx as base templates, either as saved files or as in-memory DOCX.
  • It is not mandatory for the methods to set the same number of documents to be created. Bulk Processing allows to define different number of documents per method, creating or omitting the corresponding ones if necessary.
  • The package includes numerous examples of use for each method.
  • Unlike the CreateDocxFromTemplate methods, Bulk Processing replaces the placeholders of the document, header and footer targets by default. It is possible to define specific targets with the options of each method.