PHPackages                             d36dak/docx-builder - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [PDF &amp; Document Generation](/categories/documents)
4. /
5. d36dak/docx-builder

ActiveLibrary[PDF &amp; Document Generation](/categories/documents)

d36dak/docx-builder
===================

A PHP package for programmatically building docx documents.

v0.1.2(2mo ago)04MITPHPPHP ^8.2

Since May 21Pushed 2mo agoCompare

[ Source](https://github.com/D36Dak/docx-builder)[ Packagist](https://packagist.org/packages/d36dak/docx-builder)[ RSS](/packages/d36dak-docx-builder/feed)WikiDiscussions main Synced 1w ago

READMEChangelogDependencies (3)Versions (4)Used By (0)

docx-builder
============

[](#docx-builder)

`docx-builder` is a small PHP library for creating DOCX documents programmatically. It provides a fluent builder API for composing paragraphs, tables, images, page breaks, headers, and footers, then writing the result as a `.docx` file, or returning the generated document contents as a string.

The package is designed around a lightweight document model and bundled OpenXML resources, so simple documents can be generated without preparing a separate template file.

Features
--------

[](#features)

- Create DOCX documents from PHP code.
- Add styled paragraphs with font family, font size, color, bold, italic, and underline options.
- Configure paragraph alignment, spacing before and after, and line spacing.
- Build paragraphs from multiple text runs.
- Add page number and total page count fields.
- Add page breaks.
- Add tables with configurable header rows and cell styling.
- Add images with width, height, alignment, and alt text.
- Add default and first-page headers and footers.
- Configure page format and margins.
- Save documents to disk or return generated DOCX contents as a string.

Requirements
------------

[](#requirements)

- PHP 8.2 or newer
- PHP `zip` extension
- PHP `dom` extension

Public API
----------

[](#public-api)

The supported public API is limited to `DocxBuilder`, `ParagraphBuilder`, and `ParagraphElement`. `ParagraphElement` is public as the value returned by `ParagraphBuilder::build()` and accepted by `DocxBuilder`. Other classes are implementation details and are marked `@internal`; they may change without a backward compatibility promise.

Installation
------------

[](#installation)

This package is installed via Composer. To add a dependency to `docx-builder` in your project run the following command:

```
composer require d36dak/docx-builder
```

Usage examples
--------------

[](#usage-examples)

The following example can be used in any PHP project after installing the package with Composer. This example is also available as `examples/basic.php` and can be run from the project root:

```
php examples/basic.php
```

```
