Forum


Replies: 3   Views: 4509
Downloaded word document says it's corrupted
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@wotbot.com.au  · 03-11-2017 - 08:34

Hello, I use laravel and I used the namespace version of phpdocx. I followed the instructions with the header, but the generated document says it's corrupted. Here's the code.

public function downloadWordDocument($documentId)
{
    $result = $this->initializeDocument($documentId);
    if ($result) {
        $this->_wordClient->render();
        $document    = $result['document'];
        $shaFileName = $result['sha'] . '.docx';
        $filePath    = Storage::disk('temp')->getDriver()->getAdapter()->getPathPrefix() . $shaFileName;
        $title       = $document->template->enable_prefix ? "{$document->code} {$document->title}" : $document->title;

        return response()->download($filePath, $title . ".docx", [
            'Content-Type' => 'application/vnd.openxmlformats-officedocument.' .
                'wordprocessingml.document',
            'Content-Disposition'       => 'attachment; filename="' . $title . '.docx"',
            'Content-Transfer-Encoding' => 'binary',
            'Content-Length'            => filesize($filePath)
        ]);
    }
}

Posted by admin  · 03-11-2017 - 08:44

Hello,

It seems your web server is returning a wrong mime type or it's adding some additional content to the DOCX. phpdocx is fully tested on Windows, Linux and macOS, and the createDocxAndDownload method uses a generic PHP code, but some configurations of web servers may require other code to download the documents.
 
On https://www.phpdocx.com/documentation/cookbook/download-generated-docx you can find an alternative code to download DOCX documents. This code may fix your error, but sometimes the problem comes from a framework that is adding header information (such as the topic https://www.phpdocx.com/en/forum/default/topic/1463) or any other external source (such as a CMS like Drupal).
 
We recommend you to test the library standalone, for example running the included samples using the createDocxAndDownload method, and do a search to get the source of your issue:
 
 
 
 
On this last page a user needed to do an exit; after the DOCX is downloaded to avoid adding extra information from his app.
As you are using a Premium license you can also test the stream mode to download the DOCX:
 
 
Sometimes we compare both DOCX (the document generated in the fs and the downloaded one) using a HEX viewer to find the source of the error. After doing the previous tests, if you still have the same proble, please send to contact[at]phpdocx.com the DOCX generated in the fs (download it using FTP if using a remote server) and the DOCX downloaded using a web browser.
 
Regards.

Posted by admin@wotbot.com.au  · 09-11-2017 - 08:43

Gee, I apologize for the late update, I was able to fix the issue, I followed this solution https://stackoverflow.com/questions/29906595/laravel-5-file-downloads-invalid,
Again, I use laravel. But the method `ob_end_clean` is a generic PHP method, It could help others experiencing the issue even after adding the proper headers. Here's my updated code: 

public function downloadWordDocument($documentId)
{
    try {
        $result = $this->initializeDocument($documentId);
        if ($result) {
            $this->_wordClient->render();
            $document    = $result['document'];
            $shaFileName = $result['sha'] . '.docx';
            $filePath    = Storage::disk('temp')->getDriver()->getAdapter()->getPathPrefix() . $shaFileName;
            $title       = $document->template->enable_prefix ? "{$document->code} {$document->title}" : $document->title;
            ob_end_clean();
            return response()->download($filePath, $title . ".docx", [
                'Content-Type'              => 'application/vnd.openxmlformats-officedocument.' .
                    'wordprocessingml.document',
                'Content-Disposition'       => 'attachment; filename="' . $title . '.docx"',
                'Content-Transfer-Encoding' => 'binary',
                'Content-Length'            => filesize($filePath)
            ]);
        }
    } catch (\InvalidArgumentException $err) {
        return abort(400, $err->getMessage());
    } catch (\Exception $err) {
        return abort(400);
    }
}



Also, How do I filter through all my posts in the forum?.

Posted by admin  · 09-11-2017 - 10:52

Hello,

Thanks for sharing your solution, it may be helpful for other users. Anyway, if a framework, CMS or any PHP code is adding extra information to a download, the most common issue is some wrong configuration or some PHP chunks or logs that are being added wrongly.

About your question, there's no option to filter the forum posts by a user, but you can write your username in the search field (https://www.phpdocx.com/search/).

Regards.