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.

Disable logger

phpdocx integrates the log4php logger to generate and store the information, warning and error messages. If you don't need this logger, e.g., if you have finished the filtering and proof phase of the library, then you can disable it with the static method disableLogger after doing the require of the library:

You can replace it for other compatible logger with PSR3 by executing the static method setLogger:

PHP 7

Since the phpdocx version 5.1 release, all licenses support PHP 7, along with PHP 5.2 and its major versions. In opposition to older versions, PHP 7 offers numerous performance improvements due to the inclusion of a new PHPNG engine.

PHP 7 allows to generate documents more quickly and with no need of making any changes in phpdocx's code.

DOCX loaded in memory

phpdocx 6.5 has suppressed the process of creating a temporal copy in the file system of the base document or template. It only stores in the file system the final DOCX.

This improvement has been possible thanks to a new class that keeps in memory the files, while it lessens the resources the server uses when working with the phpdocx API.

All packages from phpdocx 6.5 on include this feature.

Templates loaded in memory (Premium license 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)

To parse 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 license 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 an 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: