Requirements

  • PHP 5.2.x or newer
  • PHP Extension ZipArchive
  • PHP Extension xmllib
  • For opening by PHPWord created files in Microsoft Office < 2007 you need the "Microsoft Office Compatibility Pack". You can get it here for free: Microsoft Office Compability Pack

Installation and configuration

Installation and configuration is very easy:
  • Extract the ZIP-Archive
  • Copy the source files to your webserver
  • If your PHPWord working directory isn't your PHP include path you can set the PHPWord base path. To set the PHPWord base path open the PHPWord.php inside the root folder and edit the value of PHPWORD_BASE_PATH constant.

Basic example

// Include the PHPWord.php, all other classes were loaded by an autoloader
require_once 'PHPWord.php';

// Create a new PHPWord Object
$PHPWord = new PHPWord();

// Every element you want to append to the word document is placed in a section. So you need a section:
$section = $PHPWord->createSection();

// After creating a section, you can append elements:
$section->addText('Hello world!');

// You can directly style your text by giving the addText function an array:
$section->addText('Hello world! I am formatted.', array('name'=>'Tahoma', 'size'=>16, 'bold'=>true));

// If you often need the same style again you can create a user defined style to the word document
// and give the addText function the name of the style:
$PHPWord->addFontStyle('myOwnStyle', array('name'=>'Verdana', 'size'=>14, 'color'=>'1B2232'));
$section->addText('Hello world! I am formatted by a user defined style', 'myOwnStyle');

// You can also putthe appended element to local object an call functions like this:
$myTextElement = $section->addText('Hello World!');
$myTextElement->setBold();
$myTextElement->setName('Verdana');
$myTextElement->setSize(22);

// At least write the document to webspace:
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
$objWriter->save('helloWorld.docx');

Documentation

You can download examples and a full documentation of PHPWord here

Font Style changes between 0.6.1 and 0.6.2:

With the release 0.6.2 the paragraph properties has been moved into a separate Class!
So, every text element now has two style properties: FONT and PARAGRAPH. See the following example for more information:

// OLD release, 0.6.1:
$styleFont = array('bold'=>true, 'size'=>16, 'name'=>'Calibri', 'align'=>'center', 'spaceAfter'=>100);
$section->addText('Hello World', $styleFont);

// NEW release, 0.6.2:
$styleFont = array('bold'=>true, 'size'=>16, 'name'=>'Calibri');
$styleParagraph = array('align'=>'center', 'spaceAfter'=>100);
$section->addText('Hello World', $styleFont, $styleParagraph);

Last edited Jul 25, 2010 at 7:56 PM by raw_venture_ad, version 18

Comments

ozaarm Oct 21 at 2:07 PM 
Re: the styling, setBold(), setSize() may have disappeared, but this from /Examples/Text.php worked :

$PHPWord->addFontStyle('myStyle1', array('bold'=>true, 'italic'=>true, 'size'=>16));
$section->addText('Hello text with the new stylish style !','myStyle1');

ozaarm Oct 21 at 1:50 PM 
same as fallphenix Aug 1 at 2:00 PM, with the example code around setBold and setSize, commented out and it worked.

PHP Fatal error: Call to undefined method PHPWord_Section_Text::setBold()
PHP Fatal error: Call to undefined method PHPWord_Section_Text::setSize()

Pratiksha2211 Aug 6 at 6:23 AM 
Its work good but sometime i have problem with some special characters like &,$ etc.. and is there any way to read and write doc file ??

fallphenix Aug 1 at 12:00 PM 
error over :
$myTextElement = $section->addText('Hello World!');
$myTextElement->setBold();
$myTextElement->setName('Verdana');
$myTextElement->setSize(22);

object(PHPWord_Section_Text)[84]
private '_text' => string 'Hello World!' (length=12)
private '_styleFont' => null
private '_styleParagraph' => null


setter setBold(),..; are undifined

yangbai May 24 at 12:51 PM 
beautiful!

rahmuna Feb 26 at 8:43 AM 
i can not open the file in webpage

hake12308 Dec 18, 2013 at 2:19 AM 
hi,can it make the part of word document protected? the name in the word office is limited to edit.

JackStone Nov 25, 2013 at 7:51 PM 
Is there just a simple find-and-replace function? I want to be able to go into an existing section and replace the text there.

hervehant Aug 19, 2013 at 11:58 AM 
please
I wonder how two columns in a word document in php from PHPWord

kishore1587 Mar 13, 2013 at 3:36 PM 
Hi i m new for this
please let me know
How to add table in word file using this PHP word?

huanga Oct 9, 2012 at 5:31 PM 
docx uses a very special unit for calculating width and height, etc. It took me a long time to search for it. In case you need to set a length too read this first: http://startbigthinksmall.wordpress.com/2010/01/04/points-inches-and-emus-measuring-units-in-office-open-xml/

djodjo Oct 2, 2012 at 7:51 AM 
Is it possible to open a doc file, change properties (title, tags, subject, ...) and save the file ?

papyloup Jul 9, 2012 at 12:54 PM 
any possibilité to read a docx file with bookmarks or position of cursor ?

sreevathsanr Jun 22, 2012 at 6:36 PM 
hey.. i want to create a msword 2003 file. i am doubtful whether it supports, if so, can u help me pls.. Thanks in Advance

danivu May 21, 2012 at 1:10 PM 
great tool, but some problem with utf-8 characters

Maybe could be done same as is done odtPHP for OpenOffice, use formated document template and fill with data. This is ismplest and easiest way to create document,
each client can have his own style without changing the source code

caashie Jan 23, 2012 at 12:51 PM 
need help with utf-8 support...

onekit Jul 20, 2011 at 8:06 AM 
I have found the reason why Cyrillic didn't displayed correct. The library in Section.php do utf8_encode() twice. English letters don't care about double utf8_encode(), but russian letters turns to abracadabra-language. So, remark all of utf8_encode() functions in Section.php and all work done. Need not to encode to utf8 without checking if it already in utf8. If source already in UTF-8, then we'll needn't to encode it.

onekit Jul 18, 2011 at 7:47 AM 
Problem with UTF-8, can't write Cyrillic.

hardikhardik May 18, 2011 at 1:06 PM 
Its great....Is there any possibilities to read docx document?

lagrandeours May 3, 2011 at 11:35 PM 
it's work great ... But : i have some problems with with utf-8 characters, do you have any idea how to solve this matter ...

panuwat27 Mar 7, 2011 at 4:50 PM 
great!