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

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

作者:互联网

我正在使用PHP Word& htmltodocx_converter创建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

任何人都知道如何将文件即时输出到浏览器?

解决方法:

下面的示例可能有效,但为了在所有浏览器中获得预期的下载行为,Content-Type标头必须适用于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

感谢@jamsandwich:Word 2007的正确标题应该是application / vnd.openxmlformats-officedocument.wordprocessing ml.document.

Reference Microsoft

标签:php,phpword
来源: https://codeday.me/bug/20190623/1271403.html