The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End Loading A .docx with PHPWord Reply To: Loading A .docx with PHPWord

Harris Christiansen

Alright, thank you. For now so you can reproduce as much as possible I’ll put all up all code for what I have done and tell you what my file looks like.

I have tested the script with multiple .docx files, all of which I either download from online or type up something in Microsoft Word. I have tried things even as basic as Hello World. I am saving my files as I would save any Microsoft Word file, not in any special, template fashion, as I have not seen any indication that I should do so.

My code is:

require_once $_SERVER['DOCUMENT_ROOT'].'/Resources/plugins/PhpWord/Autoloader.php';
$PHPWord = new \PhpOffice\PhpWord\PhpWord();

$document = $PHPWord->loadTemplate('Resources/documents/test.docx');

$document->setValue('theDate', '2014-07-25');

$file = 'testFile.docx';
header("Content-Description: File Transfer");
header('Content-Disposition: attachment; filename="' . $file . '"');
header("Content-Type: application/docx");
header('Content-Transfer-Encoding: binary');
header("Cache-Control: public");
header('Expires: 0');
$xmlWriter = \PhpOffice\PhpWord\IOFactory::createWriter($PHPWord, 'Word2007');