Generate MS Word document files with Codeigniter and Phpword library

The second reason is I decide to give my students full downloaded example not only screens from the code, therefore I make exclusive simple Codeigniter theme with minimum design files and with very well structure to help you to be very comfortable when use this code

My simple Codeigniter version built by the latest version for Codeigniter 3 and I will update it continually, also the design theme built with bootstrap 3 latest version and also will update continually.

The third reason is this the first tutorial in the internet which teach you how to integrate the latest version from phpword library and how to use it to build MSword files with PHP, yes you will see many tutorials for how to build Microsoft word files but many of them not work good or not compatible enough but phpword library is very professional library and also it was free open source library.

But the problem is that many tutorials focus on the old version from this library — it was 0.16 phpword version and this old version lack many features, you cannot have free to build dynamic Microsoft word reports with your application just using preset file template, but in this tutorial you will have full working example by how to build dynamic word file with .docx extension.

Note: phpword is a part from Phpoffice package, you can use this package for many targets like to read and write RTF, HTML PDF files if you want to read the full documentation for more details you can read it from here

Installation:

1- Download the project from the bottom link page and extract it in localhost

2- Create new database, name it as (demo)

3- Import tables from application/tables

4- Go to the URL http://localhost/codeigniter_demo/

Project structure:
In this app we have :

1- Phpword controller

In this controller we have two methods Index to show news table and download to generate Msword files from news table

2- Phpword model

To select our news from news table

3- Phpword library

Import phpword original file from third party folder

4- PhpWord third_party

The original files for PhpWord library, you can also download this package from their GitHub repo in this link, but I recommended to use it from my full example directly

5- Global folder

Contains assets file like bootstrap 3 main framework and starter theme with JavaScript , html , CSS files

6- Views folder

Which contains views file for our layout templates with header and footer and content

How it works?

When you see the demo page you will find download link on the bottom of the page, when you click on it you will invoke download function.

In this function :

* We load phpword library

* Fetch news and pass it to the variable $news

* Remove compatibility mode on the file title, we pass number 14 and 15 to point to word 2010 and word 2013

* Add path to the variable $targetFile and filename to $filename

* Start foreach loop to fetch all news and make dynamic word content

* AddSection function to start new page

* addText function to add new text (i use it to add title but you can use it in titles or paragraphs),you can pass style to this function.

* Add addTextBreak to add break after text in other words to start new line

* Add addImage function to add new image ,we can pass styles to this function

* Add another addTextBreak function with another addText to start the paragraph

* Use save function to save the docx file as tmp file on the fly and then use header to download this file and exit the function

TRY Demo Download


Originally published at webeasystep.com.