Billybob Hakalaka Billybob Hakalaka - 1 year ago 630
PHP Question

Issues Installing PHPword

I've been trying to install PHPWord for the last 4 hours and have had no luck. I've tried all methods of installation including composer as well as downloading the folder itself. Whenever I run the php file, it always returns me the error "Fatal error: Class 'PhpWord' not found in /home/ubuntu/workspace/hello-world.php on line 9" Also, it seems that whenever I attached the phpoffice/phpword in the composer.json file, it kept on giving me an installation error saying no version specified. BTW, I am running these files on a cloud hosting website (

I've attached my composer.json as well as the hello_world.php

Any help is much appreciated.



//use PhpOffice\PhpWord\PhpWord;
//use PhpOffice\PhpWord\Style\Font;
// Creating the new document...
$phpWord = new PhpWord();

/* Note: any element you append to a document must reside inside of a Section. */

Note: it's possible to customize font style of the Text element you add in three ways:
- inline;
- using named font style (new font style object will be implicitly created);
- using explicitly created font style object. */
// Adding Text element with font customized inline...
'"Great achievement is usually born of great sacrifice, '
. 'and is never the result of selfishness." '
. '(Napoleon Hill)'
array('name' => 'Tahoma', 'size' => 10)

// Adding Text element with font customized using named font style...
$fontStyleName = 'oneUserDefinedStyle';
array('name' => 'Tahoma', 'size' => 10, 'color' => '1B2232', 'bold' => true)
'"The greatest accomplishment is not in never falling, '
. 'but in rising again after you fall." '
. '(Vince Lombardi)'

// Adding Text element with font customized using explicitly created font style object...
$fontStyle = new \PhpOffice\PhpWord\Style\Font();
$myTextElement = $section->addText(
htmlspecialchars('"Believe you can and you\'re halfway there." (Theodor Roosevelt)')

// Saving the document as OOXML file...
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');

// Saving the document as ODF file...
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'ODText');

// Saving the document as HTML file...
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'HTML');

/* Note: we skip RTF, because it's not XML-based and requires a different example. /
/ Note: we skip PDF, because "HTML-to-PDF" approach is used to create PDF documents. */

This is the composer.json require part

"php": ">=5.3.3",
"ext-xml": "*",

Answer Source

It looks like it installed properly since you're getting past the autoloader statements.

The issue is that PhpWord is not the class, it would be \PhpOffice\PhpWord\PhpWord since it's namespaced.

So you can try:

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

or add the use statement back towards the top of the script which is commented out:

use PhpOffice\PhpWord\PhpWord;

This will allow you to call the class as PhpWord instead of the fully namespaced version.


Try this:

From the command line (you've probably already done this):

composer require phpoffice/phpword

Create file test.php with contents:


require_once 'vendor/autoload.php';

$phpword = new \PhpOffice\PhpWord\PhpWord();
$section = $phpword->addSection();

$section->addText("Hello World!");

$phpword->save('./hello.docx', 'Word2007');

That should work.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download