How to create Word file in PHP?

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 :

  • Title
  • 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

  • client_full_name
  • client_email_address
  • client_mobile_number
  • client_website
  • date_today



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:



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.

Kiran Chandrashekhar

Hey, Thanks for dropping by. My name is Kiran Chandrashekhar. I am a full-time software freelancer. I love Maths and Mathematical Shortcuts. Numbers fascinate me. I will be posting articles on Mathematical Shortcuts, Software Tips, Programming Tips in this website. I love teaching students preparing for various competitive examinations. Read my complete story.


  1. Hello Kiran,
    congratulations for your code, it’s great!
    I would like to know how I can replace the text I have in the footer of the Word file.

    Thank you

  2. Hello sir ,
    How can I put the HTML form Elements in the Doc file.
    LIke , I want to put the checkbox in the generated DOC file , then !!
    Help me in this.

Leave a Reply

Your email address will not be published. Required fields are marked *

Protected by WP Anti Spam