I
I
Iossarian2019-02-28 16:44:06
PHP
Iossarian, 2019-02-28 16:44:06

Why is PHPWord giving a broken file?

Good evening. I am using the following code to populate a file using PHPWord in yii2 on Ubuntu:

$PhpWord = new \PhpOffice\PhpWord\PhpWord();
                $document = $PhpWord->loadTemplate('/var/www/basic/web/Dogovor.docx'); //шаблон
                $document->setValue('company_name', $_POST['News']['title'] );
                $document->setValue('worker',  $_POST['News']['text']);
            header("Content-Type:application/vnd.ms-office");
            header('Cache-Control: max-age=0');
            header('Content-Disposition: attachment;filename="Dogovor.docx"');
            $document->saveAs('php://output');

When you try to open the file, it breaks down and offers to try to restore it, but this does not end with success.
5c77e5a0e2d82252738633.png
If you save the completed file locally, and not give it to the browser, then there are no problems. What can be wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Iossarian, 2019-03-01
@Iossarian

The solution turned out to be to use the renderPartial() method instead of render().

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question