【发布时间】:2021-12-02 00:15:03
【问题描述】:
您好,我在尝试创建 docx 文件时总是出错,我想直接下载它。我的浏览器卡在ERR_INVALID_RESPONSE。顺便说一句,我正在使用 CodeIgniter 框架。这是代码。
<?php
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Writer\Word2007;
class Test extends MY_Controller{
public function create()
{
$phpWord = new PhpWord();
$section = $phpWord->addSection();
$section->addText('Hello World!');
$file = 'HelloWorld.docx';
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');
$xmlWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
$xmlWriter->save("php://output");
}
}
此代码仅取自 PhpWord 文档中的示例,但不知何故它不起作用。浏览器上的ERR_INVALID_RESPONSE 没有错误。我错过了什么?
【问题讨论】:
标签: codeigniter-3 phpword