https://github.com/wrklst/docxmustache
laravel 12.x docx template manipulation class, based on mustache templating language
https://github.com/wrklst/docxmustache
docx docxtemplater laravel-5-package microsoft-word moustache mustache mustache-templates openxml pdf php-library reporting word
Last synced: 3 months ago
JSON representation
laravel 12.x docx template manipulation class, based on mustache templating language
- Host: GitHub
- URL: https://github.com/wrklst/docxmustache
- Owner: wrklst
- License: mit
- Created: 2017-05-06T18:36:20.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2025-04-30T15:41:06.000Z (about 1 year ago)
- Last Synced: 2026-01-11T16:26:19.777Z (5 months ago)
- Topics: docx, docxtemplater, laravel-5-package, microsoft-word, moustache, mustache, mustache-templates, openxml, pdf, php-library, reporting, word
- Language: PHP
- Homepage: https://wrklst.art
- Size: 379 KB
- Stars: 56
- Watchers: 7
- Forks: 19
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README

# DocxMustache *for Laravel 12.x.*
[](LICENSE) [](https://scrutinizer-ci.com/g/wrklst/docxmustache/?branch=master) [](https://scrutinizer-ci.com/g/wrklst/docxmustache/build-status/master) [](https://styleci.io/repos/90483440)
Docx template manipulation class for Laravel 12.x, based on [mustache templating language](https://mustache.github.io). This class is still under heavy development and works more like proof of concept at the moment. Things will change quickly and might break things.

With DocxMustache, you can:
- Replace text using Mustache syntax.
- Embed images dynamically.
- Support basic HTML styling (bold, italic, underline).
---
## Installation
Install the package via Composer:
```bash
composer require wrklst/docxmustache
```
Refer to the [Examples](#example) section and the `examples` folder for configuration and usage instructions.
---
## Features
### HTML Conversion
Basic HTML conversion is supported, including:
- Bold (``)
- Italic (``)
- Underline (``)
**Limitations:**
- Does not support combined styling (e.g., bold + italic).
- Non-HTML values must be escaped using:
```php
htmlspecialchars($value, ENT_COMPAT, 'UTF-8');
```
**Special Note:**
To prevent unnecessary escaping of HTML, prefix the value with:
```php
*[[DONOTESCAPE]]*
```
---
### Replacing Images
Dynamic image replacement is supported. Follow these steps:
1. Add the image URL (reachable and supported format) to the image's **alt text description** field in the DOCX template.
2. Use pseudo-tags around the URL, like so:
```text
[IMG-REPLACE]http://placehold.it/350x150[/IMG-REPLACE]
```
**Note:** Images will be resampled to match the constraints of the placeholder image in the template.
---
### DOCX to PDF Conversion
To enable DOCX-to-PDF conversion, install `libreoffice-common` on your server:
```bash
sudo apt install libreoffice-common
```
For Arial font support, install:
```bash
sudo apt-get install ttf-mscorefonts-installer
```
---
## Dependencies
### Required Packages
- [mustache/mustache](https://packagist.org/packages/mustache/mustache)
- [intervention/image](http://image.intervention.io)
For Intervention Image, ensure you:
1. Add the [provider and alias](http://image.intervention.io/getting_started/installation#laravel) to your app config.
2. Install `gd` or `imagick` as required ([details here](http://image.intervention.io/getting_started/installation#laravel)).
### Laravel-Specific Dependencies
If you want to port the package to a non-Laravel environment, consider replacing:
- **Storage and File Classes**: Based on [Flysystem](https://flysystem.thephpleague.com), can be replaced with PHP native file handling.
- **Process Handling**: Uses `\Symfony\Component\Process\Process` for PDF conversion.
---
## Example
Check out the `examples` folder for sample templates and usage.
---
## Contributions
Contributions are welcome! To contribute:
1. Fork the repository.
2. Create a pull request with your changes.
3. Include a corresponding issue for discussion.
We’ll be happy to review and discuss your ideas!
---
## Why Another DOCX Templating Solution?
While there are existing libraries for DOCX manipulation, they often:
- Use proprietary or non-standard templating syntaxes.
- Focus on complex feature sets, making them heavyweight for simple use cases.
DocxMustache was designed to:
- Use the widely adopted Mustache syntax.
- Provide simple and intuitive value and image replacement.
- Support repeating content in multiple dimensions.
### Alternatives
Here are other popular PHP libraries for DOCX manipulation:
- [openTBS – Tiny But Strong](http://www.tinybutstrong.com/opentbs.php)
- [PHPWord](https://github.com/PHPOffice/PHPWord)
- [docxtemplater pro](https://modules.docxtemplater.com) (MIT licensed; image replacement requires a commercial plugin)
- [docxpresso](http://www.docxpresso.com) (commercial)
- [phpdocx](https://www.phpdocx.com) (commercial)