Library / SDK
PHPOffice/PHPWord avatar
PHPOffice/PHPWord

PHPWord: Generate and Read Word Documents from PHP

A pure PHP library for reading and writing word processing documents

7,592 stars2,736 forksPHPNOASSERTION

At a glance

What is it?
A pure PHP library for creating and reading Word documents (OOXML), OpenDocument, RTF, HTML, and PDF files. No external dependencies beyond standard PHP extensions.
Who is it for?
PHPWord is for PHP developers who need to generate or read Word documents. Adopt it if you build PHP applications that export reports, invoices, or documents to Word format.
Can I use it commercially?
Check first. The repository uses a licence we do not classify automatically, so read its LICENSE file before any commercial use.
Is it still maintained?
Yes. The repository last received commits 2 days ago.
What is it written in?
Mainly PHP, according to GitHub's language statistics.

Answers come from the project's GitHub data, last synced on September 29, 2026, and from our analysis. They are not legal advice.

Editorial analysis

Programmatic Word Document Generation from PHP

PHPWord generates Word documents dynamically from PHP code. It provides classes to write to and read from different document file formats: Microsoft Office Open XML (OOXML, the native Word format), OpenDocument Format (ODF), Rich Text Format (RTF), HTML, and PDF. This is for developers building reporting systems, invoice generators, or document export features. Instead of manually assembling Word files or relying on external binary tools, you programmatically create documents in PHP. The library is pure PHP; it does not depend on Microsoft Word or LibreOffice being installed. This makes PHPWord suitable for server-side document generation in web applications, batch processing scripts, or any PHP environment. You control the document structure and content entirely through code, allowing for dynamic personalization, data-driven layouts, and automated mass document creation.

Creating Documents

PHPWord uses a PHP object model. Create a new document, add sections, and insert content. The basic pattern is:

php
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection();
$section->addText('Example text');

Documents must contain sections; all content goes inside a section. You set document properties (title, subject, creator), define sections with different page layouts (portrait/landscape, page size, page numbering), add headers and footers, and insert content. You can customize font styles in multiple ways. Complex paragraphs (called "text runs") can contain nested elements. The library abstracts away the underlying XML complexity of Word formats: you call PHP methods, and PHPWord handles serialization to OOXML or ODF format.

Tables, Images, Shapes, and Charts

PHPWord supports a large range of document elements. Supported elements include: custom font styles (bold, italic, color), paragraph styles (centered, multi-column, spacing), text breaks and page breaks, images (local, remote, or watermarks), tables with row and cell customization (including repeat-as-header-row for multi-page tables, background colors, rowspan, colspan), bulleted and numbered lists at multiple levels, hyperlinks, footnotes and endnotes, drawing shapes (arc, curve, line, polyline, rect, oval), and charts (pie, doughnut, bar, line, area, scatter, radar). Form fields (textinput, checkbox, dropdown) are supported. Templates and XSL transformation are available for advanced use cases: you can create a template in Word, then use PHPWord to clone and customize rows or blocks dynamically. Document sections can have different settings: page size, orientation, margins, and page numbering. East Asian font support is included, so you can use UTF-8 characters and non-Latin scripts.

Installation and Requirements

PHPWord requires PHP 7.1 or later. Installation is via Composer:

bash
composer require phpoffice/phpword

To use the development version:

bash
composer require phpoffice/phpword:dev-master

The README lists required dependencies: XML Parser extension and Laminas Escaper component. Optional extensions enable additional features: Zip extension for writing OOXML and ODF documents, GD extension for adding and manipulating images, XMLWriter extension for OOXML and ODF serialization, XSL extension for applying XSL 1.0 style sheets to templates, and dompdf library for PDF output. Omitting optional extensions disables their corresponding features; for instance, without the Zip extension, OOXML and ODF output is not available.

The repository includes more than two dozen sample files demonstrating different features. Run `php -S localhost:8000` in the samples directory and browse to http://localhost:8000 to view runnable examples. Sample files cover basic text, tab stops, sections, text runs, multi-column layouts, footnotes, template cloning, table row pagination, tables, East Asian fonts, reading Word 2007 and Word 97 formats, headers and footers, images, lists, links, OLE objects, table of contents, watermarks, text breaks, background colors, table row rules, checkboxes, template blocks, and reading ODF documents.

Reading and Templates

PHPWord can read existing Word documents. The samples folder includes Sample_11_ReadWord2007.php for reading Word 2007 format and Sample_11_ReadWord97.php for reading Word 97 format, demonstrating both modern and legacy document reading. The library supports creating documents from templates using TemplateProcessor, which clones rows or blocks dynamically from an existing document template. This is demonstrated in Sample_07_TemplateCloneRow.php and Sample_23_TemplateBlock.php.

XSL 1.0 style sheets are supported for transforming headers, main document parts, and footers within OOXML templates. This enables sophisticated document assembly workflows where a base template is transformed by applying an XSL style sheet that modifies structure and content without requiring manual document manipulation. ODF (OpenDocument Format) documents can also be read; Sample_24_ReadODText.php shows the pattern.

Maintenance Status

PHPWord is actively maintained. The last push was 2026-09-28. The most recent release is 1.4.0 (2025-06-05); earlier releases are 1.3.0 (2024-09-01) and 1.2.0 (2023-12-28). The project has continuous integration and unit testing. The project uses Packagist for distribution and includes coverage reporting via Coveralls. The project accepts contributions; the README links to a CONTRIBUTING.md file and encourages bug reports and feature requests on GitHub.

When PHPWord Is Insufficient

PHPWord is document generation, not a full editor. It cannot handle complex existing documents with embedded media or advanced layouts that exceed the library's feature set. Reading support is basic; complex Word documents with intricate formatting, embedded media, or macros may not round-trip through PHPWord without data loss. Conversion of arbitrary HTML to Word format is not supported; HTML is one of the output formats for exporting generated documents, not a reliable input format for importing HTML pages into Word. The library writes binary OLE objects such as embedded Excel spreadsheets, but does not support reading them back from an existing document.

If you need to programmatically edit existing Word files with complex macros, form field automation beyond textinput, checkbox, and dropdown support, or sophisticated template logic, PHPWord may not cover all cases. The library's PDF output depends on dompdf for rendering; if you need different PDF rendering engines or advanced PDF features like forms or signatures, this is a limitation. Word 97 format is supported for reading only; new documents cannot be created in the Word 97 format, only in OOXML, ODF, RTF, HTML, or PDF.

Editorial conclusion

PHPWord is for PHP developers who need to generate or read Word documents. Adopt it if you build PHP applications that export reports, invoices, or documents to Word format. The library is actively maintained and has comprehensive sample code. Skip it if you need advanced features or editing that go beyond document generation. Verify that the required PHP extensions (XML Parser, Zip, XMLWriter) are installed and enabled. For PDF output, verify that dompdf is compatible with your environment.

Frequently asked questions

How do I install PHPWord?

PHPWord is installed via Composer: `composer require phpoffice/phpword`. It requires PHP 7.1 or later and the XML Parser extension.

How do I use PHPWord?

Create a new PhpWord object, add a section, and insert content: `$phpWord = new \PhpOffice\PhpWord\PhpWord()`, then `$section = $phpWord->addSection()`, then `$section->addText()`. The README provides extensive examples in the samples folder.

What file formats can PHPWord create?

PHPWord can create Microsoft Office Open XML (OOXML/Word), OpenDocument Format (ODF), Rich Text Format (RTF), HTML, and PDF files.

Can PHPWord read Word documents?

Yes. PHPWord can read existing Word documents. The samples folder includes examples for reading Word 2007 and Word 97 formats.

What is PHPWord's license?

PHPWord is licensed under LGPL version 3, an open-source license that allows commercial use and modification.

Official sources

  1. Issues
  2. PHPOffice/PHPWord on GitHub
  3. Project website
  4. README
  5. Releases
Add this badge to your README

If you maintain this project, the badge below links readers to this analysis and shows its maintenance status from the daily GitHub snapshot. Paste the markdown into your README; add ?metric=license or ?metric=stars to the image URL for a different field.

Add this badge to your README

markdown
[![Hysen Labs](https://hysenlabs.com/badge/phpoffice-phpword.svg)](https://hysenlabs.com/projects/phpoffice-phpword)