Download the PHP package dskripchenko/php-docx without Composer
On this page you can find all versions of the php package dskripchenko/php-docx. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download dskripchenko/php-docx
More information about dskripchenko/php-docx
Files in dskripchenko/php-docx
Package php-docx
Short Description Pure-PHP DOCX (OOXML) library: bidirectional HTML ↔ DOCX conversion, full styles cascade, tables with vMerge/gridSpan, lists, images, headers/footers, watermarks, fields, bookmarks. No external dependencies.
License MIT
Informations about the package php-docx
dskripchenko/php-docx
Pure-PHP DOCX (Office Open XML) library: bidirectional HTML ↔ DOCX conversion, fluent programmatic builder, variable detection, round-trip-safe AST. No external dependencies beyond standard PHP extensions.
Read this in other languages: English · Русский · 中文 · Deutsch
Table of contents
- Features
- Requirements
- Installation
- Quick start
- HTML → DOCX
- Programmatic builder
- DOCX → HTML / AST
- HTML → DOCX
- Programmatic builder API
- DOCX → HTML (Reader)
- Headers, footers & watermarks
- Variable detection
- Length helpers
- AST overview
- Round-trip
- Architecture
- Development
- License
Features
- HTML → DOCX writer — full set of typical layout elements (paragraphs/headings/tables/lists/images/links/fields), inline-style resolution, custom heading registry.
- DOCX → HTML reader — parses arbitrary Word/Pages/LibreOffice documents into a typed AST, then serialises back to HTML with inline styles. Style cascade (docDefaults → named → direct), theme colors, numbering reconstruction, vMerge/gridSpan collapse, watermark detection (VML + DrawingML).
- Fluent programmatic builder —
DocumentBuilderwith closure scopes for nested structures (tables, lists, headers). - Variable detection — MERGEFIELD, SDT content controls, configurable
text patterns (
{{x}},${x},%x%). - Multi-header/footer — default / first-page / even-pages variants
with automatic
<w:titlePg/>and<w:evenAndOddHeaders/>plumbing. - Round-trip safe — read DOCX → AST → write DOCX produces a valid document; bytes-level differences are limited to whitespace/ordering.
- PHP 8.2+ —
readonlyvalue-objects, named arguments, constructor promotion, enums. - Zero composer dependencies.
Out of scope
Tracked changes, comments, embedded charts, OLE objects, footnotes/endnotes, SmartArt, math equations (OMML), form fields, custom XML parts.
Requirements
- PHP 8.2+
ext-zip,ext-dom,ext-mbstring
Installation
Quick start
1. HTML → DOCX
2. Programmatic builder
3. DOCX → HTML / AST
HTML → DOCX
Input HTML must use inline styles only (no <style> blocks). Use a
CSS-inliner upstream if needed.
Supported elements
| Category | HTML tags |
|---|---|
| Text blocks | <p>, <h1..h6>, <div>, <pre>, <blockquote> |
| Inline marks | <strong>/<b>, <em>/<i>, <u>, <s>/<del>, <sup>, <sub>, <mark> |
| Code/teletype | <code>, <kbd>, <samp>, <var>, <cite>, <dfn>, <q>, <small> |
| Links | <a href> external, <a href="#anchor"> internal, <a id> bookmarks |
| Images | <img src="data:image/...;base64,..."> |
| Tables | <table>, <thead>/<tbody>, <tr>, <th>/<td>, <colgroup>/<col>, <caption>, colspan, rowspan |
| Lists | <ul>, <ol type="a/A/i/I" start="N">, <li value="N">, <dl>/<dt>/<dd> |
| Custom tags | <page-number/>, <page-total/>, <current-date format="...">, <page-break> |
| Layout | <hr>, <br>, <figure>/<figcaption> |
Inline styles
The converter understands style="…" properties:
- Run-level:
font-family,font-size,font-weight,font-style,text-decoration,color,background-color - Paragraph-level:
text-align,margin,text-indent,line-height,border,padding - Table-level:
width,border,border-collapse - Cell-level:
width,padding,border,vertical-align,background-color
Custom tags
These become OOXML field codes (<w:fldSimple w:instr="PAGE">).
Custom heading styles
Programmatic builder API
The Build namespace provides a fluent API for assembling DOCX
documents block by block, finalising to the same immutable AST that the
HTML pipeline produces.
DocumentBuilder
Entry point. Accumulates body, header/footer, watermark, page setup.
ParagraphBuilder
Inside ->paragraph(fn ($p) => …):
Paragraph-level styling:
TableBuilder
Spans and merges:
ListBuilder
RunStyleBuilder
Used inside ->styled(text, fn (RunStyleBuilder) => …) or standalone via
RunStyleBuilder::new()->…->build().
Length helpers
Convert common units to OOXML twips (1 twip = 1/20 pt). Used wherever a twip int is expected.
Most builders expose unit-aware shortcuts:
- TableBuilder:
widthPt/widthMm/widthCm/widthInches,cellMarginsMm/cellMarginsPt - TableCellBuilder:
widthPt/Mm/Cm/Inches,paddingMm/Pt/Cm/Inches - ColumnBuilder:
widthPt/Mm/Cm/Inches/Px - ParagraphBuilder:
indentMm/Cm/Pt/Inches,spacingPt/Mm - RunStyleBuilder:
fontSizePt
DOCX → HTML (Reader)
High-level: DocxReader
This runs the full pipeline: package unpack → styles resolve → body/header/footer parsing → vMerge/list reconstruction → image extraction → watermark detection → page setup.
Low-level: DocxPackageReader
If you need the raw OOXML parts:
Serializer: AST → HTML
HTML output uses inline styles only — re-loadable into the same library
via Html\Converter::fromHtml($imported->bodyHtml).
Headers, footers & watermarks
Three header/footer types are supported per section: default, first
(title page), even (even pages). Word automatically renders the right
one based on page number.
The writer automatically:
- emits
<w:titlePg/>insectPrwhen first-page header/footer is set - emits
word/settings.xmlwith<w:evenAndOddHeaders/>when even header/footer is set
Watermark
Renders as a 45°-rotated VML text shape on every page.
Variable detection
Scans an imported DOCX for three kinds of variables:
- MERGEFIELD — Word mail-merge native, both simple
<w:fldSimple>and complex<w:fldChar>form. - SDT content controls —
<w:sdt>with<w:tag w:val="...">. - Text patterns — configurable regexes (defaults:
{{name}},${name},%name%).
Detection runs across body + all headers + all footers. Results are
deduplicated by (source, name).
Length helpers
See Length helpers above. Conversion table:
| Unit | Twips | Pt | Notes |
|---|---|---|---|
| 1 twip | 1 | 0.05 | OOXML native |
| 1 pt | 20 | 1 | typography |
| 1 mm | ~57 | 2.83 | metric |
| 1 cm | ~567 | 28.35 | metric |
| 1 inch | 1440 | 72 | imperial |
| 1 px | 15 | 0.75 | CSS @ 96 DPI |
AST overview
All elements live under Dskripchenko\PhpDocx\Element namespace.
| Element | Type | Notes |
|---|---|---|
Document |
root | { section: Section, watermarkText: ?string } |
Section |
container | { body, header, footer, pageSetup, firstHeader, firstFooter, evenHeader, evenFooter } |
Paragraph |
BlockElement | { children: InlineElement[], style: ParagraphStyle, headingLevel: ?int } |
Run |
InlineElement | { text: string, style: RunStyle } |
Hyperlink |
InlineElement | { href: ?string, anchor: ?string, children: InlineElement[] } |
Bookmark |
InlineElement | { name: string, children: InlineElement[] } |
Image |
both | { binary, format, widthEmu, heightEmu, altText } |
Field |
InlineElement | { instruction: string, style: RunStyle } |
LineBreak, PageBreak, HorizontalRule |
both | marker elements |
Table |
BlockElement | { rows: TableRow[], style, caption, gridColumnsTwips } |
TableRow |
element | { cells: TableCell[], isHeader, heightTwips } |
TableCell |
element | { children: BlockElement[], style: CellStyle } |
ListNode |
BlockElement | { items: ListItem[], ordered, format, startAt } |
ListItem |
element | { children: InlineElement[], nestedList: ?ListNode } |
Styles live under Dskripchenko\PhpDocx\Style:
RunStyle— font, weight, italic, color, size, highlight, …ParagraphStyle— alignment, indents, spacing, bordersCellStyle— width, padding, borders, valign, gridSpan, rowSpanTableStyle— width, borders, alignment, cell margins, layoutPageSetup,PaperSize,Orientation,Alignment,VerticalAlign,BorderStyle,Border,BorderSet
Round-trip
The library targets semantic round-trip safety, not byte equality — content, structure and styling survive, but XML ordering and whitespace may differ.
In-scope round-trip features:
- Paragraphs/headings with all run formatting
- Tables with
vMerge/gridSpanreconstruction - Lists (bullet/decimal/letter/roman) with arbitrary nesting
- Images with EMU sizes and alt text
- Hyperlinks (external + internal anchors) and bookmarks
- Headers/footers (default/first/even) and watermarks
- Field codes (PAGE, NUMPAGES, DATE, MERGEFIELD)
- Page setup (size, orientation, margins)
Out-of-scope features are silently dropped (footnotes, comments, equations, etc.).
Architecture
The same Document AST is shared by HTML conversion, programmatic
construction and DOCX reading — every entry/exit point operates on
typed value-objects.
Development
License
MIT — see LICENSE.
All versions of php-docx with dependencies
ext-dom Version *
ext-mbstring Version *
ext-zip Version *