Welcome Guest, Not a member yet? Register   Sign In
Blank document when downloading word file just created.
#1

(This post was last modified: 06-09-2016, 06:32 PM by Shawn.)

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?
Reply
#2

If a file is open it cannot be modified.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

(06-10-2016, 04:18 AM)InsiteFX Wrote: If a file is open it cannot be modified.

How do I ensure that the file has been closed after the TemplateProcessor->saveAs statement, before attempting the force_download?
Reply
#4

This is the solution I arrived at:
I created as directory structure at the same level of the application and system directory (outside the public html)
assets\templates\receipt_template.docx
assets\receipts\

In my model:
Code:
// delete any previous files created and downloaded, first
$this->delete_receipt('../assets/receipts');
//create TemplateProcessor object with word doc
$templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor('../assets/templates/receipt_template.docx');
// make substitutions
$templateProcessor->setValue('name', htmlspecialchars('Elementary School Teachers Federtion of Ontario', ENT_COMPAT, 'UTF-8'));
$templateProcessor->setValue('amount', htmlspecialchars('35.50', ENT_COMPAT, 'UTF-8'));
// generate file name and save in receipts directory
$datetime = date('Y_M_d_G_i');
$filename = 'receipt_' . $datetime . '.docx';
$templateProcessor->saveAs('../assets/receipts/' . $filename);
//download
force_download('../assets/receipts/' . $filename, NULL, TRUE);
Reply




Theme © iAndrew 2016 - Forum software by © MyBB