Corrupted .docx download using phpdocx

自作多情 提交于 2019-12-02 09:23:55

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;

This is difficult to pinpoint without direct access to the template file, but here are some pointers where templating engines often fail:

  • Try logging all your PHP variables to console: print print_r($this->rec->variable_name, true); and then check to make sure that all your variables are strings, and that none are NULL.
  • Inspect your template file and make sure that the style (ie. font type, font size, etc) is consistent in each template variable. In other words, make sure that there are no variables where half the variable is a different style to the rest of the variable. This particular subtlety is very easy to introduce in a template file, and generally the easiest way to fix it is to simple delete and rewrite each template variable.

Lastly, try removing the 'html' parameter when invoking the addTemplateVariable method and see whether that makes a difference. If you're not actually using HTML, then there's no point in passing the 'html' parameter. Conversely, if you are using HTML, then the corrupted file may be a case of incorrectly structured HTML, causing Microsoft Word to flag the the document as corrupted.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!