php如何生成文件到客户端,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.

标签:php,phpword

来源: https://codeday.me/bug/20190623/1271403.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值