Forum


Replies: 9   Views: 1568
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 - 17:04

Hello,

The documentation (https://www.phpdocx.com/documentation/cookbook/) details how to integrate phpdocx with some frameworks and CMS: Symfony, CakePHP, Drupal, Laravel, Yii and others to use the library. We move your request to the website team to add new documentation about working with the stream mode with specific frameworks/CMS.

About what output do I need to have from PhpDocx (DocX, Docx Structure?) in order to stream the result, it's done calling:

CreateDocx::$streamMode = true;

as it's detailed on the API documentation page: https://www.phpdocx.com/api-documentation/performance/zip-stream-docx-with-PHP. and the code from a previous reply details:

<?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());

You can't return (or echo) the mergeDocx output as a DOCX stream doing:

$output = $merge->mergeDocx($queue[0], array(), 'example.docx', array());
return $output;

because it returns a DOCXStructure object, that is created using an internal phpdocx class (DOCXStructure). A DOCXStructure object can use the saveDocx method to save/stream the content:

// CreateDocx::$streamMode = true can also be used to stream the content instead of save the document using a DOCXStructure object

$docxStructure->saveDocx('document');

The stream mode and in-memory DOCX features included in phpdocx and how to use them are explained on the following documentation pages:

https://www.phpdocx.com/documentation/cookbook/improve-phpdocx-performance

https://www.phpdocx.com/documentation/cookbook/in-memory-docx-documents

CreateDocx::$streamMode = true generates a stream, so if you need to get it to be used with other workflow (such as returning the stream as a Response), you may need to capture it to stream the content again. This can be done using PHP stream functions, for example stream_copy_to_stream (https://www.php.net/manual/en/function.stream-copy-to-stream.php), but this is not part of the workflow of phpdocx, that just generates the stream, but from external code that may vary based on the framework/CMS/custom code. On https://symfonycasts.com/screencast/symfony-uploads/file-streaming you can find a guide about using streamings (the code uses stream_copy_to_stream to illustrate the sample) with Symfony 5 that may be helpful for your case.

Regards.