CodeIgniter 中的 PHPWord 类未生成正确的内容,在 Word 文件中抛出错误
好吧,我在 PHPWord 类上遇到了困难,可以找到它: http://phpword.codeplex.com
奇怪的是,当我在“示例”文件中使用相同的代码时,它在生成时工作正常。当我不使用标头强制下载时,文件将正常打开。尽管与标头一起使用此代码会导致它在下载时在单词文件中抛出错误,表示文件已损坏且无法打开,但随后它打开得很好。
public function export()
{
// Load PHPWORD Library
$this->load->library('PHPWord');
$sec = $this->phpword->createSection($secStyle);
$header = $sec->createHeader();
$header->addWatermark('images/CC_watermark.png', array('marginTop'=>1015, 'marginLeft'=>-80));
$resultSelected = $this->input->post('cbox');
foreach($resultSelected as $row)
{
$sec->addText($row);
echo $row."<br>";
}
$fileName = "Plan_Generate_".date('Ymd').".docx";
// Force Download
$filePath = $fileName;
$fileName = basename($filePath);
// $fileSize = filesize($filePath);
// Output headers.
header("Cache-Control: private");
header("Content-Type: application/stream");
// header("Content-Length: ".$fileSize);
header("Content-Disposition: attachment; filename=".$fileName);
// Output file.
// readfile ($filePath);
// exit();
// Save File
// $fileName = "files/SomethingNew_".date("Ymd").".docx";
$objWriter = PHPWord_IOFactory::createWriter($this->phpword, 'Word2007');
$objWriter->save('php://output');
}
这是我在尝试生成文件时使用的代码。我遇到的问题是当它尝试强制下载时会抛出错误。感谢您的帮助!如果您不完全理解问题,请提出问题。
更新:
这是我收到的错误的图像。感谢您的快速回复,我实际上将尝试使用 Codeigniters 的方式来完成此操作,Tomm。早晨。
Ok, so I'm having difficulty with the PHPWord class which can be found: http://phpword.codeplex.com
The weird thing about it is when I use this same code in the "example" file it works fine in generating. When I don't force a download using headers the file will open up just fine. Using this code though with the headers is causing it when it downloads to throw errors in the word file saying the file is corrupt and can't be opened, but then it opens up just fine.
public function export()
{
// Load PHPWORD Library
$this->load->library('PHPWord');
$sec = $this->phpword->createSection($secStyle);
$header = $sec->createHeader();
$header->addWatermark('images/CC_watermark.png', array('marginTop'=>1015, 'marginLeft'=>-80));
$resultSelected = $this->input->post('cbox');
foreach($resultSelected as $row)
{
$sec->addText($row);
echo $row."<br>";
}
$fileName = "Plan_Generate_".date('Ymd').".docx";
// Force Download
$filePath = $fileName;
$fileName = basename($filePath);
// $fileSize = filesize($filePath);
// Output headers.
header("Cache-Control: private");
header("Content-Type: application/stream");
// header("Content-Length: ".$fileSize);
header("Content-Disposition: attachment; filename=".$fileName);
// Output file.
// readfile ($filePath);
// exit();
// Save File
// $fileName = "files/SomethingNew_".date("Ymd").".docx";
$objWriter = PHPWord_IOFactory::createWriter($this->phpword, 'Word2007');
$objWriter->save('php://output');
}
This is the code i'm using when trying to generate the file. The trouble I'm having is it's throwing an error when it tries to force download. Thanks for any help! Ask questions if you don't fully understand the question.
Update:
Here's the image of the error's I'm receiving. Thanks for the quick responses and I'm actually going to try the Codeigniters way of doing it Tomm. morning.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该通过 CodeIgniters 函数设置标头,这可能是导致您出现问题的原因:
CI 输出类
$this->output->set_header();
允许您手动设置服务器标头,输出类将在输出最终呈现的显示时为您发送这些标头。示例:
我的假设是您正在尝试以 PHP 方式更新标头,但您正在按照 CI 规则进行输出。
You should be setting headers via CodeIgniters functions, that is what could be causing the issue for you:
CI Output Class
$this->output->set_header();
Permits you to manually set server headers, which the output class will send for you when outputting the final rendered display. Example:
My assumption is you are trying to do update headers the PHP way, but you are playing by CI rules for output.
该问题已通过使用 CI 的
$this->download->force_download()
修复。并通过
ob_start()
、ob_get_contents()
和ob_end_clean()
为需要帮助的人传递数据。This was fixed by using CI's
$this->download->force_download()
.And by passing the data through
ob_start()
,ob_get_contents()
, andob_end_clean()
for people needing help with it.您确定不想
只进行流式传输吗?实际的错误是什么?是单词、浏览器还是 php 引发了错误?
另外,CodeIgniter 有一个下载帮助程序来发送文件...您可能想尝试一下。
http://codeigniter.com/user_guide/helpers/download_helper.html
Are you sure you don't want
instead of just stream? What is the actual error and is it word or the browser or php that is throwing the error?
Also, CodeIgniter has a download helper to send files... You may want to try that.
http://codeigniter.com/user_guide/helpers/download_helper.html