Question

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

Was it helpful?

Solution

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.

OTHER TIPS

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.

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top