2014-05-02 14:44:32 -04:00
# 
2014-05-06 13:04:18 +02:00
[](https://packagist.org/packages/phpoffice/phpword)
2022-11-16 22:35:01 +01:00
[](https://github.com/PHPOffice/PHPWord/actions/workflows/ci.yml)
2018-12-29 23:17:17 +01:00
[](https://scrutinizer-ci.com/g/PHPOffice/PHPWord/)
2022-11-16 22:35:01 +01:00
[](https://scrutinizer-ci.com/g/PHPOffice/PHPWord/)
2014-05-02 14:44:32 -04:00
[](https://packagist.org/packages/phpoffice/phpword)
2014-05-06 13:04:18 +02:00
[](https://packagist.org/packages/phpoffice/phpword)
2015-05-26 22:23:19 +04:00
[](https://gitter.im/PHPOffice/PHPWord)
2014-05-02 14:44:32 -04:00
2014-05-05 22:31:29 +07:00
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), [Rich Text Format ](http://en.wikipedia.org/wiki/Rich_Text_Format ) (RTF), HTML, and PDF.
2022-11-16 22:27:10 +01:00
PHPWord is an open source project licensed under the terms of [LGPL version 3 ](COPYING.LESSER ). PHPWord is aimed to be a high quality software product by incorporating [continuous integration ](https://github.com/PHPOffice/PHPWord/actions ) and unit testing. You can learn more about PHPWord by reading the [Developers' Documentation ](http://phpword.readthedocs.org/ ).
2014-05-05 22:31:29 +07:00
2017-07-05 21:39:22 +02:00
If you have any questions, please ask on [StackOverFlow ](https://stackoverflow.com/questions/tagged/phpword )
2014-06-16 07:32:57 +07:00
Read more about PHPWord:
- [Features ](#features )
- [Requirements ](#requirements )
- [Installation ](#installation )
- [Getting started ](#getting-started )
- [Contributing ](#contributing )
- [Developers' Documentation ](http://phpword.readthedocs.org/ )
2014-05-05 22:31:29 +07:00
## Features
2014-05-02 14:44:32 -04:00
2022-09-16 10:17:31 +02:00
With PHPWord, you can create OOXML, ODF, or RTF documents dynamically using your PHP scripts. Below are some of the things that you can do with PHPWord library:
2014-05-02 14:44:32 -04:00
2014-05-05 22:31:29 +07:00
- 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
2014-06-12 18:15:00 +07:00
- Insert drawing shapes (arc, curve, line, polyline, rect, oval)
- Insert charts (pie, doughnut, bar, line, area, scatter, radar)
2014-06-16 00:09:14 +07:00
- Insert form fields (textinput, checkbox, and dropdown)
2014-05-05 22:31:29 +07:00
- Create document from templates
2016-07-30 16:02:23 +04:00
- Use XSL 1.0 style sheets to transform headers, main document part, and footers of an OOXML template
2014-05-05 22:31:29 +07:00
- ... and many more features on progress
2014-05-02 14:44:32 -04:00
## Requirements
2014-05-05 22:31:29 +07:00
PHPWord requires the following:
2022-10-24 11:08:15 +02:00
- PHP 7.1+
2014-05-05 22:31:29 +07:00
- [XML Parser extension ](http://www.php.net/manual/en/xml.installation.php )
2020-10-18 13:16:43 +02:00
- [Laminas Escaper component ](https://docs.laminas.dev/laminas-escaper/intro/ )
2016-06-28 21:37:36 +04:00
- [Zip extension ](http://php.net/manual/en/book.zip.php ) (optional, used to write OOXML and ODF)
2014-05-05 22:31:29 +07:00
- [GD extension ](http://php.net/manual/en/book.image.php ) (optional, used to add images)
2016-06-28 21:37:36 +04:00
- [XMLWriter extension ](http://php.net/manual/en/book.xmlwriter.php ) (optional, used to write OOXML and ODF)
2014-05-05 22:31:29 +07:00
- [XSL extension ](http://php.net/manual/en/book.xsl.php ) (optional, used to apply XSL style sheet to template )
2015-11-14 17:34:30 +04:00
- [dompdf library ](https://github.com/dompdf/dompdf ) (optional, used to write PDF)
2014-05-02 14:44:32 -04:00
## Installation
2015-11-14 17:24:36 +04:00
PHPWord is installed via [Composer ](https://getcomposer.org/ ).
2019-07-09 17:22:30 +02:00
To [add a dependency ](https://getcomposer.org/doc/04-schema.md#package-links ) to PHPWord in your project, either
2015-11-14 17:24:36 +04:00
2017-12-05 00:14:57 +01:00
Run the following to use the latest stable version
```sh
2022-09-16 14:23:58 +02:00
composer require phpoffice/phpword
2017-12-05 00:14:57 +01:00
```
2022-11-16 22:27:10 +01:00
or if you want the latest unreleased version
2017-12-05 00:14:57 +01:00
```sh
2022-11-16 22:27:10 +01:00
composer require phpoffice/phpword:dev-master
2014-05-02 14:44:32 -04:00
```
2014-06-08 22:38:50 +07:00
## Getting started
2014-05-02 14:44:32 -04:00
2014-06-08 22:38:50 +07:00
The following is a basic usage example of the PHPWord library.
2014-05-02 14:44:32 -04:00
```php
2014-12-09 21:35:26 +04:00
< ?php
2015-11-14 17:24:36 +04:00
require_once 'bootstrap.php';
2014-06-08 22:38:50 +07:00
2014-12-09 21:35:26 +04:00
// Creating the new document...
2014-05-02 14:44:32 -04:00
$phpWord = new \PhpOffice\PhpWord\PhpWord();
2014-12-09 21:35:26 +04:00
/* Note: any element you append to a document must reside inside of a Section. */
// Adding an empty Section to the document...
2014-05-02 14:44:32 -04:00
$section = $phpWord->addSection();
2014-12-09 21:35:26 +04:00
// Adding Text element to the Section having font styled by default...
$section->addText(
2016-06-04 20:06:37 +04:00
'"Learn from yesterday, live for today, hope for tomorrow. '
. 'The important thing is not to stop questioning." '
. '(Albert Einstein)'
2014-12-09 21:35:26 +04:00
);
/*
2014-12-09 21:46:19 +04:00
* Note: it's possible to customize font style of the Text element you add in three ways:
2014-12-09 21:35:26 +04:00
* - inline;
* - using named font style (new font style object will be implicitly created);
* - using explicitly created font style object.
*/
2014-12-09 21:52:17 +04:00
// Adding Text element with font customized inline...
2014-12-09 21:35:26 +04:00
$section->addText(
2016-06-04 20:06:37 +04:00
'"Great achievement is usually born of great sacrifice, '
. 'and is never the result of selfishness." '
. '(Napoleon Hill)',
2014-12-09 21:35:26 +04:00
array('name' => 'Tahoma', 'size' => 10)
);
2014-05-02 14:44:32 -04:00
2014-12-09 21:52:17 +04:00
// Adding Text element with font customized using named font style...
2014-12-09 21:35:26 +04:00
$fontStyleName = 'oneUserDefinedStyle';
2014-12-09 21:46:19 +04:00
$phpWord->addFontStyle(
$fontStyleName,
array('name' => 'Tahoma', 'size' => 10, 'color' => '1B2232', 'bold' => true)
);
2014-12-09 21:35:26 +04:00
$section->addText(
2016-06-04 20:06:37 +04:00
'"The greatest accomplishment is not in never falling, '
. 'but in rising again after you fall." '
. '(Vince Lombardi)',
2014-12-09 21:35:26 +04:00
$fontStyleName
2014-06-08 22:38:50 +07:00
);
2014-12-09 21:35:26 +04:00
2014-12-09 21:52:17 +04:00
// Adding Text element with font customized using explicitly created font style object...
2014-12-09 21:35:26 +04:00
$fontStyle = new \PhpOffice\PhpWord\Style\Font();
$fontStyle->setBold(true);
$fontStyle->setName('Tahoma');
$fontStyle->setSize(13);
2016-06-04 20:06:37 +04:00
$myTextElement = $section->addText('"Believe you can and you\'re halfway there." (Theodor Roosevelt)');
2014-05-02 14:44:32 -04:00
$myTextElement->setFontStyle($fontStyle);
2014-12-09 21:35:26 +04:00
// 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');
2014-12-09 21:52:17 +04:00
/* 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. */
2014-05-02 14:44:32 -04:00
```
2014-11-23 12:51:10 +04:00
2017-12-29 14:42:48 +01:00
More examples are provided in the [samples folder ](samples/ ). For an easy access to those samples launch `php -S localhost:8000` in the samples directory then browse to [http://localhost:8000 ](http://localhost:8000 ) to view the samples.
2018-12-03 16:09:20 +01:00
You can also read the [Developers' Documentation ](http://phpword.readthedocs.org/ ) for more detail.
2014-06-08 22:38:50 +07:00
2014-05-05 22:31:29 +07:00
## Contributing
2014-11-23 12:38:18 +04:00
We welcome everyone to contribute to PHPWord. Below are some of the things that you can do to contribute.
2014-05-02 14:44:32 -04:00
2019-07-11 17:14:35 -04:00
- Read [our contributing guide ](CONTRIBUTING.md ).
2022-11-16 22:27:10 +01:00
- [Fork us ](https://github.com/PHPOffice/PHPWord/fork ) and [request a pull ](https://github.com/PHPOffice/PHPWord/pulls ) to the [master ](https://github.com/PHPOffice/PHPWord/tree/master ) branch.
2014-11-23 12:38:18 +04:00
- Submit [bug reports or feature requests ](https://github.com/PHPOffice/PHPWord/issues ) to GitHub.
- Follow [@PHPWord ](https://twitter.com/PHPWord ) and [@PHPOffice ](https://twitter.com/PHPOffice ) on Twitter.