All formats

PHPWord

LanguagePHP (7.1+ / 8.x)
LicenseLGPL-3.0-only
Repositorygithub.com/PHPOffice/PHPWord
Documentationphpoffice.github.io/PHPWord
Depends onext-dom, ext-gd, ext-json, ext-xml, ext-zip, phpoffice/math
Read / writereads RTF (text, paragraphs, bold/italic/underline/strike, font size, centering); writes RTF with text styles, paragraphs, lists, tables, images, links, page breaks

PHPWord builds documents from an object model (PhpWord → sections → text, text runs, lists, tables, images, links) and saves them with a writer: Word2007 (DOCX), ODText, RTF, HTML or PDF. Readers load DOCX, DOC, ODT, HTML and RTF into the same model, so you can convert between them.

The RTF writer covers common formatting. The RTF reader is basic: it keeps text, paragraphs and simple character formatting and skips fonts, colors, pictures and fields. For complex RTF input, convert with LibreOffice first. LGPL-3.0 allows use in proprietary applications as long as the library itself stays replaceable.

Installation#

Shell
composer require phpoffice/phpword

Basic usage: write an RTF document#

PHP
<?php
require 'vendor/autoload.php';

use PhpOffice\PhpWord\IOFactory;
use PhpOffice\PhpWord\PhpWord;

$phpWord = new PhpWord();
$phpWord->setDefaultFontName('Arial');
$phpWord->setDefaultFontSize(11);

$section = $phpWord->addSection();
$section->addTitle('Invoice 42', 1);
$section->addText('Thank you for your order.');
$section->addText('Total: 120.00 EUR', ['bold' => true, 'color' => 'C00000']);

$run = $section->addTextRun();
$run->addText('Questions? ');
$run->addLink('https://example.com/support', 'Contact support');

IOFactory::createWriter($phpWord, 'RTF')->save('invoice.rtf');

How to add a table#

PHP
$table = $section->addTable(['borderSize' => 6, 'borderColor' => '999999']);
foreach ([['Item', 'Qty'], ['Paper', '2'], ['Pens', '10']] as $row) {
    $table->addRow();
    foreach ($row as $cell) {
        $table->addCell(3000)->addText($cell);   // width in twips
    }
}

How to read an RTF file and get its text#

PHP
use PhpOffice\PhpWord\Element\Text;
use PhpOffice\PhpWord\Element\TextRun;
use PhpOffice\PhpWord\IOFactory;

$phpWord = IOFactory::load('letter.rtf', 'RTF');

$lines = [];
foreach ($phpWord->getSections() as $section) {
    foreach ($section->getElements() as $element) {
        if ($element instanceof TextRun) {
            $line = '';
            foreach ($element->getElements() as $child) {
                if ($child instanceof Text) {
                    $line .= $child->getText();
                }
            }
            $lines[] = $line;
        } elseif ($element instanceof Text) {
            $lines[] = $element->getText();
        }
    }
}
echo implode("\n", $lines);

How to convert RTF to DOCX or HTML#

PHP
$phpWord = IOFactory::load('letter.rtf', 'RTF');
IOFactory::createWriter($phpWord, 'Word2007')->save('letter.docx');
IOFactory::createWriter($phpWord, 'HTML')->save('letter.html');

How to convert DOCX to RTF#

PHP
$phpWord = IOFactory::load('report.docx');          // Word2007 reader is the default
IOFactory::createWriter($phpWord, 'RTF')->save('report.rtf');

Tips#