CodeIgniter中的PHPWord类不会生成正确的文字文件中的错误 [英] PHPWord class in CodeIgniter is not generating correct, throwing errors in word file

查看:610
本文介绍了CodeIgniter中的PHPWord类不会生成正确的文字文件中的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我在PHPWord类中遇到困难,可以找到:



$ this-> output-> set_header();



允许您手动设置服务器头,输出类将在输出最终渲染的显示时发送给您。示例:

  $ this-> output-> set_header(HTTP / 1.0 200 OK); 
$ this-> output-> set_header(HTTP / 1.1 200 OK);
$ this-> output-> set_header('Last-Modified:'.gmdate('D,d M Y H:i:s',$ last_update)。'GMT');
$ this-> output-> set_header(Cache-Control:no-store,no-cache,must-revalidate);
$ this-> output-> set_header(Cache-Control:post-check = 0,pre-check = 0);
$ this-> output-> set_header(Pragma:no-cache);

我的假设是您正在尝试使用PHP方式来更新标头,但是您正在使用CI规则用于输出。


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.

解决方案

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:

$this->output->set_header("HTTP/1.0 200 OK");
$this->output->set_header("HTTP/1.1 200 OK");
$this->output->set_header('Last-Modified: '.gmdate('D, d M Y H:i:s', $last_update).' GMT');
$this->output->set_header("Cache-Control: no-store, no-cache, must-revalidate");
$this->output->set_header("Cache-Control: post-check=0, pre-check=0");
$this->output->set_header("Pragma: no-cache"); 

My assumption is you are trying to do update headers the PHP way, but you are playing by CI rules for output.

这篇关于CodeIgniter中的PHPWord类不会生成正确的文字文件中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆