Stars: 158
Forks: 32
Pull Requests: 4
Issues: 14
Watchers: 17
Last Updated: 2023-08-01 15:47:33
The XSL-way templating library for MS Office Word DOCX documents.
License: MIT License
Languages: PHP
Your donations are always appreciated!
PHPStamp is a simple PHP templating engine for XML-based Microsoft Word documents.
Library aims to provide native XML-way of templating for DOCX documents as an altenative to treating its content as a string 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) http://habrahabr.ru/post/244421/
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.
Library requires PHP 5.3+ with DOM, XSL and Zip extensions.
Also depends on doctrine2/Lexer
package.
Install with Composer.
composer require shadz3rg/php-stamp
<?php
require 'vendor/autoload.php';
use PHPStamp\Templator;
use PHPStamp\Document\WordDocument;
$cachePath = 'path/to/writable/directory/';
$templator = new Templator($cachePath);
// Enable debug mode to generate template with every render call.
// $templator->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 = array(
'library' => 'PHPStamp 0.1',
'simpleValue' => 'I am simple value',
'nested' => array(
'firstValue' => 'First child value',
'secondValue' => 'Second child value'
),
'header' => 'test of a table row',
'students' => array(
array('id' => 1, 'name' => 'Student 1', 'mark' => '10'),
array('id' => 2, 'name' => 'Student 2', 'mark' => '4'),
array('id' => 3, 'name' => 'Student 3', 'mark' => '7')
),
'maxMark' => 10,
'todo' => array(
'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();