CodeIgniter 中的 PHPWord 类未生成正确的内容,在 Word 文件中抛出错误

发布于 2024-12-02 07:59:08 字数 1914 浏览 10 评论 0原文

好吧,我在 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.

Word Errors

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦言归人 2024-12-09 07:59:08

您应该通过 CodeIgniters 函数设置标头,这可能是导致您出现问题的原因:

CI 输出类

$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 规则进行输出。

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.

暗恋未遂 2024-12-09 07:59:08

该问题已通过使用 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(), and ob_end_clean() for people needing help with it.

那一片橙海, 2024-12-09 07:59:08

您确定不想

 header("Content-Type: application/octet-stream");

只进行流式传输吗?实际的错误是什么?是单词、浏览器还是 php 引发了错误?

另外,CodeIgniter 有一个下载帮助程序来发送文件...您可能想尝试一下。
http://codeigniter.com/user_guide/helpers/download_helper.html

Are you sure you don't want

 header("Content-Type: application/octet-stream");

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文