PHP Word, send generated file to browser as output without saving it on disc

a 夏天 提交于 2019-12-10 13:54:07

问题


I'm using PHP Word & htmltodocx_converter to create a word document, all works well but I just need an example of how I can send the file to browser for download without saving it on disc. All examples I see you save the file to your disc:

// 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

Anyone knows of how I can output the file to the browser on the fly?


回答1:


This below example maybe works, but to get the expected behaviour of the download in all browsers, the Content-Type header has to be correct for Word2007. Maybe you will need some additional headers for proper output.

You can try this:

$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

Thanks to @jamsandwich: The correct header for Word 2007 should be application/vnd.openxmlformats-officedocument.wordprocessing‌​ml.document.

Reference Microsoft



来源:https://stackoverflow.com/questions/33872714/php-word-send-generated-file-to-browser-as-output-without-saving-it-on-disc

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!