shadz3rg / php-stamp
MS Office Word DOCX 文档的 XSL 方式模板库。
1.0.0
2023-11-18 09:49 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-18 11:37:52 UTC
README
PHPStamp 是一个用于基于 XML 的 Microsoft Word 文档的简单模板引擎。
库旨在为 DOCX 文档提供原生的 XML 模板方式,作为将内容视为纯文本进行正则替换的替代方案,后者存在许多缺点。
它试图清理混乱的 WYSIWYG 生成代码,并从文档中创建可重用的 XSL 样式表。
一些额外信息
(EN) https://redd.it/30conp
(RU) https://habr.com/ru/articles/244421/
功能
- 将 XSL 模板缓存到文件系统以快速渲染文档。
- 跟踪文档模式 - 如果原始文档已更新,则生成和缓存新的模板。
- 可配置占位符标签的大括号。
- 基本扩展系统,有助于生成内容块,如 Cells 或 ListItems。
已知问题
插入到占位符标签中的值可能会被拼写检查突出显示为不正确,因为库删除了语言属性,而 Microsoft 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();