PHP Word, sending the generated file to the browser as output without saving it to disk

I use PHP Word and htmltodocx_converter to create a dictionary document, everything works fine, but I just need an example of how I can send a file to the browser for downloading without saving it to disk. All the examples, I see, you save the file to your disk:

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

Does anyone know how I can output a file to a browser on the fly?

+4
source share
1 answer

This example below may work, but in order to get the expected download behavior in all browsers, the title Content-Typeshould be correct for Word2007. You may need additional headers for the correct output.

:

$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 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.

Microsoft

+4

Source: https://habr.com/ru/post/1617079/


All Articles