Monday, May 13, 2013

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:
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.
Notes: As I write this, PHPWord can create .docx, .rtf and .odt file.
  • Without more a do, go download the required package
Download CodeIgniter Download PHPWord
  • 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:
1
2
3
4
5
6
7
8
9
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
 
require_once APPPATH."/third_party/PHPWord.php"; 
 
class Word extends PHPWord { 
    public function __construct() { 
        parent::__construct(); 
    } 
}
  • 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)
1
2
3
$this->load->library('word');
//our docx will have 'lanscape' paper orientation
$section = $this->word->createSection(array('orientation'=>'landscape'));
  • Adding some text and style it!
1
2
3
4
5
6
7
8
9
10
11
// Add text elements
$section->addText('Hello World!');
$section->addTextBreak(1);
 
$section->addText('I am inline styled.', array('name'=>'Verdana', 'color'=>'006699'));
$section->addTextBreak(1);
 
$this->word->addFontStyle('rStyle', array('bold'=>true, 'italic'=>true, 'size'=>16));
$this->word->addParagraphStyle('pStyle', array('align'=>'center', 'spaceAfter'=>100));
$section->addText('I am styled by two style definitions.', 'rStyle', 'pStyle');
$section->addText('I have only a paragraph style definition.', null, 'pStyle');
  • 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)
1
2
3
4
5
6
7
8
// Add image elements
$section->addImage(FCPATH.'/image/_mars.jpg');
$section->addTextBreak(1);
 
$section->addImage(FCPATH.'/image/_earth.JPG', array('width'=>210, 'height'=>210, 'align'=>'center'));
$section->addTextBreak(1);
 
$section->addImage(FCPATH.'/image/_mars.jpg', array('width'=>100, 'height'=>100, 'align'=>'right'));
  • Create a table and give some style
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// Define table style arrays
$styleTable = array('borderSize'=>6, 'borderColor'=>'006699', 'cellMargin'=>80);
$styleFirstRow = array('borderBottomSize'=>18, 'borderBottomColor'=>'0000FF', 'bgColor'=>'66BBFF');
 
// Define cell style arrays
$styleCell = array('valign'=>'center');
$styleCellBTLR = array('valign'=>'center', 'textDirection'=>PHPWord_Style_Cell::TEXT_DIR_BTLR);
 
// Define font style for first row
$fontStyle = array('bold'=>true, 'align'=>'center');
 
// Add table style
$this->word->addTableStyle('myOwnTableStyle', $styleTable, $styleFirstRow);
 
// Add table
$table = $section->addTable('myOwnTableStyle');
 
// Add row
$table->addRow(900);
 
// Add cells
$table->addCell(2000, $styleCell)->addText('Row 1', $fontStyle);
$table->addCell(2000, $styleCell)->addText('Row 2', $fontStyle);
$table->addCell(2000, $styleCell)->addText('Row 3', $fontStyle);
$table->addCell(2000, $styleCell)->addText('Row 4', $fontStyle);
$table->addCell(500, $styleCellBTLR)->addText('Row 5', $fontStyle);
 
// Add more rows / cells
for($i = 1; $i <= 2; $i++) {
 $table->addRow();
 $table->addCell(2000)->addText("Cell $i");
 $table->addCell(2000)->addText("Cell $i");
 $table->addCell(2000)->addText("Cell $i");
 $table->addCell(2000)->addText("Cell $i");
 
 $text = ($i % 2 == 0) ? 'X' : '';
 $table->addCell(500)->addText($text);
}
  • Lastly, give the Ms Word document as .docx file to users without saving it to server’s hard-disk.
1
2
3
4
5
6
7
$filename='just_some_random_name.docx'; //save our document as this file name
header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document'); //mime type
header('Content-Disposition: attachment;filename="'.$filename.'"'); //tell browser what's the file name
header('Cache-Control: max-age=0'); //no cache
 
$objWriter = PHPWord_IOFactory::createWriter($this->word, 'Word2007');
$objWriter->save('php://output');
  • 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!

2 comments:

  1. How can i create the word document page in online and edit and save in cloud?

    ReplyDelete
  2. library are not working = PHPword

    ReplyDelete