I have been striking my head against the wall in order to solve the problem of downloading the dynamically generate Word document. I would like to have a solution where on clicking the button, it would ask user to download the word document. I was able to generate the word document, but it was getting saved in my local directory and not asking for a specific location to download. After then, I made some changes but then it stopped generating the word document. I'm not sure what I'm doing wrong. Can someone please let me know what am I doing wrong.
<?
ini_set('display_errors',1);
ini_set('display_startup_errors',1);
require_once($_SERVER['DOCUMENT_ROOT'].'/vendor/phpoffice/src/PhpWord/Autoloader.php');
\PhpOffice\PhpWord\Autoloader::register();
$phpWord = new \PhpOffice\PhpWord\PhpWord();
if(isset($_POST['postData'])){
$jsonData = json_decode($_POST['postData'],true);
$fieldText = $jsonData['testingTime'];
$section = $phpWord->addSection();
$header = $section->addHeader();
$section->addText($fieldText);
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord,'Word2007');
//$filename = "Result.docx"; #Also, this is not working
$objWriter->save('Result.docx');
header('Content-Description: File Transfer');
//header('Content-Type: application/force-download');
header('Content-type: application/octet-stream');
header("Content-Disposition: attachment; Filename=Result.docx");
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Expires: 0');
header('Pragma: public');
header('Content-Length: ' .filesize('Result.docx'));
flush();
readfile('Result.docx');
unlink('Result.docx'); // deletes the temporary file
exit;}
?>