https://github.com/shadz3rg/php-stamp
The XSL-way templating library for Word DOCX documents.
https://github.com/shadz3rg/php-stamp
doc document docx dom microsoft office php template word xml xsl
Last synced: 26 days ago
JSON representation
The XSL-way templating library for Word DOCX documents.
- Host: GitHub
- URL: https://github.com/shadz3rg/php-stamp
- Owner: shadz3rg
- License: mit
- Created: 2014-10-13T16:11:06.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2023-11-18T09:57:38.000Z (about 2 years ago)
- Last Synced: 2025-10-21T23:10:21.669Z (3 months ago)
- Topics: doc, document, docx, dom, microsoft, office, php, template, word, xml, xsl
- Language: PHP
- Homepage:
- Size: 208 KB
- Stars: 166
- Watchers: 15
- Forks: 34
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
PHPStamp
=========
PHPStamp is a simple templating engine for [XML-based Microsoft Word documents](https://learn.microsoft.com/en-us/office/open-xml/word/structure-of-a-wordprocessingml-document?tabs=cs).
Library aims to provide native XML-way of templating for DOCX documents as an alternative to treating its content as a plain text for regex replacing, which has a lot of downsides.
Instead it tries to clean messy WYSIWYG-generated code and create reusable XSL stylesheet from document.
Some additional information:
(EN) https://redd.it/30conp
(RU) https://habr.com/ru/articles/244421/
Features
----
- Caching XSL template to filesystem for fast document render.
- Track document mode - generate and cache new template if original document was updated.
- Configurable brackets for placeholder tags.
- Basic extension system, which helps to generate content blocks such as Cells or ListItems.
Known issues
----
Values inserted into placeholder tags may be highlighted as incorrect by spellcheck, since library removes language attribute and MS Word tries to check it with system language.
Requirements
----
Library requires PHP 7.4+ with DOM, XSL, Zip extensions.
Installation
----
Install with Composer:
`composer require shadz3rg/php-stamp`
Usage
----
##### Template:

```php
debug = true;
// Enable track mode to generate template with every original document change.
// $templator->trackDocument = true;
$documentPath = 'path/to/document.docx';
$document = new WordDocument($documentPath);
$values = [
'library' => 'PHPStamp 0.1',
'simpleValue' => 'I am simple value',
'nested' => [
'firstValue' => 'First child value',
'secondValue' => 'Second child value'
],
'header' => 'test of a table row',
'students' => [
['id' => 1, 'name' => 'Student 1', 'mark' => '10'],
['id' => 2, 'name' => 'Student 2', 'mark' => '4'],
['id' => 3, 'name' => 'Student 3', 'mark' => '7']
],
'maxMark' => 10,
'todo' => [
'TODO 1',
'TODO 2',
'TODO 3'
]
];
$result = $templator->render($document, $values);
// Now you can get template result.
// 1. HTTP Download
$result->download();
// Or
// 2. Save to file
// $saved = $result->save(__DIR__ . '/static', 'Test_Result1.docx');
// if ($saved === true) {
// echo 'Saved!';
// }
// Or
// 3. Buffer output
// echo $result->output();
```
##### Result:
