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
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 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
-
aldi rizaldiwhen trying to download word : Fatal error: Class 'PhpOffice\PhpWord\PhpWord' not found in C:\xampp\htdocs\codeigniter_demo\application\controllers\Phpword.php on line 24Reply
-
admininfo@webeasystep.comfollow the installation and it will works good,you can check demo with yourselfReply
-
-
neerajwhen trying to download word : Fatal error: Class 'PhpOffice\PhpWord\PhpWord' not found in C:\xampp\htdocs\codeigniter_demo\application\controllers\Phpword.php on line 24 - See more at: http://webeasystep.com/blog/view_article/Generate_MS_Word_document_files_with_Codeigniter_and_Phpword_library#sthash.7fPmGZSp.dpufReply
-
Tiago Scalco de MelloDownload link is out Oops, you've found a dead linkReply
-
admininfo@webeasystep.comthanks Tiago ,Link was fixed , if you see any broken links tell me and i am ready to fix it you are welcome my friendReply
-
-
ojchrisWill this work in codeigniter 2? I mean is it the same set up for implementing it in codeigniter 2? ThanksReply
-
Mohamed Augustin Sesaynice tutorial hope we will have more to improve our learningReply
-
admininfo@webeasystep.comyou are welcome mohamed , for sure i am working onReply
-
-
RanjeetFatal error: Class 'PhpOffice\PhpWord\PhpWord' not found in C:\xampp\htdocs\companyenc\application\controllers\Phpword.php on line 23Reply
-
rifqiFatal error: Cannot use PhpOffice\PhpWord\Shared\String as String because 'String' is a special class name in C:\xampp\htdocs\codeigniter_demo\application\third_party\PhpWord\Style\AbstractStyle.php on line 20 mohon pencerahannnya minReply
-
Sangramsinh SambhajiRao desaiType: PhpOffice\PhpWord\Exception\Exception Message: Could not close zip file test.docx. Filename: /application/third_party/PhpWord/Shared/ZipArchive.php Line Number: 161Reply
-
FabricioFor those who are still looking a way around this issue with PHP7, here is a page (is in indonessian so use a translator), I tested it, it worked and I am using it.Reply