|  Download <!---
Markdown source for rst files since it's easier to write in md format.
This source file will be converted to rst using pandoc or any other converter.
Don't forget to change code::directive tocode-block::in the resulting rst.
--> ContentsIntroductionPHPWord 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 (OOXML or OpenXML), OASIS Open Document Format for Office Applications (OpenDocument or ODF), and Rich Text Format (RTF). PHPWord is an open source project licensed under the terms of LGPL version 3. PHPWord is aimed to be a high quality software product by incorporating continuous integration and unit testing. You can learn more about PHPWord by reading this Developers' Documentation and the API Documentation. Features
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
Insert hyperlinks
Insert footnotes and endnotes
Insert drawing shapes (arc, curve, line, polyline, rect, oval)
Insert charts (pie, doughnut, bar, line, area, scatter, radar)
Insert form fields (textinput, checkbox, and dropdown)
Create document from templates
Use XSL 1.0 style sheets to transform main document part of OOXML template
... and many more features on progress
 File formatsBelow are the supported features for each file formats. Writers| Features                |                    | DOCX | ODT | RTF | HTML | PDF |
|-------------------------|--------------------|------|-----|-----|------|-----|
| Document Properties | Standard           | ✓    | ✓   | ✓   | ✓    |     |
|                         | Custom             | ✓    | ✓   |     |      |     |
| Element Type        | Text               | ✓    | ✓   | ✓   | ✓    | ✓   |
|                         | Text Run           | ✓    | ✓   | ✓   | ✓    | ✓   |
|                         | Title              | ✓    | ✓   |     | ✓    | ✓   |
|                         | Link               | ✓    | ✓   | ✓   | ✓    | ✓   |
|                         | Preserve Text      | ✓    |     |     |      |     |
|                         | Text Break         | ✓    | ✓   | ✓   | ✓    | ✓   |
|                         | Page Break         | ✓    |     | ✓  |      |     |
|                         | List               | ✓    |     |     |      |     |
|                         | Table              | ✓    | ✓   | ✓   | ✓    | ✓   |
|                         | Image              | ✓    | ✓   | ✓   | ✓    |     |
|                         | Object             | ✓    |     |     |      |     |
|                         | Watermark          | ✓    |     |     |      |     |
|                         | Table of Contents  | ✓    |     |     |      |     |
|                         | Header             | ✓    |     |     |      |     |
|                         | Footer             | ✓    |     |     |      |     |
|                         | Footnote           | ✓    |     |     | ✓    |     |
|                         | Endnote            | ✓    |     |     | ✓    |     |
| Graphs              | 2D basic graphs    | ✓    |     |     |      |     |
|                         | 2D advanced graphs |      |     |     |      |     |
|                         | 3D graphs          | ✓    |     |     |      |     |
| Math                | OMML support       |      |     |     |      |     |
|                         | MathML support     |      |     |     |      |     |
| Bonus               | Encryption         |      |     |     |      |     |
|                         | Protection         |      |     |     |      |     | Readers| Features                |                    | DOCX | ODT | RTF | HTML|
|-------------------------|--------------------|------|-----|-----|-----|
| Document Properties | Standard           | ✓    |     |     |     |
|                         | Custom             | ✓    |     |     |     |
| Element Type        | Text               | ✓    | ✓   | ✓   | ✓   |
|                         | Text Run           | ✓    |     |     |     |
|                         | Title              | ✓    | ✓   |     |     |
|                         | Link               | ✓    |     |     |     |
|                         | Preserve Text      | ✓    |     |     |     |
|                         | Text Break         | ✓    |     |     |     |
|                         | Page Break         | ✓    |     |     |     |
|                         | List               | ✓    | ✓   |     | ✓   |
|                         | Table              | ✓    |     |     | ✓   |
|                         | Image              | ✓    |     |     |     |
|                         | Object             |      |     |     |     |
|                         | Watermark          |      |     |     |     |
|                         | Table of Contents  |      |     |     |     |
|                         | Header             | ✓    |     |     |     |
|                         | Footer             | ✓    |     |     |     |
|                         | Footnote           | ✓    |     |     |     |
|                         | Endnote            | ✓    |     |     |     |
| Graphs              | 2D basic graphs    |      |     |     |     |
|                         | 2D advanced graphs |      |     |     |     |
|                         | 3D graphs          |      |     |     |     |
| Math                | OMML support       |      |     |     |     |
|                         | MathML support     |      |     |     |     |
| Bonus               | Encryption         |      |     |     |     |
|                         | Protection         |      |     |     |     | ContributingWe welcome everyone to contribute to PHPWord. Below are some of the things that you can do to contribute: Installing/configuringRequirementsMandatory: Optional PHP extensions: InstallationThere are two ways to install PHPWord, i.e. via Composer or manually by downloading the library. Using ComposerTo install via Composer, add the following lines to your composer.json: {
    "require": {
       "phpoffice/phpword": "dev-master"
    }
}
 Manual installTo install manually, download PHPWord package from github. Extract the package and put the contents to your machine. To use the library, include src/PhpWord/Autoloader.phpin your script and invokeAutoloader::register. require_once '/path/to/src/PhpWord/Autoloader.php';
\PhpOffice\PhpWord\Autoloader::register();
 Using samplesAfter 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 samplesfolder, e.g.http://localhost/PhpWord/samples/. General usageBasic exampleThe following is a basic example of the PHPWord library. More examples are provided in the samples folder. <?php
require_once 'src/PhpWord/Autoloader.php';
\PhpOffice\PhpWord\Autoloader::register();
// Creating the new document...
$phpWord = new \PhpOffice\PhpWord\PhpWord();
/Note: any element you append to a document must reside inside of a Section./
// Adding an empty Section to the document...
$section = $phpWord->addSection();
// Adding Text element to the Section having font styled by default...
$section->addText(
    htmlspecialchars(
        '"Learn from yesterday, live for today, hope for tomorrow. '
            . 'The important thing is not to stop questioning." '
            . '(Albert Einstein)'
    )
);
/*
 * 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...
$section->addText(
    htmlspecialchars(
        '"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';
$phpWord->addFontStyle(
    $fontStyleName,
    array('name' => 'Tahoma', 'size' => 10, 'color' => '1B2232', 'bold' => true)
);
$section->addText(
    htmlspecialchars(
        '"The greatest accomplishment is not in never falling, '
            . 'but in rising again after you fall." '
            . '(Vince Lombardi)'
    ),
    $fontStyleName
);
// Adding Text element with font customized using explicitly created font style object...
$fontStyle = new \PhpOffice\PhpWord\Style\Font();
$fontStyle->setBold(true);
$fontStyle->setName('Tahoma');
$fontStyle->setSize(13);
$myTextElement = $section->addText(
    htmlspecialchars('"Believe you can and you\'re halfway there." (Theodor Roosevelt)')
);
$myTextElement->setFontStyle($fontStyle);
// Saving the document as OOXML file...
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
$objWriter->save('helloWorld.docx');
// Saving the document as ODF file...
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'ODText');
$objWriter->save('helloWorld.odt');
// Saving the document as HTML file...
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'HTML');
$objWriter->save('helloWorld.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./
 SettingsThe PhpOffice\PhpWord\Settingsclass provides some options that will affect the behavior of PHPWord. Below are the options. XML Writer compatibilityThis option sets XMLWriter::setIndent and XMLWriter::setIndentString. The default value of this option is true(compatible), which is required for OpenOffice to render OOXML document correctly. You can set this option tofalseduring development to make the resulting XML file easier to read. \PhpOffice\PhpWord\Settings::setCompatibility(false);
 Zip classBy default, PHPWord uses PHP ZipArchive to read or write ZIP compressed archive and the files inside them. If you can't have ZipArchive installed on your server, you can use pure PHP library alternative, PCLZip, which included with PHPWord. \PhpOffice\PhpWord\Settings::setZipClass(\PhpOffice\PhpWord\Settings::PCLZIP);
 Default fontBy default, every text appears in Arial 10 point. You can alter the default font by using the following two functions: $phpWord->setDefaultFontName('Times New Roman');
$phpWord->setDefaultFontSize(12);
 Document informationYou can set the document information such as title, creator, and company name. Use the following functions: $properties = $phpWord->getDocInfo();
$properties->setCreator('My name');
$properties->setCompany('My factory');
$properties->setTitle('My title');
$properties->setDescription('My description');
$properties->setCategory('My category');
$properties->setLastModifiedBy('My name');
$properties->setCreated(mktime(0, 0, 0, 3, 12, 2014));
$properties->setModified(mktime(0, 0, 0, 3, 14, 2014));
$properties->setSubject('My subject');
$properties->setKeywords('my, key, word');
 Measurement unitsThe base length unit in Open Office XML is twip. Twip means "TWentieth of an Inch Point", i.e. 1 twip = 1/1440 inch. You can use PHPWord helper functions to convert inches, centimeters, or points to twips. // Paragraph with 6 points space after
$phpWord->addParagraphStyle('My Style', array(
    'spaceAfter' => \PhpOffice\PhpWord\Shared\Converter::pointToTwip(6))
);
$section = $phpWord->addSection();
$sectionStyle = $section->getStyle();
// half inch left margin
$sectionStyle->setMarginLeft(\PhpOffice\PhpWord\Shared\Converter::inchToTwip(.5));
// 2 cm right margin
$sectionStyle->setMarginRight(\PhpOffice\PhpWord\Shared\Converter::cmToTwip(2));
 ContainersContainers are objects where you can put elements (texts, lists, tables, etc). There are 3 main containers, i.e. sections, headers, and footers. There are 3 elements that can also act as containers, i.e. textruns, table cells, and footnotes. SectionsEvery visible element in word is placed inside of a section. To create a section, use the following code: $section = $phpWord->addSection($sectionStyle);
 The $sectionStyleis an optional associative array that sets the section. Example: $sectionStyle = array(
    'orientation' => 'landscape',
    'marginTop' => 600,
    'colsNum' => 2,
);
 Page numberYou can change a section page number by using the pageNumberingStartstyle of the section. // Method 1
$section = $phpWord->addSection(array('pageNumberingStart' => 1));
// Method 2
$section = $phpWord->addSection();
$section->getStyle()->setPageNumberingStart(1);
 MulticolumnYou can change a section layout to multicolumn (like in a newspaper) by using the breakTypeandcolsNumstyle of the section. // Method 1
$section = $phpWord->addSection(array('breakType' => 'continuous', 'colsNum' => 2));
// Method 2
$section = $phpWord->addSection();
$section->getStyle()->setBreakType('continuous');
$section->getStyle()->setColsNum(2);
 Line numberingYou can apply line numbering to a section by using the lineNumberingstyle of the section. // Method 1
$section = $phpWord->addSection(array('lineNumbering' => array()));
// Method 2
$section = $phpWord->addSection();
$section->getStyle()->setLineNumbering(array());
 Below are the properties of the line numbering style. 
`start` Line numbering starting value
`increment` Line number increments
`distance` Distance between text and line numbering in twip
`restart` Line numbering restart setting continuous|newPage|newSection
 HeadersEach section can have its own header reference. To create a header use the addHeadermethod: $header = $section->addHeader();
 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. FootersEach section can have its own footer reference. To create a footer, use the addFootermethod: $footer = $section->addFooter();
 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. Other containersTextruns, table cells, and footnotes are elements that can also act as containers. See the corresponding "Elements" section for the detail of each elements. ElementsBelow are the matrix of element availability in each container. The column shows the containers while the rows lists the elements. | Num | Element       | Section | Header | Footer | Cell | Text Run | Footnote |
|-----|---------------|---------|--------|--------|------|----------|----------|
| 1   | Text          | v       | v      | v      | v    | v        | v        |
| 2   | Text Run      | v       | v      | v      | v    | -        | -        |
| 3   | Link          | v       | v      | v      | v    | v        | v        |
| 4   | Title         | v       | ?      | ?      | ?    | ?        | ?        |
| 5   | Preserve Text | ?       | v      | v      | v*   | -        | -        |
| 6   | Text Break    | v       | v      | v      | v    | v        | v        |
| 7   | Page Break    | v       | -      | -      | -    | -        | -        |
| 8   | List          | v       | v      | v      | v    | -        | -        |
| 9   | Table         | v       | v      | v      | v    | -        | -        |
| 10  | Image         | v       | v      | v      | v    | v        | v        |
| 11  | Watermark     | -       | v      | -      | -    | -        | -        |
| 12  | Object        | v       | v      | v      | v    | v        | v        |
| 13  | TOC           | v       | -      | -      | -    | -        | -        |
| 14  | Footnote      | v       | -      | -      | v| v      | -        |
| 15  | Endnote       | v       | -      | -      | v| v      | -        |
| 16  | CheckBox      | v       | v      | v      | v    | -        | -        |
| 17  | TextBox       | v       | v      | v      | v    | -        | -        |
| 18  | Field         | v       | v      | v      | v    | v        | v        |
| 19  | Line          | v       | v      | v      | v    | v        | v        |
| 20  | Shape         | v       | v      | v      | v    | v        | v        |
| 21  | Chart         | v       | -      | -      | -    | -        | -        |
| 22  | Form Fields   | v       | v      | v      | v    | v        | v        | Legend: 
`v` Available
`v*` Available only when inside header/footer
`v` Available only when inside section
`-` Not available
`?` Should be available
 TextsText can be added by using addTextandaddTextRunmethod.addTextis used for creating simple paragraphs that only contain texts with the same style.addTextRunis 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: $section->addText($text, [$fontStyle], [$paragraphStyle]);
$textrun = $section->addTextRun([$paragraphStyle]);
 You can use the $fontStyleand$paragraphStylevariable 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. Inline style examples: $fontStyle = array('name' => 'Times New Roman', 'size' => 9);
$paragraphStyle = array('align' => 'both');
$section->addText('I am simple paragraph', $fontStyle, $paragraphStyle);
$textrun = $section->addTextRun();
$textrun->addText('I am bold', array('bold' => true));
$textrun->addText('I am italic', array('italic' => true));
$textrun->addText('I am colored', array('color' => 'AACC00'));
 Defined style examples: $fontStyle = array('color' => '006699', 'size' => 18, 'bold' => true);
$phpWord->addFontStyle('fStyle', $fontStyle);
$text = $section->addText('Hello world!', 'fStyle');
$paragraphStyle = array('align' => 'center');
$phpWord->addParagraphStyle('pStyle', $paragraphStyle);
$text = $section->addText('Hello world!', 'pStyle');
 TitlesIf 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 addTitleStyleandaddTitlemethod. $phpWord->addTitleStyle($depth, [$fontStyle], [$paragraphStyle]);
$section->addTitle($text, [$depth]);
 Its necessary to add a title style to your document because otherwise the title won't be detected as a real title. LinksYou can add Hyperlinks to the document by using the function addLink: $section->addLink($linkSrc, [$linkName], [$fontStyle], [$paragraphStyle]);
 
`$linkSrc` The URL of the link.
`$linkName` Placeholder of the URL that appears in the document.
`$fontStyle` See "Font style" section.
`$paragraphStyle` See "Paragraph style" section.
 Preserve textsThe addPreserveTextmethod is used to add a page number or page count to headers or footers. $footer->addPreserveText('Page {PAGE} of {NUMPAGES}.');
 BreaksText breaksText breaks are empty new lines. To add text breaks, use the following syntax. All paramaters are optional. $section->addTextBreak([$breakCount], [$fontStyle], [$paragraphStyle]);
 
`$breakCount` How many lines
`$fontStyle` See "Font style" section.
`$paragraphStyle` See "Paragraph style" section.
 Page breaksThere are two ways to insert a page breaks, using the addPageBreakmethod or using thepageBreakBeforestyle of paragraph. $section->addPageBreak();
 ListsTo add a list item use the function addListItem. Basic usage: $section->addListItem($text, [$depth], [$fontStyle], [$listStyle], [$paragraphStyle]);
 Parameters: 
`$text` Text that appears in the document.
`$depth` Depth of list item.
`$fontStyle` See "Font style" section.
`$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.
 Advanced usage: You can also create your own numbering style by changing the $listStyleparameter with the name of your numbering style. $phpWord->addNumberingStyle(
    'multilevel',
    array('type' => 'multilevel', 'levels' => array(
        array('format' => 'decimal', 'text' => '%1.', 'left' => 360, 'hanging' => 360, 'tabPos' => 360),
        array('format' => 'upperLetter', 'text' => '%2.', 'left' => 720, 'hanging' => 360, 'tabPos' => 720),
        )
     )
);
$section->addListItem('List Item I', 0, null, 'multilevel');
$section->addListItem('List Item I.a', 1, null, 'multilevel');
$section->addListItem('List Item I.b', 1, null, 'multilevel');
$section->addListItem('List Item II', 0, null, 'multilevel');
 TablesTo add tables, rows, and cells, use the addTable,addRow, andaddCellmethods: $table = $section->addTable([$tableStyle]);
$table->addRow([$height], [$rowStyle]);
$cell = $table->addCell($width, [$cellStyle]);
 Table style can be defined with addTableStyle: $tableStyle = array(
    'borderColor' => '006699',
    'borderSize' => 6,
    'cellMargin' => 50
);
$firstRowStyle = array('bgColor' => '66BBFF');
$phpWord->addTableStyle('myTable', $tableStyle, $firstRowStyle);
$table = $section->addTable('myTable');
 Cell spanYou can span a cell on multiple columns by using gridSpanor multiple rows by usingvMerge. $cell = $table->addCell(200);
$cell->getStyle()->setGridSpan(5);
 See Sample_09_Tables.phpfor more code sample. ImagesTo add an image, use the addImagemethod to sections, headers, footers, textruns, or table cells. $section->addImage($src, [$style]);
 
source String path to a local image or URL of a remote image
styles Array fo styles for the image. See below.
 Examples: $section = $phpWord->addSection();
$section->addImage(
    'mars.jpg',
    array(
        'width' => 100,
        'height' => 100,
        'marginTop' => -1,
        'marginLeft' => -1,
        'wrappingStyle' => 'behind'
    )
);
$footer = $section->addFooter();
$footer->addImage('http://example.com/image.php');
$textrun = $section->addTextRun();
$textrun->addImage('http://php.net/logo.jpg');
 WatermarksTo add a watermark (or page background image), your section needs a header reference. After creating a header, you can use the addWatermarkmethod to add a watermark. $section = $phpWord->addSection();
$header = $section->addHeader();
$header->addWatermark('resources/_earth.jpg', array('marginTop' => 200, 'marginLeft' => 55));
 ObjectsYou can add OLE embeddings, such as Excel spreadsheets or PowerPoint presentations to the document by using addObjectmethod. $section->addObject($src, [$style]);
 Table of contentsTo add a table of contents (TOC), you can use the addTOCmethod. Your TOC can only be generated if you have add at least one title (See "Titles"). $section->addTOC([$fontStyle], [$tocStyle], [$minDepth], [$maxDepth]);
 
`$fontStyle`: See font style section
`$tocStyle`: See available options below
`$minDepth`: Minimum depth of header to be shown. Default 1
`$maxDepth`: Maximum depth of header to be shown. Default 9
 Options for $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.
 Footnotes & endnotesYou can create footnotes with addFootnoteand endnotes withaddEndnotein texts or textruns, but it's recommended to use textrun to have better layout. You can useaddText,addLink,addTextBreak,addImage,addObjecton footnotes and endnotes. On textrun: $textrun = $section->addTextRun();
$textrun->addText('Lead text.');
$footnote = $textrun->addFootnote();
$footnote->addText('Footnote text can have ');
$footnote->addLink('http://test.com', 'links');
$footnote->addText('.');
$footnote->addTextBreak();
$footnote->addText('And text break.');
$textrun->addText('Trailing text.');
$endnote = $textrun->addEndnote();
$endnote->addText('Endnote put at the end');
 On text: $section->addText('Lead text.');
$footnote = $section->addFootnote();
$footnote->addText('Footnote text.');
 The footnote reference number will be displayed with decimal number starting from 1. This number use FooterReferencestyle which you can redefine byaddFontStylemethod. Default value for this style isarray('superScript' => true); CheckboxesCheckbox elements can be added to sections or table cells by using addCheckBox. $section->addCheckBox($name, $text, [$fontStyle], [$paragraphStyle])
 
`$name` Name of the check box.
`$text` Text following the check box
`$fontStyle` See "Font style" section.
`$paragraphStyle` See "Paragraph style" section.
 TextboxesTo be completed. FieldsTo be completed. LinesTo be completed. ShapesTo be completed. ChartsTo be completed. Form fieldsTo be completed. StylesSectionBelow are the available styles for section: 
`orientation` Page orientation, i.e. 'portrait' (default) or 'landscape'
`marginTop` Page margin top in twips
`marginLeft` Page margin left in twips
`marginRight` Page margin right in twips
`marginBottom` Page margin bottom in twips
`borderTopSize` Border top size in twips
`borderTopColor` Border top color
`borderLeftSize` Border left size in twips
`borderLeftColor` Border left color
`borderRightSize` Border right size in twips
`borderRightColor` Border right color
`borderBottomSize` Border bottom size in twips
`borderBottomColor` Border bottom color
`headerHeight` Spacing to top of header
`footerHeight` Spacing to bottom of footer
`gutter` Page gutter spacing
`colsNum` Number of columns
`colsSpace` Spacing between columns
`breakType` Section break type (nextPage, nextColumn, continuous, evenPage, oddPage)
 The following two styles are automatically set by the use of the orientationstyle. You can alter them but that's not recommended. 
`pageSizeW` Page width in twips
`pageSizeH` Page height in twips
 FontAvailable font styles: 
`name` Font name, e.g. Arial
`size` Font size, e.g. 20, 22,
`hint` Font content type, default, eastAsia, or cs
`bold` Bold, true or false
`italic` Italic, true or false
`superScript` Superscript, true or false
`subScript` Subscript, true or false
`underline` Underline, dash, dotted, etc.
`strikethrough` Strikethrough, true or false
`doubleStrikethrough` Double strikethrough, true or false
`color` Font color, e.g. FF0000
`fgColor` Font highlight color, e.g. yellow, green, blue
`bgColor` Font background color, e.g. FF0000
`smallCaps` Small caps, true or false
`allCaps` All caps, true or false
 ParagraphAvailable paragraph styles: 
`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
 TableTable styles: 
`width` Table width in percent
`bgColor` Background color, e.g. '9966CC'
`border(Top|Right|Bottom|Left)Size` Border size in twips
`border(Top|Right|Bottom|Left)Color` Border color, e.g. '9966CC'
`cellMargin(Top|Right|Bottom|Left)` Cell margin in twips
 Row styles: 
`tblHeader` Repeat table row on every new page, true or false
`cantSplit` Table row cannot break across pages, true or false
`exactHeight` Row height is exact or at least
 Cell styles: 
`width` Cell width in twips
`valign` Vertical alignment, top, center, both, bottom
`textDirection` Direction of text
`bgColor` Background color, e.g. '9966CC'
`border(Top|Right|Bottom|Left)Size` Border size in twips
`border(Top|Right|Bottom|Left)Color` Border color, e.g. '9966CC'
`gridSpan` Number of columns spanned
`vMerge` restart or continue
 ImageAvailable image styles: 
`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` Wrapping style, inline, square, tight, behind, or infront
 Numbering level
`start` Starting value
`format` Numbering format bullet|decimal|upperRoman|lowerRoman|upperLetter|lowerLetter
`restart` Restart numbering level symbol
`suffix` Content between numbering symbol and paragraph text tab|space|nothing
`text` Numbering level text e.g. %1 for nonbullet or bullet character
`align` Numbering symbol align left|center|right|both
`left` See paragraph style
`hanging` See paragraph style
`tabPos` See paragraph style
`font` Font name
`hint` See font style
 Templates processingYou can create a .docx document template with included search-patterns which can be replaced by any value you wish. Only single-line values can be replaced. To deal with a template file, use new TemplateProcessorstatement. After TemplateProcessor instance creation the document template is copied into the temporary directory. Then you can useTemplateProcessor::setValuemethod to change the value of a search pattern. The search-pattern model is:${search-pattern}. Example: $templateProcessor = new TemplateProcessor('Template.docx');
$templateProcessor->setValue('Name', 'Somebody someone');
$templateProcessor->setValue('Street', 'Coming-Undone-Street 32');
 It is not possible to directly add new OOXML elements to the template file being processed, but it is possible to transform main document part of the template using XSLT (see TemplateProcessor::applyXslStyleSheet). See Sample_07_TemplateCloneRow.phpfor example on how to create multirow from a single row in a template by usingTemplateProcessor::cloneRow. See Sample_23_TemplateBlock.phpfor example on how to clone a block of text usingTemplateProcessor::cloneBlockand delete a block of text usingTemplateProcessor::deleteBlock. Writers & readersOOXMLThe package of OOXML document consists of the following files. 
_rels/
- .rels
docProps/
- app.xml
- core.xml
- custom.xml
word/
- rels/
    - document.rels.xml
- media/
- theme/
    - theme1.xml
- document.xml
- fontTable.xml
- numbering.xml
- settings.xml
- styles.xml
- webSettings.xml
[Content_Types].xml
 OpenDocumentPackageThe package of OpenDocument document consists of the following files. 
META-INF/
- manifest.xml
Pictures/
content.xml
meta.xml
styles.xml
 content.xmlThe structure of content.xmlis described below. 
office:document-content
- office:font-facedecls
- office:automatic-styles
- office:body
    - office:text
        - draw:*
        - office:forms
        - table:table
        - text:list
        - text:numbered-paragraph
        - text:p
        - text:table-of-contents
        - text:section
    - office:chart
    - office:image
    - office:drawing
 styles.xmlThe structure of styles.xmlis described below. 
office:document-styles
- office:styles
- office:automatic-styles
- office:master-styles
    - office:master-page
 RTFTo be completed. HTMLTo be completed. PDFTo be completed. RecipesCreate float left imageUse absolute positioning relative to margin horizontally and to line vertically. $imageStyle = array(
    'width' => 40,
    'height' => 40
    'wrappingStyle' => 'square',
    'positioning' => 'absolute',
    'posHorizontalRel' => 'margin',
    'posVerticalRel' => 'line',
);
$textrun->addImage('resources/_earth.jpg', $imageStyle);
$textrun->addText($lipsumText);
 Download the produced file automaticallyUse php://outputas the filename. $phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->createSection();
$section->addText('Hello World!');
$file = 'HelloWorld.docx';
header("Content-Description: File Transfer");
header('Content-Disposition: attachment; filename="' . $file . '"');
header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Expires: 0');
$xmlWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
$xmlWriter->save("php://output");
 Create numbered headingsDefine a numbering style and title styles, and match the two styles (with pStyleandnumStyle) like below. $phpWord->addNumberingStyle(
    'hNum',
    array('type' => 'multilevel', 'levels' => array(
        array('pStyle' => 'Heading1', 'format' => 'decimal', 'text' => '%1'),
        array('pStyle' => 'Heading2', 'format' => 'decimal', 'text' => '%1.%2'),
        array('pStyle' => 'Heading3', 'format' => 'decimal', 'text' => '%1.%2.%3'),
        )
    )
);
$phpWord->addTitleStyle(1, array('size' => 16), array('numStyle' => 'hNum', 'numLevel' => 0));
$phpWord->addTitleStyle(2, array('size' => 14), array('numStyle' => 'hNum', 'numLevel' => 1));
$phpWord->addTitleStyle(3, array('size' => 12), array('numStyle' => 'hNum', 'numLevel' => 2));
$section->addTitle('Heading 1', 1);
$section->addTitle('Heading 2', 2);
$section->addTitle('Heading 3', 3);
 Add a link within a titleApply 'HeadingN' paragraph style to TextRun or Link. Sample code: $phpWord = new \PhpOffice\PhpWord\PhpWord();
$phpWord->addTitleStyle(1, array('size' => 16, 'bold' => true));
$phpWord->addTitleStyle(2, array('size' => 14, 'bold' => true));
$phpWord->addFontStyle('Link', array('color' => '0000FF', 'underline' => 'single'));
$section = $phpWord->addSection();
// Textrun
$textrun = $section->addTextRun('Heading1');
$textrun->addText('The ');
$textrun->addLink('https://github.com/PHPOffice/PHPWord', 'PHPWord', 'Link');
// Link
$section->addLink('https://github.com/', 'GitHub', 'Link', 'Heading2');
 Remove [Compatibility Mode] text in the MS Word title barUse the Metadata\Compatibility\setOoxmlVersion(n)method withnis the version of Office (14 = Office 2010, 15 = Office 2013). $phpWord->getCompatibility()->setOoxmlVersion(15);
 Frequently asked questionsIs this the same with PHPWord that I found in CodePlex?No. This one is much better with tons of new features that you can’t find in PHPWord 0.6.3. The development in CodePlex is halted and switched to GitHub to allow more participation from the crowd. The more the merrier, right? I’ve been running PHPWord from CodePlex flawlessly, but I can’t use the latest PHPWord from GitHub. Why?PHPWord requires PHP 5.3+ since 0.8, while PHPWord 0.6.3 from CodePlex can run with PHP 5.2. There’s a lot of new features that we can get from PHP 5.3 and it’s been around since 2009! You should upgrade your PHP version to use PHPWord 0.8+. ReferencesISO/IEC 29500, Third edition, 2012-09-01Formal specificationsOther resources |