Forum


Replies: 6   Views: 2859
Createdocx::$streammode creates a corrupt file
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 sam3d  · 17-12-2017 - 00:41

Using the latest Premium version...

We don't want to save files on the server, so we are setting 

CreateDocx::$streamMode = true;

From your examples, we can run this successfully: ...\examples\Templates\processTemplate\sample_1.php

If we change: 

$docx->createDocx('example_processTemplate_1');

To: 

$docx->createDocxAndDownload('example_processTemplate_1');

It downloads the file and creates a copy on the server. 

But if we change it to : 

CreateDocx::$streamMode = true;
$docx->createDocxAndDownload('example_processTemplate_1');

Then the resulting download shows a corrupt warning when opening in MS Word. 

This issue occurs on a vagrant localhost and on our server.  It occurs in both Chrome and Firefox. 

How can we avoid saving a file on the server and receive an uncorrupted file ? 

 

Posted by admin  · 17-12-2017 - 08:55

Hello,

When you use the streamMode option (to avoid generating DOCX files in the fs) you need to use the createDocx method to generate the stream content. You can't use the createDocxAndDownload with the streamMode option, as the method uses PHP headers to download a DOCX file not a stream.

Please check the included samples in the package and the examples and information available on:

https://www.phpdocx.com/api-documentation/performance/zip-stream-docx-with-PHP

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

Please enable the streamMode option and then use the createDocx method to generate the stream using PHP or Ajax.

Regards.

Posted by sam3d  · 17-12-2017 - 13:17

Thank you.  We can now run ...\examples\Templates\processTemplate\sample_1.php  using $streamMode without a corrupt file error. 

But if we change sample_1 to use a simple form, we still get a corrupt file error when we open the download:

<?php

if ( isset( $_POST['tester'] ) )  {
        
        require_once '../../../classes/CreateDocx.inc';

        $docx = new CreateDocxFromTemplate('../../files/TemplateVariables.docx');

        $variables = $docx->getTemplateVariables();
        $docx->processTemplate($variables);

        CreateDocx::$streamMode = true;

        $docx->createDocx('TEST');

}

?>

<br>-----TEST----<br><br>

<form action="" method="post">

        <input type="hidden" name="tester" value="1" >
        
        <input type="submit" value="Submit">
        
</form>

If we use the same form but remove $streamMode and use $docx->createDocxAndDownload instead, the download is not corrupted, but we have a copy in our fs. 

Posted by admin  · 17-12-2017 - 17:15

Hello,

If it's working standalone but not when adding other PHP code, the most common issue is that the web server or PHP response is adding some string to the DOCX. For example some information from HTML or some response from a framework or CMS. We have run your script on our test servers and all of them are working correctly and the DOCX output is not corrupted.

Some old posts talk about this question:

https://www.phpdocx.com/en/forum/default/topic/347

https://www.phpdocx.com/en/forum/default/topic/1508

https://www.phpdocx.com/en/forum/default/topic/1463

The first two needed to use the ob_clean PHP function and the third topic was solved fixing the response of the framework that as being used wrongly. They talk about the createDocxAndDownload method, but it's the same case.

To find out the source of the problem we recommend you to compare both DOCX (with and without the stream mode) using a HEX viewer. The corrupted one will have more strings with the extra information that is being added.

We also recommend you run the same script using PHP CLI mode to check the output and find if the web server or PHP are adding the extra contents to the DOCX:

$ php script.php > document.docx

About the createDocxAndDownload method, it will always generate the file in the fs. You can remove the DOCX after the document is generated and downloaded setting the removeAfterDownload option to true (https://www.phpdocx.com/api-documentation/layout-and-general/create-and-download-docx-with-PHP).

Regards.

Posted by sam3d  · 17-12-2017 - 18:48

Thank you. We appreciate your patience. 

Adding an exit() call at the end of the simple form handler above solves the corrupted file issue. 

Is it possible you could share a simple ajax example? 

When we use ajax, the document is sent as part of the response ( it appears in the browser console ) and is not downloaded.  And we get a jquery error: 

SyntaxError: Unexpected token P in JSON at position 0

 

Posted by admin  · 18-12-2017 - 08:01

Hello,

We move your request to the dev team to write a new article for the cookbook (https://www.phpdocx.com/documentation/cookbook/).

If you get a generic error such as 'Unexpected token P in JSON at position 0' is because the stream output is not being handled correctly by your JS, for example, it's not being readed as a binary content. Maybe the easiest solution for your project would be using a library such as jquery-binarytransport (https://github.com/henrya/js-jquery/tree/master/BinaryTransport).

Regards.

Posted by sam3d  · 18-12-2017 - 13:05

Thanks for the tip re binary transport. 

A cookbook article would be great addition.