Tutorials

Article Content:Generate MS Word document files with Codeigniter and Phpword library

Generate MS Word document files with Codeigniter and Phpword library

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

TRY Demo   Download



  • aldi rizaldi
    when 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
    June 28, 2016
    Reply
    • admin
      [email protected]
      follow the installation and it will works good,you can check demo with yourself
      June 28, 2016
      Reply
  • neeraj
    when 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.dpuf
    August 9, 2016
    Reply
    • admin
      [email protected]
      download the full code from github and it will work good with you you can also show the demo,it works good
      August 9, 2016
      Reply
    • admin
      [email protected]
      thanks neeraj , code updated and i fixed the issue
      August 19, 2016
      Reply
  • Kirino
    Thank you for the wonderful sample for integrating PHPWord into CodeIgniter. I am working to upgrade our legacy system from PHPWord 0.6 to 0.13 and not at all familiar with Composer or Namespaces so this is a great jumping off point! However, after much struggling I’ve found that while it’s working great in PHP 5.6 and fails in PHP 7.0. The error that I’m getting is: “Fatal error: Cannot use PhpOffice\PhpWord\Shared\String as String because 'String' is a special class name in C:\wamp7\www\codeigniter_demo\application\third_party\PhpWord\Style\AbstractStyle.php on line 20” Checking on the internet there is a reference to this bug in the issues area of the PHPWord project on github. https://github.com/PHPOffice/PHPWord/issues/736#issuecomment-191300938 which seems to indicate that it’s a problem with the version of PHPWord you are using in your demo. However, upgrading to the most current build is generating all sorts of other errors which I imagine is due to my ignorance of how to properly upgrade. Any possibility of upgrading your demo to see if that makes it function in PHP7? I’m running WAMP (wampserver3.0.6_x64) under Windows7. Thanks for the great resource!
    September 29, 2016
    Reply
    • admin
      [email protected]
      Hi Kirino, you are welcome About your wondering,Codeigniter 3.0 not full supported for PHP 7 also PHPWord.,so I decided to not support or test any demos on PHP 7 Untill codeigniter 4 came. The current version I supported 5.6,but you still can find some help in phpword github repository and codegniter.com forum thanks
      September 30, 2016
      Reply
  • Pavel J
    Hi, I use this tutorial to create docx document. On localhost (win10 XAMPP) there is all OK. But in production (PHP 5.6 a MySQL 5.5) dox document is corrupt. Look at: "http://www.ralton.cz/create_order_doc/227". What can bee reason. Thanks.
    December 14, 2016
    Reply
    • admin
      [email protected]
      Hi,Pavel J sorry man But there is no way for suggestions here,sure to follow all steps and it will works good as you see in the demo. the best way to do this is to open log and generate new file and see what recorded in log file after file generation
      December 15, 2016
      Reply
  • Tiago Scalco de Mello
    Download link is out Oops, you've found a dead link
    February 27, 2017
    Reply
    • admin
      [email protected]
      thanks Tiago ,Link was fixed , if you see any broken links tell me and i am ready to fix it you are welcome my friend
      February 28, 2017
      Reply

Leave a Reply

Your email address will not be published.


Notify me of followup comments via e-mail.
You can also Subscribe without commenting.