Replies: 2   Views: 3200
Slow inefficient document merging
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 nicks  · 22-11-2012 - 14:10

Hi All,

I'm working to use PHPDOCX as a way to create Word documents on the fly rather than the previous painful method of using an XSLT template and combining it with XML generated output (truly horrible!), and it is proving very useful, however I've now hit upon a performance issue with this technique.

I've been using MergeDocx and also the AddTemplateVariable methods to build a single word document using 10s to 100s of other smaller template documents (mainly pre-formatted tables). This functionally works fine, though seems very inefficient as I'm having to merge a document, write it to the file system before opening it and adding it as a template.

A snip of code below:

$time_start = microtime(true);
$docx = new DocxUtilities();
$options = array('mergeType' => 1);
$docx->mergeDocx($TMP_FileExt, 'A.docx', $TMP_FileExt, $options);
if ($_GET['Debug'] == 1)
$time_end = microtime(true);
$time = $time_end - $time_start;
echo "Merging Report [" . round($time,4) . "s]<BR/ >";
$time_start = microtime(true);
$docx = new CreateDocx();
$docx->addTemplateVariable('PS_IP', $X);
$docx->addTemplateVariable($PSData, 'table', array('header' => false));
$docx->clearBlocks(); //We clean any remaining block tags
$PSData = array();
if ($_GET['Debug'] == 1)
$time_end = microtime(true);
$time = $time_end - $time_start;
echo "Writing to Report [" . round($time,4) . "s]<BR/ >";

I've added a timer in to see how long the processing is taking, and we are talking about 0.5 of second on average, as a result when combining 100s of small template documents it soon mounts up!

I may well have missing a useful method/trick, though there doesn't seem to be a way of working with documents in memory which I assume should be a lot quicker?

Many thanks in advance.



Posted by nicks  · 11-04-2013 - 12:13

Just to add to my original post.

I've found that generation is speeded up significant by creating the object just once and then make a copy as and when needed.

It is possible to use a Linux RAM disk to when working on files, more info here: