I am working with Laravel 5.8 and i am using phpword to creat .rtf file.
When i download it the it is opening as zip file. I changed the file type rtf to docx but i got the same result.
Asked
Active
Viewed 97 times
1
-
What content type do you send to the client? – MrTux Feb 10 '21 at 06:18
-
I am not creating it with header. I just save template. here is my code. ~~~ $templateProcessor->setUpdateFields(true); $time = date('dmYhis'); $templateProcessor->saveAs(public_path('Report_File/Pci_ROC_' . $time . '.rtf')); return response()->download(public_path('Report_File/Pci_ROC_' . $time . '.rtf'))->deleteFileAfterSend(true); ~~~ – Feb 10 '21 at 06:52
-
Cf. https://stackoverflow.com/a/20415796/3906760 – MrTux Feb 10 '21 at 07:37
-
Do other RTF files not generated via PHPWord open normally? – apokryfos Feb 10 '21 at 07:53
-
Yes Other RTF files not generated via PHPWord open normal. – Feb 10 '21 at 09:12
-
@MrTux i tried that but it is not working for me – Feb 10 '21 at 10:01
-
Then since this may be an issue with how the file is generated can you share the code you are using? Also try to save the file first and open it from the downloaded location to see if the issue persists. – apokryfos Feb 10 '21 at 13:15
-
When file saved and i opened it files opens normally. i got this issue when i download the file and want to directly access from browser. – Feb 11 '21 at 05:28
1 Answers
0
With this bellow code i can manage to download the file as RTF instead of Zip archive.
Ok now I can manage to solve it with bellow code
############## Create RTF file ##############
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
$objWriter->save(public_path('Report_File/InfoSec_DFAR_Report_' . $time . '.rtf'));
############## Create RTF file ##############
############## Download RTF file ##############
header("Cache-Control: public"); // needed for internet explorer
header("Content-Type: application/rtf");
header("Content-Transfer-Encoding: Binary");
header("Content-Length:".filesize(public_path('Report_File/filename' . $time . '.rtf')));
header("Content-Disposition: attachment; filename=InfoSec_DFAR_Report_".$time. '.rtf');
readfile(public_path('Report_File/filename' . $time . '.rtf'));
die();
############## Download RTF file ##############