PHP Word,将生成的文件作为输出发送到浏览器而不将其保存在光盘上
PHP Word, send generated file to browser as output without saving it on disc
我正在使用 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
header 必须对 Word2007 正确。也许您需要一些额外的 headers 才能正确输出。
你可以试试这个:
$filename = "YOUR_Filename.docx";
header( "Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.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 的正确 header 应该是 application/vnd.openxmlformats-officedocument.wordprocessingml.document
。
我正在使用 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
header 必须对 Word2007 正确。也许您需要一些额外的 headers 才能正确输出。
你可以试试这个:
$filename = "YOUR_Filename.docx";
header( "Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.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 的正确 header 应该是 application/vnd.openxmlformats-officedocument.wordprocessingml.document
。