aramirez92 / docxmustache
docx template manipulation class, based on mustache templating language
Requires
- php: >=7.1.0
- intervention/image: ^2.5
- mustache/mustache: ^2.12
- wrklst/zipper: ^1.0
- dev-master
- 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: 2024-11-09 10:51:38 UTC
README
DocxMustache for Laravel 6.0.
Docx template manipulation class for Laravel 6.0, based on mustache templating language. 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.
This package helps you to use docx files with mustache syntax as templates to merge richly styled documents with information from any data source. It can replace text and images and supports basic html styling (bold, itallic, underline).
Installation
composer require wrklst/docxmustache
Please check depencies down below and examples folder for configuration and usage.
HTML conversion
Current HTML conversion is basic and only supports singular runs of bold, italic and underlined text and no combination of these. It requires all values non html to be escaped with
htmlspecialchars($value, ENT_COMPAT, 'UTF-8');
and a prefix of
*[[DONOTESCAPE]]*
so the class knows not to escape the html before it is converted to openXML.
Replacing images
The image needs to be a reachable URL with a image in a supported format. The url value needs to be placed into the alt text description field of the image.
Images will be resampled to the constraints of the placeholder image.
The Image value needs to be formated the with pseudo tags around, such as:
[IMG-REPLACE]http://placehold.it/350x150[/IMG-REPLACE]
Example
Please also checkout the example in the example folder to get a basic understand of how to use this class.
DOCX to PDF conversion
Conversion to PDF requires libreoffice-common
to be installed on the server (used for conversion).
Use sudo apt install libreoffice-common
on your ubuntu/debian based server. Also install ttf-mscorefonts if you need support for Arial font when converting docx documents to pdf sudo apt-get install ttf-mscorefonts-installer
Other Dependencies
The package is dependent on several Laravel specific functions. It could easily be ported to other frameworks or be ported to be framework agnostic. In addition to the Laravel dependency, the page uses the following packages:
- mustache/mustache
- intervention/image (requires adding provider and alias to your app config as well as gd or imagick, please check the image intervention webpage for details)
Laravel specific dependencies (only relevant if ported into non Laravel environment):
- Storage and File class, based on Flysystem (for local file access, could also be replaced by phps own fopen etc methods)
- \Symfony\Component\Process\Process (only for PDF conversion)
Contributions
If you would like to contribute something to this package, please feel free to make a pull request and a corresponding issue and we will be happy to review and discuss.
Why another openXML / docx template solution?
There are some classes out there that help with writing and or changing the content of word documents, some with commercial licenses and some free. For our particular purpose we did not need most of the features many of the libraries out there have – we needed a simple solution that would allow to replace values and images and traverse through data in a easy and straightforward manner.
Many of the other libraries use cloning to repeat a block with some custom templating syntax instead of using a existing template syntax. We use the usual mustache syntax, also to achieve repeating content in as many dimensions as needed.
Other PHP Classes to manipulate openXML word documents:
- openTBS – Tiny But Strong
- PHPWord
- docxtemplater pro (basic opensource / free version / MIT license available as of writing; image replacing is a commercial plugin)
- docxpresso (commercial)
- phpdocx (commercial)