Maurice
Maurice

Reputation: 477

PHPWord - Not working locally?

Does anyone have any experience using PHPWord? I installed it using Composer like I have done with many other modules, but when I run the "Basic Usage" example, nothing happens. (autoloader.php is working fine).

If I change $objWriter->save('helloWorld.docx'); to $xmlWriter->save("php://output"); I at least see something is happening (see below), but nothing I do downloads a DOCX, ODT, or HTML file.

enter image description here

Normally Composer downloads any requires/dependencies, is there something else I need to do?

Using Ampps on Windows.

Upvotes: 0

Views: 1503

Answers (1)

Maurice
Maurice

Reputation: 477

Urgh, found it. You need to add the relevant headers for it to work, no idea why the example provided doesn't mention this:

<?php

require_once("vendor/autoload.php");

$phpWord = new \PhpOffice\PhpWord\PhpWord();

$section = $phpWord->addSection();
$section->addText('Hello World');

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

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

?>

Upvotes: 2

Related Questions