phpword通过浏览器下载文件

phpword downloading document through browser

我正在尝试下载此文档,但遇到了一些问题。我可以很好地创建和保存它,但尝试在浏览器上输出时会弹出错误。

The file xxx.docx cannot be opened because there are problems with the contents.

其次是

Word found unreadable content in xxx.docx. Do you want to recover the contents of this document? 

我点击是,然后关注

this file cannot be opened using Microsoft Word.

我点击打开,打开没有问题。如果我浏览到文件的存储位置并打开它,它可以正常打开,所以我相信我的 headers 设置有误?

// Saving the document as OOXML file...
        $objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
        $objWriter->save(Yii::app()->params['exportToDir'].$filename.".docx");
        header("Content-Description: File Transfer");
        header('Content-Disposition: attachment; filename="' . $filename . '.docx"');
        //header("Content-Type: application/docx");
        header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
        header('Content-Transfer-Encoding: binary');
        header("Cache-Control: public");
        header('Expires: 0');
        $objWriter->save("php://output");

以下适用于 Excel。这是一个 json 请求

$filename = "InvoiceSummaryReport_".date("Y-m", strtotime($datestr)) . "_" . date('Y-m-d_H-i-s_T').".xls";
        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="'.$filename.'"');
        header('Cache-Control: max-age=0');
        // If you're serving to IE 9, then the following may be needed
        header('Cache-Control: max-age=1');

        // If you're serving to IE over SSL, then the following may be needed
        //header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
        header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
        header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
        header ('Pragma: public'); // HTTP/1.0

        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        $objWriter->save(Yii::app()->params['exportToDir'].$filename);

        echo "reports/$filename";

尝试复制单词,但不起作用

$filename = "Weekly-MC-Report-" . date('d_F_Y').".docx" ;
        // Saving the document as OOXML file...
        header("Content-Description: File Transfer");
        header('Content-Disposition: attachment; filename="' . $filename . '"');
        header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
        header('Content-Transfer-Encoding: binary');
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header('Expires: 0');

        $objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
        $objWriter->save(Yii::app()->params['exportToDir'].$filename);      
        echo "reports/$filename";

与我的工作定义(下面)唯一不同的是缓存控制部分,即你可以尝试使用这些(至少为了识别导致你头痛的行,即使你试图为您生成的文档)...

    header("Content-Description: File Transfer");
    header('Content-Disposition: attachment; filename="' . $file . '"');
    header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
    header('Content-Transfer-Encoding: binary');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Expires: 0');

    $objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($PHPWord, 'Word2007');
    $objWriter->save('php://output');