我使用了来自PHPWord网站的示例代码:http://phpword.codeplex.com/documentation,当我尝试用Word打开它时,我得到错误消息"The Office open XML file test.docx cannot be Open,因为内容有问题。“当我点击“详细信息”时,系统会简单地提示“该文件已损坏,无法打开”。它确实可以让我修复和打开它,但这对用户不是很友好……下面是我使用的代码:
// Create a new PHPWord Object
$PHPWord = new PHPWord();
// Every element you want to append to the word document is placed in a section. So you need a section:
$section = $PHPWord->createSection();
// After creating a section, you can append elements:
$section->addText('Hello world!');
// You can directly style your text by giving the addText function an array:
$section->addText('Hello world! I am formatted.', array('name'=>'Tahoma', 'size'=>16, 'bold'=>true));
// If you often need the same style again you can create a user defined style to the word document
// and give the addText function the name of the style>:
$PHPWord->addFontStyle('myOwnStyle', array('name'=>'Verdana', 'size'=>14, 'color'=>'1B2232'));
$section->addText('Hello world! I am formatted by a user defined style', 'myOwnStyle');
// You can also putthe appended element to local object an call functions like this:
$myTextElement = $section->addText('Hello World!');
header('Content-Type: application/vnd.ms-word');
header('Content-Disposition: attachment;filename="test.docx"');
header('Cache-Control: max-age=0');
// At least write the document to webspace:
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
$objWriter->save('php://output');
如您所见,我确实使用了php://output作为保存。任何关于如何摆脱腐败的想法。我确实打开了压缩,并看到在document.xml的末尾似乎有一个空行。也许这是导致它的原因?
谢谢!
发布于 2014-03-26 09:22:44
在输出之前只需添加ob_clean();即可!
ob_clean();
$objWriter->save('php://output');
这将清理您的输出,现在您可以安全地生成docx文件:)
发布于 2011-12-29 12:23:36
您添加的任何文本都不应包含HTML字符。将所有适用的字符转换为HTML实体例如,如果您必须先添加以下"Me & my Code“,请执行此操作:
$Text_to_Add = htmlentities("Me & my Code");
$section->addText($Text_to_Add);
使用Builtin函数保存文件。docx文件是zip文件(您可以在winrar或winzip中打开它们),因此不应使用php://output
$objWriter->save('helloWorld.docx');
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=helloWorld.docx");
header("Content-Type: application/docx");
header("Content-Transfer-Encoding: binary");
这样,文件将被创建,然后由用户下载。
附注: docx文件实际上是XML文件。因此,任何xml保留字符都会损坏文件。一种解决方法是按如下方式转换文本
function xmlEntities($str)
{
$xml = array('"','&','&','<','>',' ','¡','¢','£','¤','¥','¦','§','¨','©','ª','«','¬','­','®','¯','°','±','²','³','´','µ','¶','·','¸','¹','º','»','¼','½','¾','¿','À','Á','Â','Ã','Ä','Å','Æ','Ç','È','É','Ê','Ë','Ì','Í','Î','Ï','Ð','Ñ','Ò','Ó','Ô','Õ','Ö','×','Ø','Ù','Ú','Û','Ü','Ý','Þ','ß','à','á','â','ã','ä','å','æ','ç','è','é','ê','ë','ì','í','î','ï','ð','ñ','ò','ó','ô','õ','ö','÷','ø','ù','ú','û','ü','ý','þ','ÿ');
$html = array('"','&','&','<','>',' ','¡','¢','£','¤','¥','¦','§','¨','©','ª','«','¬','­','®','¯','°','±','²','³','´','µ','¶','·','¸','¹','º','»','¼','½','¾','¿','À','Á','Â','Ã','Ä','Å','Æ','Ç','È','É','Ê','Ë','Ì','Í','Î','Ï','Ð','Ñ','Ò','Ó','Ô','Õ','Ö','×','Ø','Ù','Ú','Û','Ü','Ý','Þ','ß','à','á','â','ã','ä','å','æ','ç','è','é','ê','ë','ì','í','î','ï','ð','ñ','ò','ó','ô','õ','ö','÷','ø','ù','ú','û','ü','ý','þ','ÿ');
$str = str_replace($html,$xml,$str);
$str = str_ireplace($html,$xml,$str);
return $str;
}
$Text_to_Add = htmlentities("Me & my Code");
$Test_to_Add_XML_Cleaned = xmlEntities($Text_to_Add);
$section->addText($Test_to_Add_XML_Cleaned);
发布于 2016-10-28 08:11:52
我知道这是一个老生常谈的问题,但我也遇到了同样的问题,并且找到了最简单的解决方案。我遇到的问题是我使用Symfony,并且在生成文件时,我必须使用Notepad++编辑文件,以查看内容后面是否有错误消息,说明控制器需要一个Response()。
因此,我最终在->save()
之后添加了exit;
,它工作得很好。
https://stackoverflow.com/questions/8576964
复制相似问题