In this tutorial, we will examine how to create Word file in PHP. We often come across a situation, where we need to generate Microsoft Word document in PHP. There are a number of free/paid libraries available which can be used to generate Microsoft Word document.
If we were to use to any of the readily available libraries, it would take many lines of code in PHP just to generate a very simple Word document. It also lacks the formatting that you generally expect form a normal Microsoft Word document. Most of the libraries, lack of support for various formatting in Word document.
So, at the end of this tutorial, you will learn how to create a Microsoft Word file in PHP with all the advanced formatting features that you generally expect form a normal Microsoft Word document.
Let us consider an example, where we need to create a word document as shown below in PHP. I created a simple Word document using Microsoft Office 2010 Software. The document can be downloaded from here. This is a typical document where you would need to generate the following :
- Various Headings
- A table with client information and so on.
Now, the problem with generating such a document using any libraries is that, it hardly supports all of the formatting we have used in the document.
Step 1 : Prepare a Template File
The trick in dynamically generating word document in PHP is to first create a template file. For instance, in the above document, we notice that, except for the table where we enter the client information, most of the information like the Header, Headings remains same for all the clients.
So, we would prepare a so-called template file, where we create a Microsoft Word document using Office Software. And instead of entering the client information, we type-in placeholders as shown. Notice the various placeholders like
Step 2 : Save the Template file as DOCX document
Once you have prepared the template file, you would now save the file as an docx Word document. Now let us save this file as template.docx. The template.docx file is nothing but a zip file. This file can be unzipped like any zip archive.
Step 3 : Open the Zip File and populate the Placeholders
Now, open the Docx file as a zip file and replace the placeholders created in Step 1 with relevant information. The following PHP code can be used:
$zip = new ZipArchive;
//Docx file is nothing but a zip file. Open this Zip File
if($zip->open($fullpath) == true)
//In the Open XML Wordprocessing format content is stored
//in the document.xml file located in the word directory.
$key_file_name = 'word/document.xml';
$message = $zip->getFromName($key_file_name);
$timestamp = date('Y-m-d H:i:s');
//Replace the Placeholders with actual values
$message = str_replace("client_full_name", "Kiran Chandrashekhar", $message);
$message = str_replace("client_email_address", "[email protected]", $message);
$message = str_replace("client_mobile_number", "+917829713845", $message);
$message = str_replace("client_website", "sapnaedu.in", $message);
$message = str_replace("date_today", $timestamp, $message);
//Replace the content with the new content created above.
//That's it, you have the new Word document in the Result Directory with placeholders replaced with new content
The complete PHP source for writing an Microsoft Excel file can be downloaded from here.
I hope, you find this article very useful. If you have any doubts or have any comments concerning this tutorial, Please leave a comment or contact me. I would be glad to help.