Tag Archives: phpword

Creating Ms Word Document using CodeIgniter and PHPWord

One of my reader – Don Lafferty – told us that integrating PHPWord into CodeIgniter is also as easy as integrating PHPExcel into CodeIgniter. So, I decided to give it a try and it really does so simple.

What is PHPWord? From github:

[box type=”info”] PHPWord is a library written in PHP that create word documents. No Windows operating system is needed for usage because the result are docx files (Office Open XML) that can be opened by all major office software.[/box]

Notes: As I write this, PHPWord can create .docx, .rtf and .odt file.

  • Without more a do, go download the required package

[button link=”http://codeigniter.com/” newwindow=”yes” color=”orange”] Download CodeIgniter[/button]

[button link=”https://github.com/PHPOffice/PHPWord” newwindow=”yes” color=”black”]Download PHPWord[/button]

  • Extract PHPWord (only extract PHPWord.php and PHPWord folder) into your CodeIgniter’s application/third_party
ci phpword extract file

Only extract those two


Your application/third_party will look like this

ci third_party folder

Your new third_party folder after adding PHPWord

  • Now, create a new library inside your CodeIgniter’s application/libraries. Let’s call it Word.php. Here’s the full source:

  • OK, it’s done.
  • Now we can call and use the Word library from within your controllers (examples are taken from PHPWord with minimal change):
  • Call/load the library and create new section (needed)

  • Adding some text and style it!

  • Adding some images! Watchout on how I add new image, don’t use base_url or site_url, PHPWord only take an absolute path to your server (it also means that you can’t insert image that isn’t hosted on your server)

  • Create a table and give some style

  • Lastly, give the Ms Word document as .docx file to users without saving it to server’s hard-disk.

  • Example result (opened using Ms Word 2007 and zoomed 50%)
ci and phpword example result

This is what our code generate. CodeIgniter + PHPWord = MS Word 2007 document

Now our CodeIgniter become more complete, we can create Excel file, create a PDF file and, in this tutorial, creating Microsoft Word 2007 document!