提问者:小点点

PHP Word,将生成的文件作为输出发送到浏览器,而不将其保存在光盘上


我使用PHP Word

// Save File
$h2d_file_uri = tempnam( "", "htd" );
$objWriter = PHPWord_IOFactory::createWriter( $phpword_object, "Word2007" );
$objWriter->save( $filename ); // At this line, I would like to output the file to the browser

任何人都知道如何我可以输出文件到浏览器上的飞行?


共1个答案

匿名用户

下面的示例可能有效,但要在所有浏览器中获得预期的下载行为,Word2007的内容类型标题必须正确。也许您需要一些额外的标题才能正确输出。

你可以试试这个:

$filename = "YOUR_Filename.docx";
header( "Content-Type: application/vnd.openxmlformats-officedocument.wordprocessing‌​ml.document" );// you should look for the real header that you need if it's not Word 2007!!!
header( 'Content-Disposition: attachment; filename='.$filename );

$h2d_file_uri = tempnam( "", "htd" );
$objWriter = PHPWord_IOFactory::createWriter( $phpword_object, "Word2007" );
$objWriter->save( "php://output" );// this would output it like echo, but in combination with header: it will be sent

感谢@jamswidge:word2007的正确标题应该是application/vnd。OpenXMLFormatsOfficeDocument。文字处理‌​ml.document

参考微软

相关问题