dskripchenko / php-docx-phpword
Typed bridge between PHPWord and dskripchenko/php-docx: export PhpWord objects to clean HTML, import HTML into PhpWord, read arbitrary DOCX straight into PhpWord models, detect template variables. The HTML layer for PHPWord pipelines.
Requires
- php: ^8.2
- dskripchenko/php-docx: ^1.1
- phpoffice/phpword: ^1.3
Requires (Dev)
- phpstan/phpstan: ^2.0
- phpunit/phpunit: ^11.5
README
The HTML layer for PHPWord pipelines. A typed bridge between PHPWord's object model and php-docx's AST — export
PhpWordobjects to clean HTML (the export PHPWord doesn't offer), import HTML intoPhpWord, read arbitrary DOCX straight into PHPWord models, detect template variables. Keep working in the API you know.
Installation
composer require dskripchenko/php-docx-phpword
Brings dskripchenko/php-docx (MIT, zero-dep) next to your existing
phpoffice/phpword. This package is MIT; PHPWord stays LGPL-3.0 — a
regular Composer dependency, nothing about your code changes.
What you get
PhpWord → HTML (the killer feature)
use Dskripchenko\PhpDocxPhpWord\PhpWordBridge; $html = PhpWordBridge::toHtml($phpWord); // clean HTML, inline styles
HTML → PhpWord
$phpWord = PhpWordBridge::fromHtml('<h1>Report</h1><p>Body with <b>bold</b>.</p>'); // … continue with PHPWord-native calls and writers
Read any DOCX into PhpWord objects
$phpWord = PhpWordBridge::readToPhpWord(file_get_contents('from-word.docx'));
Uses php-docx's reader (full style cascade, Word / Google Docs / LibreOffice / PHPWord output — fidelity is tracked on an external corpus), then maps into PHPWord models. PHPWord's own reader is not involved.
Detect template variables
$variables = PhpWordBridge::detectVariables($phpWordOrDocxBytes); // MERGEFIELD, SDT content controls, {{x}} / ${x} / %x% patterns
AST escape hatches
PhpWordBridge::toDocument($phpWord) and
PhpWordBridge::fromDocument($document) expose the underlying mapping
for pipelines that continue in php-docx.
The shared element set (the honest boundary)
The bridge maps what both models can express:
| Carried across | Not carried (skipped, never guessed) |
|---|---|
| paragraphs, text runs (bold, italic, underline, strikethrough, super/subscript, colour, size, font name) | named/theme styles (resolved values only) |
| headings 1–6 | charts, TOC, checkboxes, OLE |
tables incl. gridSpan / vMerge |
tracked changes, comments |
| bullet / numbered lists (nested) | footnotes/endnotes, OMML math |
| hyperlinks, images, line/page breaks | form fields |
| default headers/footers | first/even header variants, watermarks |
| paragraph alignment, spacing, indents | section/page geometry details |
If a document leans on the right-hand column, the bridge will not pretend otherwise — content outside the set is dropped silently by design. For full-fidelity DOCX round-trips stay in php-docx itself.
License
MIT. Depends on phpoffice/phpword (LGPL-3.0) via Composer — the
standard dynamic-linking case the LGPL is written for.