Forum


Replies: 4   Views: 3339
Corruption of multipage template
Topic closed:
Please note this is an old forum thread. Information in this post may be out-to-date and/or erroneous.
Every phpdocx version includes new features and improvements. Previously unsupported features may have been added to newer releases, or past issues may have been corrected.
We encourage you to download the current phpdocx version and check the Documentation available.

Posted by stevea  · 18-05-2012 - 17:59

I am currently working with a template that has 2 pages in it and am using the following to populate the $VARIABLES$ within the template:

[code]
$filename = 'UnitDesignTemplate-'.str_replace(' ', '', $this->rec->title);
$document = new CreateDocx();
$document->addTemplate(dirname(__FILE__).'/templates/unitdesigntemplate.docx');

$document->addTemplateVariable('LAUNCH', $this->rec->launch_time);
$document->addTemplateVariable('PLAN', $this->rec->plan_time);
$document->addTemplateVariable('RTIME', $this->rec->research_time);
$document->addTemplateVariable('DESKCRIT', $this->rec->desk_crits);
$document->addTemplateVariable('SHARE', $this->rec->publish_share);
$document->addTemplateVariable('TTIME', $this->rec->total_time);

$document->addTemplateVariable('TITLE', $this->rec->title);

...

$document->addTemplateVariable('REQUIREMENTS', $this->rec->requirements, 'html');

$document->clearBlocks();
$document->parseStyles();
$document->createDocx($filename);
[/code]

I then push the resulting docx file to the browser and remove the file from the server using:

[code]
header("Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document");
header("Content-Length: ".filesize($filename.'.docx'));
header('Content-Disposition: attachment; filename='.$filename.'.docx');
header('Content-Transfer-Encoding: binary');
readfile($filename.'.docx');
unlink($filename.'.docx');
[/code]

I originally did it this way because the createDocxAndDownload() function was the initial culprit as to why the Word document was coming out corrupted. However, this only worked once and since then has generated files that, when opened in Word (Office Pro Plus 2010, Word version 14.0.6112.5000 64bit), gives the error "The file is corrupt and cannot be opened."

Does anyone have a clue as to what can be going wrong here?

Posted by stevea  · 11-04-2013 - 12:13

After some digging online, I discovered that I needed to do the following immediately prior to the readfile() function:
[code]
ob_clean();
flush();
[/code]

Posted by stevea  · 11-04-2013 - 12:13

Well, that worked all of once and the issue persists.

Posted by stevea  · 11-04-2013 - 12:13

[b]Update:[/b]
Well, I feel dumb. I discovered the problem I was having was related to the output of the docx file. After I unlinked the file, I needed to call [code]exit;[/code], otherwise the top portion of the web interface would get appended to the end of the docx file, resulting in a 'corrupted' file.