Forum


Replies: 9   Views: 1817
How to use the streammode?
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 admin  · 02-04-2021 - 09:24

Hello,

We have run the following script (that is the same you have posted):

<?php

require_once '../../Classes/Phpdocx/Create/CreateDocx.php';

Phpdocx\Create\CreateDocx::$returnDocxStructure = true;
$queue = array();
$docx = new Phpdocx\Create\CreateDocxFromTemplate('../../examples/files/headings.docx');
$queue[] = $docx->createDocx();
Phpdocx\Create\CreateDocx::$returnDocxStructure = false;
Phpdocx\Create\CreateDocx::$streamMode = true;
$merge = new Phpdocx\Utilities\MultiMerge();
$merge->mergeDocx($queue[0], array(), 'example.docx', array());

using PHP CLI mode and redirecting the stream to a file:

$ php test.php > output.docx

and the DOCX opens with all versions of MS Word without issue.

Please run the same code using PHP CLI mode, so you can check it and find if the problem comes from then integration with Laravel. If you are adding the stream to Laravel, maybe the code you are using is not returning the correct response to use a stream (https://laravel.com/docs/8.x/responses#streamed-downloads) and some extra content is being added to the DOCX (https://www.phpdocx.com/documentation/cookbook/corrupted-docx)?

Also note that mergeDocx and createDocx methods return a DOCXStructure that can be reused. Doing:

return $merge->mergeDocx(...

doesn't return a stream but a DOCXStructure. When you set CreateDocx::$streamMode as true, then the DOCX is generated as a stream, that you needs to handle (not using a return sentence).

Regards.