Generate MS Word document files with Codeigniter and Phpword library.
Hi everyone ,this tutorial is special and exclusive for you, the first reason is I decide to attach demo example for every tutorial because I figure out that many students don't know how my project look like and I think that have a live example will help them to make full vision about what is going on,
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 structured to help you to be very comfortable when to 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 well or not compatible enough but phpword library is very professional library and also it was a free open source library.
But the problem is that many tutorials focus on the old version of this library – it was 0.16 phpword version and this old version lacks 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 of 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
Update : the next version of this tutorial about export data to excel in php codeigniter
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/
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
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 the 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 a 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 temp file on the fly and then use header to download this file and exit the function