Free PHP Library to Create & Manage Template-based DOCX Files

Leading Open Source PHP Library allows to Create, Read, Modify and Manipulate Microsoft Word Processing (DOCX) Documents from Templates. Add Tables, Insert Text, Images & Apply Formatting via free PHP API

What is MDword?

For PHP developers aiming to create or manage Microsoft Word documents, MDword stands out as a lightweight and efficient PHP DOCX REST API alternative. As an open source PHP DOCX API, MDword enables developers to create template-based DOCX files, modify content, and build rich document features—all without relying on Microsoft Office or heavy dependencies. This free word processing library supports inserting and managing tables, adding formatted text, handling images, and merging multiple Word files into one. Its minimalist and high-performance approach makes it ideal for generating documents from templates, user data, or database content in real-time within PHP apps.

Designed for simplicity and performance, MDword is a versatile PHP word processing library for building automated document workflows. Developers can easily add tables to DOCX via PHP, manipulate paragraphs, and convert Word documents PHP without external tools like LibreOffice or COM. Whether you're generating invoices, contracts, or reports, MDword provides robust DOCX handling in native PHP. With out-of-the-box compatibility in most environments and no licensing fees, it’s an excellent choice to manage Word documents PHP in dynamic web applications while keeping development lightweight and cost-effective.

Previous Next

Getting Started with MDword

The recommended way to install the MDword into your project is by using Composer. Please use the following command for a smooth installation.

Install MDword via Composer

composer require mkdreams/mdword  

Install MDword via GitHub

git clone https://github.com/mkdreams/MDword.git 

Template-Based Word Document Generation via PHP

At its core, MDword allows software developers to create new Word documents from scratch. Developers can initialize a new document object and then begin adding content to it. You can set basic document properties if the library supports them (though advanced property manipulation might vary). MDword allows users to create Word templates with placeholders that can be dynamically replaced with actual data. It Supports text, tables, images, charts, and other Word elements. The following example demonstrates, how developers can generate basic word document inside PHP applications.

How to Create Template-Based Word (.docx) Document via PHP Library?

require_once 'vendor/autoload.php';

use MDword\WordProcessor;

// Load a template
$template = new WordProcessor();
$template->load('template.docx');

// Replace placeholders
$template->setValue('{{NAME}}', 'John Doe');
$template->setValue('{{DATE}}', date('Y-m-d'));

// Save the modified document
$template->save('output.docx');

Word Text Addition & Formatting via PHP

Adding and formatting text to word documents is a primary requirement, and MDword offers capabilities to insert text and apply various formatting options inside PHP applications. Software developers can add paragraphs, text runs, and apply styles such as bold, italics, underline, font size, font family, and color. This allows for rich text content within your generated documents. The following PHP code shows how developers can insert text to Word documents and apply formatting to it via PHP library.

How to Insert Text to Word Document and apply Formatting via PHP Library?

require 'vendor/autoload.php';
use Mkdreams\MDword\MDword;
use Mkdreams\MDword\Elements\TextRun;
use Mkdreams\MDword\Elements\Paragraph;

$mdword = new MDword();
$section = $mdword->createSection(); // Sections are often containers for content

// Add a simple paragraph
$paragraph1 = new Paragraph();
$paragraph1->addText('This is a simple paragraph.');
$section->addElement($paragraph1);

// Add a paragraph with formatted text
$paragraph2 = new Paragraph();
$textRun1 = new TextRun();
$textRun1->setText('This text is ');
$paragraph2->addElement($textRun1);

$textRun2 = new TextRun();
$textRun2->setText('bold and red.');
$textRun2->setFontStyle(['bold' => true, 'color' => 'FF0000']); // Color as hex
$paragraph2->addElement($textRun2);
$section->addElement($paragraph2);

// Add text with specific font and size
$paragraph3 = new Paragraph();
$textRun3 = new TextRun();
$textRun3->setText('This is Arial, size 16.');
$textRun3->setFontStyle(['name' => 'Arial', 'size' => 16]);
$paragraph3->addElement($textRun3);
$section->addElement($paragraph3);

$mdword->save('FormattedTextDocument.docx');
echo "Formatted document created.";

Add & Manage Tables in Word Files via PHP

Tables are crucial for presenting structured data. The open source MDword library often provides mechanisms to create tables, add rows, cells, and populate them with content. Software developers can define table structures, set cell content (text, potentially other elements), and sometimes apply basic styling to borders or cell shading with just a couple of lines of code. Here is a simple that demonstrates, how to create and manage tables inside word .docx documents via PHP library.

How to Create and Manage Tables inside Word DOCX Documents via PHP Library?

require 'vendor/autoload.php';
use Mkdreams\MDword\MDword;
use Mkdreams\MDword\Elements\Table;
use Mkdreams\MDword\Elements\Row;
use Mkdreams\MDword\Elements\Cell;
use Mkdreams\MDword\Elements\TextRun;

$mdword = new MDword();
$section = $mdword->createSection();

// Create a table
$table = new Table();
// Potentially add table-level styling, e.g., borders
// $table->setStyle(['borderSize' => 6, 'borderColor' => '000000']);

// Add a header row
$headerRow = new Row();
$cellH1 = new Cell();
$cellH1->addTextRun(new TextRun('Header 1', ['bold' => true]));
$headerRow->addCell($cellH1);

$cellH2 = new Cell();
$cellH2->addTextRun(new TextRun('Header 2', ['bold' => true]));
$headerRow->addCell($cellH2);
$table->addRow($headerRow);

// Add a data row
$dataRow1 = new Row();
$cellR1C1 = new Cell();
$cellR1C1->addTextRun(new TextRun('Data A1'));
$dataRow1->addCell($cellR1C1);

$cellR1C2 = new Cell();
$cellR1C2->addTextRun(new TextRun('Data B1'));
$dataRow1->addCell($cellR1C2);
$table->addRow($dataRow1);

$section->addElement($table);
$mdword->save('DocumentWithTable.docx');
echo "Document with table created.";