0

I want to download a word file from an existing template using PHPWord. I tried this code but it generate corrupted file:

$file = ROOT . '/web/templates/recu.docx';
$PHPWord = new \PhpOffice\PhpWord\TemplateProcessor($file);

$PHPWord->setValue('Value1', date('d/m/Y'));
$PHPWord->setValue('Value2', "ffff");
$PHPWord->setValue('Value3', "sabrine");
$PHPWord->setValue('Value4', utf8_decode("555555"));
$PHPWord->setValue('Value5', "200000");

$new_file = "recu_".date('d_m_Y_H_i');
$PHPWord->saveAs(ROOT. '/web/uploads/recu/'.$new_file.'.docx');

header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment;filename="'.$new_file.'.docx"');

$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($PHPWord, 
'Word2007');
ob_clean();
$objWriter->save('php://output');
exit;

any suggestions why my code didn't work ?

Sabra
  • 177
  • 2
  • 19
  • Does only the directly downloaded version not work, or the one you store on the server neither? Is the file size around what would expect, or is it significantly smaller? Have you tried to open it with a text or hex editor, and checked if it contains any error message? (Do you have proper PHP error reporting enabled? If not, go do that first of all!) – 04FS Sep 20 '19 at 13:34

1 Answers1

0

Try this content header instead:

header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
Sumit Wadhwa
  • 2,825
  • 1
  • 20
  • 34