Using PHPWord I load a template from my public directory:
Code:
$templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor(asset_url() . 'templates/receipt_template.docx');
I generate a file name and set headers for downloading the file:
Code:
$datetime = date('Y_M_d_G_i');
$filename = 'receipt_' . $datetime . '.docx';
header("Content-Description: File Transfer");
header('Content-Disposition: attachment; filename="' . $filename . '"');
header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Expires: 0');
I save the template as a new docx file (no changes for now) to the same directory. This works. I can see the new file in the public html directory and it has all the content of the template
Code:
$templateProcessor->saveAs(set_realpath('assets/templates/' . $filename));
This code downloads a word file, but the file is blank:
Code:
force_download(set_realpath('assets/templates/' . $filename, NULL, TRUE));
The above code resides in one controller. Is it a problem with opening a new filehandle before the other one is closed?