PHPWord
| Language | PHP (7.1+ / 8.x) |
|---|---|
| License | LGPL-3.0-only |
| Repository | github.com/PHPOffice/PHPWord |
| Documentation | phpoffice.github.io/PHPWord |
| Depends on | ext-dom, ext-gd, ext-json, ext-xml, ext-zip, phpoffice/math |
| Read / write | reads 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#
- RTF output is ASCII with
\uNescapes, so non-Latin text works without setting encodings. IOFactory::load($path, 'RTF')needs the reader name; without it PHPWord assumes DOCX.- Use template processing (
TemplateProcessor) for DOCX templates; there’s no RTF equivalent, but RTF templates can be filled withstr_replaceon placeholders that contain no RTF markup.