Automatic file download using PHPWord

I am trying to use PHPWord to create text documents. And the document can be generated successfully. But there is a problem when my generated document is saved on the server. How can I make it available for download immediately?

Example:

$PHPWord = new PHPWord(); //Searching for values to replace $document = $PHPWord->loadTemplate('doc/Temp1.docx'); $document->setValue('Name', $Name); $document->setValue('No', $No); $document->save('php://output'); //it auto save into my 'doc' directory. 

How can I reference the header to load it like this:

 header("Content-Disposition: attachment; filename='php://output'"); //not sure how to link this filename to the php://output.. 

Please inform.

+10
source share
7 answers

php://output is a write-only stream that is being written to your screen (e.g. echo ).

So $document->save('php://output'); will not save the file anywhere on the server, it will simply repeat it.

It seems that $document->save does not support stream wrappers, so it literally made a file called "php://output" . Try using a different file name (I suggest a temporary file as you just want to repeat it).

 $temp_file = tempnam(sys_get_temp_dir(), 'PHPWord'); $document->save($temp_file); 

In the header the filename field is what PHP tells the browser that the file is named, it should not be the name of the file on the server. This is just the name that the browser will save as.

 header("Content-Disposition: attachment; filename='myFile.docx'"); 

So, all together:

 $PHPWord = new PHPWord(); //Searching for values to replace $document = $PHPWord->loadTemplate('doc/Temp1.docx'); $document->setValue('Name', $Name); $document->setValue('No', $No); // // save as a random file in temp file $temp_file = tempnam(sys_get_temp_dir(), 'PHPWord'); $document->save($temp_file); // Your browser will name the file "myFile.docx" // regardless of what it named on the server header("Content-Disposition: attachment; filename='myFile.docx'"); readfile($temp_file); // or echo file_get_contents($temp_file); unlink($temp_file); // remove temp file 
+12
source
 $objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007'); $filename = 'MyFile.docx'; $objWriter->save($filename); header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename='.$filename); header('Content-Transfer-Encoding: binary'); header('Expires: 0'); header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); header('Pragma: public'); header('Content-Length: ' . filesize($filename)); flush(); readfile($filename); unlink($filename); // deletes the temporary file exit; 
+8
source
 // Save File $objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007'); header("Content-Disposition: attachment; filename='myFile.docx'"); $objWriter->save("php://output"); 
+1
source

now instead of version 0.13.0 https://github.com/PHPOffice/PHPWord

 <? require_once "../include/PHPWord-develop/bootstrap.php"; $templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor('template.docx'); $templateProcessor->setValue('var01', 'Sun'); $templateProcessor->setValue('var02', 'Mercury'); //##################################################### // Save File //##################################################### //##################################################### header("Content-Disposition: attachment; filename='output01.docx'"); $templateProcessor->saveAs('php://output'); //##################################################### //##################################################### ?> 
+1
source

This is the job for me:

 $objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007', $download = true); header("Content-Disposition: attachment; filename='File.docx'"); $objWriter->save("php://output"); 
+1
source

Sorry for coming later. I stumbled upon this while trying to solve the same problem. I managed to get it working on Laravel 5 using below:

  $file_dir = $template_upload_dir.DIRECTORY_SEPARATOR.'filename.docx'; $tags = array(); if (file_exists($file_dir)) { $templateProcessor = new TemplateProcessor($file_dir); $tags = $templateProcessor->getVariables(); $replace = array(''); $templateProcessor->setValue($tags, $replace); $save_file_name = $fullname.'-'.$inv_code.'-'.date('YmdHis').'.docx'; $templateProcessor->saveAs($save_file_name); return response()->download($save_file_name)->deleteFileAfterSend(true); } 

Hope this helps someone !!!

0
source

A simple solution for Laravel based on @ user3214824 answer.

 // ... $objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($PHPWord, 'Word2007'); $doc_name = 'fileName.docx'; $objWriter->save($doc_name); // saving in the public path just for testing return response()->download(public_path($doc_name))->deleteFileAfterSend(true); 
0
source

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


All Articles