MDword: PHP Library for Dynamic Word Document Generation – Features, Performance, and Usage Guide
MDword is a PHP library that streamlines dynamic Word document creation by optimizing master template processing, fixing memory leaks, and offering detailed performance metrics, with comprehensive installation instructions, usage examples, and a comparison to PHPWord.
MDword is a PHP tool for generating Word documents, focusing on dynamic data and logic while allowing style adjustments through Word's master templates.
Changelog
Optimized master template generation speed
Fixed memory leak issues
Performance (Statistics Script)
Test Item
Time (s)
1-page master assignment 100 times
0.04
1-page master assignment 500 times
0.16
1-page master assignment 1000 times
0.33
1-page master assignment 10000 times
7.80
1750-page master assignment 100 times
4.61
1750-page master assignment 500 times
4.94
1750-page master assignment 1000 times
5.43
1750-page master assignment 10000 times
17.39
Memory Usage (Statistics Script)
Run Count
Cumulative Memory Usage
Note
1
0.050590515136719 M
First load of PHP classes
2
0.050949096679688 M
3
0.050949096679688 M
4
0.050949096679688 M
5
0.050949096679688 M
6
0.050949096679688 M
7
0.050949096679688 M
8
0.050949096679688 M
Project Introduction
Main purpose: dynamically generate Word documents. Advantage: developers only need to focus on dynamic data and logic; styling is handled via Word's master templates.
Comparison with PHPWord
Similarities
Both are PHP libraries.
Both generate Office Word files.
Differences
MDword works on master templates, offering higher efficiency and richer features.
Style changes, cover pages, headers/footers are adjusted in Word directly with MDword, whereas PHPWord requires element‑by‑element modifications.
MDword can automatically generate a table of contents.
Tutorial
Installation
// Method 1
composer require mkdreams/mdword
// Method 2 (manual autoloader)
require_once('Autoloader.php');Adding Annotations to the Master Template "temple.docx"
Refer to the sample tests for more advanced usage such as generating catalogs and numbering.
Usage Example
// Create processor and load master template
$TemplateProcessor = new WordProcessor();
$template = 'temple.docx';
$TemplateProcessor->load($template);
// Set simple values
$TemplateProcessor->setValue('value', 'r-value');
// Clone and copy sections
$TemplateProcessor->clones('people', 3);
$TemplateProcessor->setValue('name#0', 'colin0');
$TemplateProcessor->setValue('name#1', [
['text'=>'colin1','style'=>'style','type'=>MDWORD_TEXT],
['text'=>1,'type'=>MDWORD_BREAK],
['text'=>'86','style'=>'style','type'=>MDWORD_TEXT]
]);
$TemplateProcessor->setValue('name#2', 'colin2');
$TemplateProcessor->setValue('sex#1', 'woman');
$TemplateProcessor->setValue('age#0', '280');
$TemplateProcessor->setValue('age#1', '281');
$TemplateProcessor->setValue('age#2', '282');
// Insert image
$TemplateProcessor->setImageValue('image', dirname(__FILE__).'/logo.jpg');
// Delete a paragraph
$TemplateProcessor->deleteP('style');
// Save the generated document
$rtemplate = __DIR__.'/r-temple.docx';
$TemplateProcessor->saveAs($rtemplate);For more detailed examples, see the test samples in the repository.
Laravel Tech Community
Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.