This is another attempt to implement a stable, full-fledged XML-based office documents template using the standard PHP DOMDocument and XSL tools.
The task was precisely to generate a reusable template, which you would not have to manually edit or resort to third-party programs to refine it.
You can find the library here:
github.com/shadz3rg/PHPStampPrinciple of operation
Despite all the delights of visual editors, the output is always far from the most optimal document code, and MS Word is no exception. This is how simply the tags added to the document to replace the XSL logic will fail, even despite the relatively simple structure of the DOCX document.
')
The main problem is divided into fragments (they are also “run”, are represented by the nodes
<w: r /> ) paragraphs, the contents of the Tags are divided into several nodes and cannot be processed in this form.
First, let's try to simplify the markup.
1. Remove the language properties of the passages. Which is logical, when replacing a tag, a value can be inserted in any language, besides punctuation marks do not belong to the language, which causes additional problems when searching for tags.
2. Remove the empty node properties of excerpts, a purely technical point.
3. Combine the adjacent passages using the deepEqual comparison (which is still “Not implemented” in the DOMDocument) of the properties node.
If everything went according to plan, the library can only add the appropriate XSL logic. Possibility is provided - some extensibility with options. So, for example, currently implemented table rows and lists.
The resulting template is cached in the file system, so its subsequent calls will be as fast as possible (as far as XSL is capable).
Provides support for different formats. The plans were to add support for the ODT format, but in view of the excellent support for DOCX in LibreOffice, there is no particular need. Perhaps a good idea would be to generate XLSX, which should work out pretty quickly compared to PHPExcel.
Dependencies and installation
You can install the library through Composer, you need PHP 5.3 with Zip, DOM and XSL modules on the server.
{ "minimum-stability": "dev", "require": { "shadz3rg/php-stamp": "dev-master" } }
Usage example
For example, use the template:

<?php require 'vendor/autoload.php'; use PHPStamp\Templator; use PHPStamp\Document\WordDocument; $cachePath = 'path/to/writable/directory/'; $templator = new Templator($cachePath);
At the output we get a document with the data we need:
