Free PHP Library to Create & Manipulate Word Documents
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 seeking to dynamically generate or manipulate Microsoft Word documents (.docx), the open source landscape offers powerful tools that can streamline workflows and enhance application capabilities. One such notable library is MDword, a PHP library designed to create and modify Word documents programmatically without requiring Microsoft Office to be installed on the server. Unlike some heavier, enterprise-level tools, MDword focuses on minimalism, speed, and ease of use. It’s ideal for projects that need to dynamically generate documents based on templates, user input, or data from databases. The library supports various advanced features such as template-based word document generation, insertion and formatting of rich content, adding and managing tables, inserting text, combining multiple Word files into a single document and paragraphs, handling images and so on.
MDword is a powerful open source PHP library designed to simplify the process of generating Microsoft Word documents dynamically inside PHP applications. With MDword, software developers can create, read, edit, and manipulate Word documents (.docx) programmatically with ease. This library is particularly useful for generating reports, invoices, contracts, and other business documents in a structured and automated way. MDword doesn’t require a heavy framework or external tools like LibreOffice or COM automation. It works out of the box in most PHP environments. Overall, it is a versatile and developer-friendly PHP library for generating Word documents dynamically. With a focus on flexibility, ease of use, and performance, it’s an excellent tool for developers who need reliable document automation in PHP projects — all without paying for commercial libraries.
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.";