Forum


Replies: 3   Views: 4179
Embedhtml using a large amount of memory
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 surveygizmo  · 07-08-2012 - 20:20

Hi,
We use phpdocx to automatically generate word documents for our customers based on their reports.

The problem is embedHTML is using up a large amount of memory in php (~500mb) to generate a word document that only ends up being about 80k.

While we are able to create this word document, for anything larger we hit the server's memory limit. I was wondering if there was someway to stream the document out to a file during embedHTML or if there was some way to limit the memory usage.

Here's our code below along with memory usage dumps:
[code] var_dump(6,memory_get_usage());

$this->endSession();//close the document div
$outputCode = '';
$outputCode .= $this->getBody();//get the html
unset($this->documentBuffer);

var_dump(7,memory_get_usage());

$file = $saveInPath.$fileName;
$docx = new CreateDocx();

var_dump(8,memory_get_usage());

$docx->embedHTML($outputCode, array('parseDivsAsPs' => false, 'downloadImages' => true, 'id' => 'bodyContent'));
unset($outputCode);

var_dump(9,memory_get_usage());

$docx->createDocx($file);

var_dump(10,memory_get_usage());[/code]

And here's the output:
int(6)
int(54476040)
int(7)
int(54476136)
int(8)
int(56217040)
int(9)
int(498491816)
int(10)
int(503589144)

You'll notice how the memory jumps up from 56mb to 498mb between #8 and #9.

Here's a link to the test doc file that was outputted:
[url]http://surveygizmolibrary.s3.amazonaws.com/library/6175/testdoc.docx[/url]

Thanks!
-Chad