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.


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.


phpdocx 9 includes several improvements of the internal algorithm for HTML transformation. With regard to previous versions, phpdocx 9 consumes an average of 60% less memory and it is 15% faster.

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 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 ( and recover its initial state with unserialize (

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.