Replies: 6   Views: 3935
Phpdocx consuming cpu when replacing template
Topic closed:
Please note this is an old forum thread. Information in this post may be out-to-date and/or erroneous.
Every phpdocx version includes new features and improvements. Previously unsupported features may have been added to newer releases, or past issues may have been corrected.
We encourage you to download the current phpdocx version and check the Documentation available.

Posted by therasim  · 13-05-2016 - 01:44

I'm using the library to create a 70-page average document and there's a section when I have to create dynamic placeholders in a table to then fill it properly.

The issue is that it takes a considerable amount of time (~30-50 secs) to generate the file and the CPU is being consumed by this process. And the major issue is that sometimes there are multiple people attempting to create a document like this at the same time and that makes it worse.

What can I do to mitigate this issue? My assumption is that the dynamic replacement is what can be causing the delay; if so, what can I do, given that I have to embed HTML on those dynamic placeholders?


Posted by admin  · 13-05-2016 - 06:16


The first tip to improve the performance is to disable the autorepair option.
This is the signature of the constructor of
     * Construct
     * @param string $docxTemplatePath path to the template we wish to use
     * @param array $options
     * The available keys and values are:
     *  'preprocessed' (boolean) if true the variables will not be 'repaired'. Default value is false
     * @access public
    public function __construct($docxTemplatePath, $options = array())


By default phpdocx repairs the placeholders (sometimes Word breaks the XML content adding nonneeded tags in the strings), so to get the best performance you need to disable it:
$docx = new CreateDocxFromTemplate('template.docx', array('preprocessed' => true));

The second tip is to split the DOCX in two or more documents. Instead of working with a single document, please separate it, for example in two or three DOCX, and in your code replace the document placeholders and then use the MultiMerge class to merge them.

We also advise you to check PHP config (APC is highly recommended) and the server specs.


Posted by therasim  · 13-05-2016 - 10:15

Can you please provide me an example on how to use the MultiMerge class to achieve what you are describing? And do I need to check in terms of the server? Currently we are using OpCache but what else do I need to confirm is enabled/disabled there?

Posted by therasim  · 13-05-2016 - 14:24

Thanks for the information

Posted by therasim  · 18-05-2016 - 18:23

What can you advise in regards of multiple users generating these documents at the same time? Time has improved but I'm just curious if there's anything else to be done so the time of their generation is reduced when having 4+ users doing this concurrently.

RAM in our AWS instance is 8GB

Posted by admin  · 19-05-2016 - 06:23


We recommend you to get support tickets and oour team will work on your project: