wrklst / docxmustache
docx template manipulation class, based on mustache templating language
Installs: 6 318
Dependents: 0
Suggesters: 0
Security: 0
Stars: 44
Watchers: 8
Forks: 18
Open Issues: 2
Requires
- php: >=8.1
- intervention/image: ^2.7
- mustache/mustache: ^2.12
- wrklst/madzipper: ^1.5
- dev-master
- v0.0.37
- v0.0.36
- v0.0.35
- v0.0.34
- v0.0.33
- v0.0.32
- v0.0.31
- v0.0.30
- v0.0.29
- v0.0.28
- v0.0.27
- v0.0.26
- v0.0.25
- v0.0.24
- v0.0.23
- v0.0.22
- v0.0.21
- v0.0.20
- v0.0.19
- v0.0.18
- v0.0.17
- v0.0.16
- v0.0.15
- v0.0.14
- v0.0.13
- v0.0.12
- v0.0.11
- v0.0.10
- v0.0.9
- v0.0.8
- v0.0.7
- v0.0.6
- v0.0.5
- v0.0.4
- v0.0.3
- v0.0.2
- v0.0.1
- dev-scrutinizer-patch-4
- dev-analysis-qMawJM
- dev-analysis-qgEPn4
- dev-analysis-XVO63w
- dev-analysis-qByp9V
- dev-analysis-8PrVJ9
- dev-analysis-8mMj7a
- dev-scrutinizer-patch-3
- dev-scrutinizer-patch-2
- dev-scrutinizer-patch-1
This package is auto-updated.
Last update: 2025-02-21 10:16:24 UTC
README
DocxMustache for Laravel 11.x
DocxMustache is a Laravel 11.x package for manipulating DOCX templates using the Mustache templating language. It allows you to merge richly styled Word documents with data from various sources. This package is still under active development, and breaking changes may occur.
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:
composer require wrklst/docxmustache
Refer to the Examples section and the examples
folder for configuration and usage instructions.
Features
HTML Conversion
Basic HTML conversion is supported, including:
- Bold (
<b>
) - Italic (
<i>
) - Underline (
<u>
)
Limitations:
- Does not support combined styling (e.g., bold + italic).
- Non-HTML values must be escaped using:
htmlspecialchars($value, ENT_COMPAT, 'UTF-8');
Special Note: To prevent unnecessary escaping of HTML, prefix the value with:
*[[DONOTESCAPE]]*
Replacing Images
Dynamic image replacement is supported. Follow these steps:
- Add the image URL (reachable and supported format) to the image's alt text description field in the DOCX template.
- Use pseudo-tags around the URL, like so:
[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:
sudo apt install libreoffice-common
For Arial font support, install:
sudo apt-get install ttf-mscorefonts-installer
Dependencies
Required Packages
For Intervention Image, ensure you:
- Add the provider and alias to your app config.
- Install
gd
orimagick
as required (details here).
Laravel-Specific Dependencies
If you want to port the package to a non-Laravel environment, consider replacing:
- Storage and File Classes: Based on Flysystem, 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:
- Fork the repository.
- Create a pull request with your changes.
- 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
- PHPWord
- docxtemplater pro (MIT licensed; image replacement requires a commercial plugin)
- docxpresso (commercial)
- phpdocx (commercial)