Cookbook

Improve phpdocx performance

phpdocx shows great performance when working with documents, but it is possible to boost its execution speed through some functionalities.

In this article we are going to review the different features for each license that help to improve phpdocx performance.

phpdocx latest version

Use the latest version of phpdocx. New versions of phpdocx include new features and performance improvements.

PHP 8

Use the latest version of PHP to get the best performance. phpdocx supports all PHP versions from PHP 5.6 to PHP 8.3.

HTML to DOCX

New releases of phpdocx include improvements of the internal algorithm for HTML transformation. With regard to old versions, the latest version of phpdocx uses 60% less memory and it is much faster.

Templates loaded in memory (Premium licenses only)

When loading a template, phpdocx opens and parses its content from the DOCX file, thus consuming server resources as it accesses the file system. With the purpose of improving performance when working with templates, it is possible to save the parsed structure of the DOCX and reuse it later on with the parseDocx method of the DOCXStructure class.

The code to parse a DOCX and save it in a variable is as follows:

Serializing templates (Premium licenses only)

Parsing a DOCX with the previous procedure allows not only to reuse it along a script but also to serialize it to run it anytime. E.g.: you can save it in memory or in a data base with the PHP serialize function (http://php.net/manual/en/function.serialize.php) and recover its initial state with unserialize (http://php.net/manual/en/function.unserialize.php).

The following code serializes a template:

After saving this serialized object you can recover it anytime by unserializing it:

DOCX as a stream (Premium licenses only)

All phpdocx licenses save the resulting DOCX in the file system, and after that it lets you download or open it. Premium license includes the possibility of generating DOCX, either from scratch or from templates, as a stream. In other words, phpdocx obtains the content of the documents straight from its result in the browser or the CLI. With a Premium license you can also redirect its output to other target, like a data base. You can do all of that without the requirement of saving the document in the file system.

You can dinamically set up this stream mode in a script or force it for all documents in the configuration file config/phpdocxconfig.ini.

The following example shows how to enable the phpdocx stream mode:

PDF as a stream (Premium licenses only)

PDF outputs can be generated as stream using the following methods: mergePdf, removePagesPdf and watermarkPdf.

You can dinamically set up this stream mode in a script or force it for all documents in the configuration file config/phpdocxconfig.ini.

The following example shows how to merge PDF documents and get the output as stream:

Compiled mode (Premium licenses only)

Executables for Linux, Windows and macOS to work with DOCX templates and get the maximum performance. Using JSON contents, the compile mode can get styles and placeholder contents, remove placeholders and replace text, list, table and checkbox placeholders.

On Introduction about Compiled mode is available all information about this feature.