Home » Php » How to Create a word .doc file from a .doc template in php

How to Create a word .doc file from a .doc template in php

Posted by: admin July 12, 2020 Leave a comment


I need to create a word document from a word template, its something like, we need to replace the few strings (say variables) in the template with the values.

Please tell how we can do this in PHP ( or cakePHP).

How to&Answers:

I’d recommend checking out phpLiveDocx: http://www.phplivedocx.org/

And some other questions on SO that might be worth reading:


The biggest problem you will run into is that the .doc format is binary in nature (see this question and this post by Joel).

Is there any reason you cannot using either an HTML formatted document (which word has been able to open for over 10 years) or a Rich Text File? Both of these are text based, and will cause you significantly less pain. They are, as a result, very easy to use a template with.

Alternatively, if you can be sure that the user will have a newer version of Office (2007+) then you could, though it is somewhat tricky, use a .docx document, which is actually a Zip Archive and XML data. You can implement this fairly simply by creating a .docx word file that has the template you need, renaming it to .zip, extracting the files, teaching your application to do the text replacement on this in-place file structure, re-zip it using PHP’s ZipArchive class, and then sending that file (named .docx, of course).


i think this will help you


Generate docx from templates,


PHP library for the dynamical generation of Word documents in docx format.



Check http://www.phpbuilder.com/annotate/message.php3?id=1024286 and also the article it comments.

I made a similar trick when I needed to generate a simple excel table. When sending the right header, the client’s excel didn’t complain at all. Or if you can use another format directly, use it. (.docx or open document are compressed xml, so you can manipulate it more easily)