Backend Development 4 min read

MDword: PHP Library for Dynamically Generating Word Documents – Features, Comparison with PHPWord, and Usage Guide

This article introduces MDword, a PHP library for programmatically creating Word documents, outlines its recent updates, compares it with PHPWord, and provides step‑by‑step installation and usage examples including code snippets for template processing, image insertion, and document saving.

Laravel Tech Community
Laravel Tech Community
Laravel Tech Community
MDword: PHP Library for Dynamically Generating Word Documents – Features, Comparison with PHPWord, and Usage Guide

Update Log (see examples for usage)

Support multi‑level lists with paragraph style assignment

Optimized table‑of‑contents navigation

Bug fixes

Project Introduction

Purpose: dynamically generate Word files. Advantage: developers only need to focus on data and logic; styling is handled by editing the Word template (master document) directly in Office.

Comparison with PHPWord

Common Points

Both are PHP libraries (resource packages)

Both are used to generate Office Word documents

Differences

PHPWord writes each element individually, while MDword modifies the master template, offering stronger functionality and higher coding efficiency.

Changing text styles, adding cover pages, or editing headers/footers in MDword only requires adjusting the template in Word; PHPWord requires tedious per‑element adjustments.

MDword can automatically generate a table of contents.

Tutorial

Installation

// Method 1
composer require mkdreams/mdword
// Method 2 – manually include autoloader
require_once('Autoloader.php');

Adding Annotations to the Template "temple.docx"

(Image illustration omitted)

Usage Example (refer to tests\samples\simple for more features such as TOC, numbering, etc.)

// Create processor and load template
$TemplateProcessor = new WordProcessor();
$template = 'temple.docx';
$TemplateProcessor->load($template);

// Assign values
$TemplateProcessor->setValue('value', 'r-value');

// Clone rows (e.g., repeat a block 3 times)
$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 style
$TemplateProcessor->deleteP('style');

// Save the generated document
$rtemplate = __DIR__.'/r-temple.docx';
$TemplateProcessor->saveAs($rtemplate);

Result

(Resulting document screenshot omitted)

Animated Demo

(Animated GIF illustration omitted)

backend developmentPHPDocument AutomationMDwordWord generation
Laravel Tech Community
Written by

Laravel Tech Community

Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.