Forum


Replies: 3   Views: 4490
Out of memory error on larger files
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 noodletools  · 16-01-2013 - 00:56

We're seeing this error quite a bit when we attempt to convert large HTML documents using PHPDocX 2.7:

PHP Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 83 bytes) in [path_removed]/lib/dompdfParser/include/parserhtml.cls.php on line 783

Code involved:
$docx = new CreateDocx();
$docx->embedHTML($myHTML);

Any workarounds/suggestions?

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

Hello,

I recommend you to use phpdocx 3.0 that has a lot of improvements and to increase memory_limit in your server config.

Regards.

Posted by truth@proposaltech.com  · 18-12-2013 - 22:45

We had the same problem and after many hours of trial and error, I've cut the peak memory usage of phpdocx in half while generating the exact same output.  I generated the same 460kb docx file three times with the following peak memory usages:



1355mb:  3.6 as downloaded from phpdocx.com



1072mb:  Replacing accessing the font properties using the magic __get function with a normal function



631mb:  Also clearing the font properties when no longer needed.



I tried other methods of reducing memory usage, but the other methods only saved ~1% of the memory consumption and required far more changes to the code.  (For example, I swapped out some array usage for splfixedarray usage.)  The changes I made to achieve the memory reduction can be easily applied by following the notes below.  I hope the phpdocx team incorporates this in their next release (so everyone can save on memory consumption and so I don't have to patch my upgrade ;) ).



Add the following to lib/dompdfParser/include/frameparser.cls.php (e.g. near "function set_style")



  // This representation of a DOM element will likely be part of a tree

  // and will continue to be stored after processing this subtree has

  // completed.  The style information consumes a large amount of memory

  // and may be cleared when no longer needed.

  function clear_style() {

    $this->_style = null;

  }





In lib/dompdfParser/include/parserhtml.cls.php



Before each "return" in _render(), add:



                                        // Frame processing has completed.  Free memory.

                                        $frame->clear_style();



Replace



                                try{$sTemp = $properties->$style;}

with

                                try{$sTemp = $properties->nonmagic_get($style);}



In lib/dompdfParser/include/styleparser.cls.php



Replace



  function __get($prop) {



with



  function __get($prop) {

    return $this->nonmagic_get($prop);

  }



  // The magic of __get leaks memory. Calling a normal function avoids the leak.

  function nonmagic_get($prop) {