首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PHPWord下载错误( office文件无法打开,因为内容有问题)

PHPWord下载错误( office文件无法打开,因为内容有问题)
EN

Stack Overflow用户
提问于 2018-01-19 14:35:55
回答 1查看 973关注 0票数 0

我的PHPWord实现有问题。我正在构建一个功能,允许用户将内容下载到word,并为此使用PHPWord。但是,在下载文档后,我在打开时收到一个错误:

由于内容有问题,无法打开office打开的XML文件。

在接受恢复过程之后,我只能预览word文件的内容,我认为这对用户是不友好的。

这是我的PHP代码。

代码语言:javascript
复制
 <?php
    require_once '../assets/vendor/autoload.php';

        $phpWord = new \PhpOffice\PhpWord\PhpWord();
        $section = $phpWord->addSection();
        \PhpOffice\PhpWord\Shared\Html::addHtml($section, "Content");

        header('Content-Description: File Transfer'); 
        header("Content-Type: application/docx");
        header("Content-Transfer-Encoding: binary");
        header('Content-Disposition: attachment;filename="test.docx"');

        $objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
        $objWriter->save('test.docx');


    ?>
EN

回答 1

Stack Overflow用户

发布于 2018-05-09 11:35:28

这对于PHPWord.Hope来说是一个非常常见的问题,下面的代码片段将解决您的问题,因为它解决了我的问题。

代码语言:javascript
复制
$doc_filename = "Test_Report_". date("d-m-Y").".docx";

// Save file
// Saving the document as OOXML file...
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');

$temp_file_uri = tempnam('', 'anytext');
$objWriter->save($temp_file_uri);

//download code
header('Content-Description: File Transfer');
header("Content-Type: application/docx");//header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.$doc_filename);
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Content-Length: ' . filesize($temp_file_uri));
readfile($temp_file_uri);
unlink($temp_file_uri); // deletes the temporary file
exit;

-Thanks

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48343431

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档