PHPWord is a library written in pure PHP that provides a set of classes to write to and read from different document file formats. The current version of PHPWord supports Microsoft [Office Open XML](http://en.wikipedia.org/wiki/Office_Open_XML) (OOXML or OpenXML), OASIS [Open Document Format for Office Applications](http://en.wikipedia.org/wiki/OpenDocument) (OpenDocument or ODF), and [Rich Text Format](http://en.wikipedia.org/wiki/Rich_Text_Format) (RTF).
No Windows operating system is needed for usage because the resulting DOCX, ODT, or RTF files can be opened by all major [word processing softwares](http://en.wikipedia.org/wiki/List_of_word_processors).
PHPWord is an open source project licensed under [LGPL](license.md). PHPWord is [unit tested](https://travis-ci.org/PHPOffice/PHPWord) to make sure that the released versions are stable.
__Want to contribute?__ [Fork us](https://github.com/PHPOffice/PHPWord/fork) or [submit](https://github.com/PHPOffice/PHPWord/issues) your bug reports or feature requests to us.
* Set document properties, e.g. title, subject, and creator.
* Create document sections with different settings, e.g. portrait/landscape, page size, and page numbering
* Create header and footer for each sections
* Set default font type, font size, and paragraph style
* Use UTF-8 and East Asia fonts/characters
* Define custom font styles (e.g. bold, italic, color) and paragraph styles (e.g. centered, multicolumns, spacing) either as named style or inline in text
* Insert paragraphs, either as a simple text or complex one (a text run) that contains other elements
* Insert titles (headers) and table of contents
* Insert text breaks and page breaks
* Insert and format images, either local, remote, or as page watermarks
* Insert binary OLE Objects such as Excel or Visio
* Insert and format table with customized properties for each rows (e.g. repeat as header row) and cells (e.g. background color, rowspan, colspan)
* Insert list items as bulleted, numbered, or multilevel
To install manually, [download PHPWord package from github](https://github.com/PHPOffice/PHPWord/archive/master.zip). Extract the package and put the contents to your machine. To use the library, include `Classes/PHPWord.php` in your script like below.
After installation, you can browse and use the samples that we've provided, either by command line or using browser. If you can access your PHPWord library folder using browser, point your browser to the `samples` folder, e.g. `http://localhost/PHPWord/samples/`.
Each section can have its own header reference. To create a header use the `createHeader` method:
```php
$header = $section->createHeader();
```
Be sure to save the result in a local object. You can use all elements that are available for the footer. See "Footer" section for detail. Additionally, only inside of the header reference you can add watermarks or background pictures. See "Watermarks" section.
<aname="footers"></a>
### Footers
Each section can have its own footer reference. To create a footer, use the `createFooter` method:
```php
$footer = $section->createFooter();
```
Be sure to save the result in a local object to add elements to a footer. You can add the following elements to footers:
* Texts `addText` and `createTextrun`
* Text breaks
* Images
* Tables
* Preserve text
See the "Elements" section for the detail of each elements.
Text can be added by using `addText` and `createTextRun` method. `addText` is used for creating simple paragraphs that only contain texts with the same style. `createTextRun` is used for creating complex paragraphs that contain text with different style (some bold, other italics, etc) or other elements, e.g. images or links. The syntaxes are as follow:
You can use the `$fontStyle` and `$paragraphStyle` variable to define text formatting. There are 2 options to style the inserted text elements, i.e. inline style by using array or defined style by adding style definition.
* ``align`` Paragraph alignment, _left_, _right_ or _center_
* ``spaceBefore`` Space before paragraph
* ``spaceAfter`` Space after paragraph
* ``indent`` Indent by how much
* ``hanging`` Hanging by how much
* ``basedOn`` Parent style
* ``next`` Style for next paragraph
* ``widowControl`` Allow first/last line to display on a separate page, _true_ or _false_
* ``keepNext`` Keep paragraph with next paragraph, _true_ or _false_
* ``keepLines`` Keep all lines on one page, _true_ or _false_
* ``pageBreakBefore`` Start paragraph on next page, _true_ or _false_
* ``lineHeight`` text line height, e.g. _1.0_, _1.5_, ect...
* ``tabs`` Set of custom tab stops
<aname="titles"></a>
#### Titles
If you want to structure your document or build table of contents, you need titles or headings. To add a title to the document, use the `addTitleStyle` and `addTitle` method.
* ``$listStyle`` List style of the current element TYPE_NUMBER, TYPE_ALPHANUM, TYPE_BULLET_FILLED, etc. See list of constants in PHPWord_Style_ListItem.
* ``$paragraphStyle`` See "Paragraph style" section.
To add an image, use the `addImage` or `addMemoryImage` method. The first one is used when your source is stored locally, the later is used when your source is a remote URL, either another script that create image or an image on the internet.
* ``align`` Image alignment, _left_, _right_, or _center_
* ``marginTop`` Top margin in inches, can be negative
* ``marginLeft`` Left margin in inches, can be negative
* ``wrappingStyle`` Wrapping style, _inline_, _square_, _tight_, _behind_, or _infront_
#### Watermarks
To add a watermark (or page background image), your section needs a header reference. After creating a header, you can use the `addWatermark` method to add a watermark.
You can add OLE embeddings, such as Excel spreadsheets or PowerPoint presentations to the document by using `addObject` method.
```php
$section->addObject($src, [$style]);
```
<aname="toc"></a>
### Table of contents
To add a table of contents (TOC), you can use the `addTOC` method. Your TOC can only be generated if you have add at least one title (See "Titles").
```php
$section->addTOC([$fontStyle], [$tocStyle]);
```
* ``tabLeader`` Fill type between the title text and the page number. Use the defined constants in PHPWord_Style_TOC.
* ``tabPos`` The position of the tab where the page number appears in twips.
* ``indent`` The indent factor of the titles in twips.
<aname="footnotes"></a>
### Footnotes
You can create footnotes in texts or textruns, but it's recommended to use textrun to have better layout.
On textrun:
```php
$textrun = $section->createTextRun();
$textrun->addText('Lead text.');
$footnote = $textrun->createFootnote();
$footnote->addText('Footnote text.');
$textrun->addText('Trailing text.');
```
On text:
```php
$section->addText('Lead text.');
$footnote = $section->createFootnote();
$footnote->addText('Footnote text.');
```
## Templates
You can create a docx template with included search-patterns that can be replaced by any value you wish. Only single-line values can be replaced. To load a template file, use the `loadTemplate` method. After loading the docx template, you can use the `setValue` method to change the value of a search pattern. The search-pattern model is: `${search-pattern}`. It is not possible to add new PHPWord elements to a loaded template file.