79 people following this project (follow)

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

sreevathsanr Apr 14 at 10:08 PM 
Hi All,
Referring to the discussion thread, "Calling addText() method gives a line break at the end of string", which is similar to the problem at my hand.
I want to give different styles to the words in a line.. Pls help with me with the solution.
Thanks in Advance.

-Vathsan

caashie Jan 23 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!