A pure PHP library for reading and writing word processing documents
Go to file
Progi1984 ff342cff6f FIXED : Error on Travis-CI with Pyrus on PHP 5.3.3 2014-02-24 20:11:02 +01:00
Classes Fixed bug list items inside of cells 2014-02-13 12:24:11 -05:00
Tests ADDED : Basic unit tests 2014-02-24 19:38:44 +01:00
samples Fixed some merge issues 2013-12-16 06:40:30 -05:00
.gitignore Merge branch 'master' of https://github.com/PHPOffice/PHPWord 2013-12-15 14:15:28 -05:00
.travis.yml FIXED : Error on Travis-CI with Pyrus on PHP 5.3.3 2014-02-24 20:11:02 +01:00
README.md Fixed bug with table cell styles and added grid span unit tests 2014-02-13 11:06:17 -05:00
changelog.txt ADDED : Basic unit tests 2014-02-24 19:38:44 +01:00
composer.json Happy new year! 2014-02-12 11:21:04 -05:00
license.md Added readme and license file 2012-07-05 07:25:13 +02:00

README.md

PHPWord

Build Status

OpenXML - Read, Write and Create Word documents in PHP.

PHPWord is a library written in PHP that create word documents.

No Windows operating system is needed for usage because the result are docx files (Office Open XML) that can be opened by all major office software.

Want to contribute? Fork us!

Requirements

  • PHP version 5.3.0 or higher

Installation

It is recommended that you install the PHPWord library through composer. To do so, add the following lines to your composer.json.

{
    "require": {
       "phpoffice/phpword": "dev-master"
    }
}

Documentation

Table of contents

  1. Basic usage
  2. Sections
  3. Tables
  4. Images

Basic usage

The following is a basic example of the PHPWord library.

$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');

Sections

Change Section Page Numbering

You can change a section page numbering.

$section = $PHPWord->createSection();
$section->getSettings()->setPageNumberingStart(1);

Tables

The following illustrates how to create a table.

$table = $section->addTable();
$table->addRow();
$table->addCell();

Cell Style
Cell Span

You can span a cell on multiple columms.

$cell = $table->addCell(200);
$cell->getStyle()->setGridSpan(5);

Images

You can add images easily using the following example.

$section = $PHPWord->createSection();
$section->addImage('mars.jpg');

Images settings include:

  • width width in pixels
  • height height in pixels
  • align image alignment, left, right or center
  • marginTop top margin in inches, can be negative
  • marginLeft left margin in inches, can be negative
  • wrappingStyle can be inline, square, tight, behind, infront

To add an image with settings, consider the following example.

$section->addImage(
    'mars.jpg',
    array(
        'width' => 100,
        'height' => 100,
        'marginTop' => -1,
        'marginLeft' => -1,
        'wrappingStyle' => 'behind'
    )
);