使用phpdocx损坏的.docx下载 [英] Corrupted .docx download using phpdocx

查看:44
本文介绍了使用phpdocx损坏的.docx下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个项目,我们在其中使用phpdocx pro在from模板中生成一个.docx文件.我可以很容易地将数据输入模板,但是在MS Word 2010中下载并打开文件后,程序报告由于内容存在问题而无法打开文件,详细信息为文件已损坏.并且无法打开".Word可以修复文档,但是问题仍然在于,一开始就不应损坏它.

I have a project in which we are using phpdocx pro to generate a .docx file in the from templates. I can get the data in to the template easy enough, however when the file is downloaded and opened in MS Word 2010, the program reports that the file cannot be opened because there are problems with the contents, the details being "The file is corrupt and cannot be opened". Word can repair the document, however the issue still stands that it should not be corrupted in the first place.

这是我生成文档的方式:

This is how I'm generating the document:

function generateUnitDesign(){
  if($this->populateRecords()){
      require_once dirname(__FILE__).'/phpdocx/classes/CreateDocx.inc';
      $filename = 'UnitDesignTemplate-'.str_replace(' ', '', $this->rec->title);
      //Create Document
      $document = new CreateDocx();
      $document->addTemplate(dirname(__FILE__).'/templates/unitdesigntemplate.docx');

      // Fill in text fields
      $document->addTemplateVariable('TITLE', $this->rec->title);
      $document->addTemplateVariable('CHALLENGE', $this->rec->challenge, 'html');
      $document->addTemplateVariable('HOOK', $this->rec->hook, 'html');
      $document->addTemplateVariable('RESEARCH', $this->rec->research, 'html');
      $document->addTemplateVariable('AUDIENCE', $this->rec->audience, 'html');
      $document->addTemplateVariable('SUMMARY', $this->rec->project_brief, 'html');
      $document->addTemplateVariable('RESOURCES', $this->rec->resources, 'html');
      $document->addTemplateVariable('REQUIREMENTS', $this->rec->requirements, 'html');
      $document->addTemplateVariable('SCAFFOLDING', $this->rec->scaffolding, 'html');

      $document->createDocx($filename);
      unset($document);
      header("Content-Type: application/vnd.ms-word");
      header("Content-Length: ".filesize($filename.'.docx'));
      header('Content-Disposition: attachment; filename='.$filename.'.docx');
      header('Content-Transfer-Encoding: binary');
      ob_clean();
      flush();
      readfile($filename.'.docx');
      unlink($filename.'.docx');
  }
}

最初,我试图使用其 createDocxAndDownload()函数来获取文件,但是这样做会在服务器上保留.docx文件的副本,这并不理想.我想念什么吗?有没有在phpdocx方面有更多经验的人来帮忙?

Originally, I was trying to use their createDocxAndDownload() function to get the file, but it would leave a copy of the .docx file on the server, which was not ideal. Am I missing something? Is there someone with more experience with phpdocx to lend a hand?

好吧,我觉得自己是个白痴.将问题缩小到输出文件的代码部分之后,我终于在HEX编辑器中打开了文件,发现问题是在文件成功输出之后,Web前端会将HTML的开头附加到HTML的末尾.docx文件成为损坏的"文件. unlink()之后的这一行立即修复了整个问题:

Well, I feel like an idiot. After narrowing the issue down to the portion of code that outputs the file, I finally opened the file in a HEX editor and discovered the issue was that after the file was output successfully the web frontend would append the start of it's HTML to the end of the docx file making a 'corrupted' file. This one line immediately after the unlink() fixed the whole thing:

exit;

Pekka:如果您想用新信息回答这个问题,我会接受您的回答.

Pekka: If you would like to answer this with the new information, I'll accept your answer.

推荐答案

将问题缩小到输出文件的代码部分之后,我终于在HEX编辑器中打开了文件,发现问题是在文件之后如果成功输出,则Web前端会将其HTML的开头附加到docx文件的末尾,从而生成损坏的"文件. unlink()之后的这一行立即修复了整个问题:

After narrowing the issue down to the portion of code that outputs the file, I finally opened the file in a HEX editor and discovered the issue was that after the file was output successfully the web front end would append the start of it's HTML to the end of the docx file making a 'corrupted' file. This one line immediately after the unlink() fixed the whole thing:

exit;

这篇关于使用phpdocx损坏的.docx下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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