keyboarduz / php-stamp
适用于MS Office Word DOCX文档的XSL模板库。
v1.0.0
2024-05-13 13:35 UTC
Requires
- php: >=7.4
- ext-dom: *
- ext-json: *
- ext-xsl: *
- ext-zip: *
- shadz3rg/lexer: ^1.0
Requires (Dev)
- ekino/phpstan-banned-code: ^1.0
- friendsofphp/php-cs-fixer: ^3.22
- phpstan/phpstan: ^1.10
- phpunit/phpunit: ^9.6
This package is auto-updated.
Last update: 2024-09-13 14:21:38 UTC
README
PHPStamp是一个简单的模板引擎,用于基于XML的Microsoft Word文档。
该库旨在为DOCX文档提供本地的XML模板方式,作为将内容视为纯文本进行正则替换的替代方案,后者有许多缺点。
相反,它试图清理混乱的WYSIWYG生成的代码,并从文档中创建可重用的XSL样式表。
一些附加信息
(EN) https://redd.it/30conp
(RU) https://habr.com/ru/articles/244421/
功能
- 将XSL模板缓存到文件系统以快速渲染文档。
- 跟踪文档模式 - 如果原始文档已更新,则生成和缓存新模板。
- 可配置占位符标签的大括号。
- 基本的扩展系统,有助于生成内容块,如单元格或列表项。
已知问题
插入到占位符标签中的值可能会被拼写检查程序标记为不正确,因为库删除了语言属性,而MS Word试图用系统语言检查它。
要求
库需要PHP 7.4+以及DOM、XSL、Zip扩展。
安装
使用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 re-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 = [ '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();