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:
Notes: As I write this, PHPWord can create .docx, .rtf and .odt file.
Your application/third_party will look like this
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(); } } |
1 2 3 | $this->load->library('word'); //our docx will have 'lanscape' paper orientation $section = $this->word->createSection(array('orientation'=>'landscape')); |
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'); |
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')); |
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); } |
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'); |
Now our CodeIgniter become more complete, we can create Excel file, create a PDF file and, in this tutorial, creating Microsoft Word 2007 document!
well done , greats
Yes, it was that easy. I didn’t try PHPPowerPoint but maybe you could give it a shot. I bet it’s just as easy.
Thanks for making these posts. It shows just how powerful CI is.
mas, kalau ada 25 kotak html, masing2 di harus isi dengan text yang beraturan ke 25 kotak tersebut, caranya ngisi enak lewat 1 form khusus isi 25 textedit kebawah dan 1x submit ? atau dari 25 kotak di klik 1x, isi text, save by modal window (jquery)sampe 25x ulang ?, mending mana dan nyaman mana ? thanks
kalau sudah diniatkan pakai jQuery, lebih baik diletakkan di satu form dan 1x submit saja.
Karena isiannya kelihatannya banyak (25 buah), mending dibikin tabular atau accordion (jquery ui).
kalau kotaknya (text item-nya) di klik sebanyak 25x perulangan (5×5), baru klik tombol save …kurang nyaman ya ?
capek nge-klik nya..
klo mau model begitu, user harus diaksih tau, dia harus ngeklik berapa kali lagi, biar ndak merasa putus asa ngisi form nya
ok mas, iya sih, (klik 1x , isi text, klik lagi utk save text di kotak tsb sampe 25 kotak terisi dan klik submit …kayane enak model form biasa menurun ke bawah tinggal di atur biar ndak keliatan ruwet
thanks mas
how can we save it on our server ?
so, instead of
you should do:
makasih, sukses nyoba semua tutorialnya di phpword kecuali untuk templatenya.
yang ini:
loadTemplate(‘Template.docx’);
$document->setValue(‘Value1′, ‘Sun’);
$document->setValue(‘Value2′, ‘Mercury’);
$document->setValue(‘Value3′, ‘Venus’);
$document->setValue(‘Value4′, ‘Earth’);
$document->setValue(‘Value5′, ‘Mars’);
$document->setValue(‘Value6′, ‘Jupiter’);
$document->setValue(‘Value7′, ‘Saturn’);
$document->setValue(‘Value8′, ‘Uranus’);
$document->setValue(‘Value9′, ‘Neptun’);
$document->setValue(‘Value10′, ‘Pluto’);
$document->setValue(‘weekday’, date(‘l’));
$document->setValue(‘time’, date(‘H:i’));
$document->save(‘Solarsystem.docx’);
?>
kenapa error terus di setValuenya?
Thank you for the nice library. I was using a UTF-8 (unicode) fonts for the text which is to be generated in the word file.
How do I do it ?
Additionally how to set the format by the html tags ?
can you help me? i have tried implement ‘Template’ example file to CI, the idea is saving to client (not saving to server), but it didn’t work as expected
here is the code
class php_word_controller extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->library(‘word’);
}
function index() {
$templatefile = FCPATH.’assets/Template.docx’;
$PHPWord = $this->word;
$section = $PHPWord->loadTemplate($templatefile);
$section->setValue(‘Value1′,’Sun’);
$section->setValue(‘Value2′,’Mercury’);
$section->setValue(‘Value3′,’Venus’);
$section->setValue(‘Value4′,’Earth’);
$section->setValue(‘Value5′,’Mars’);
$section->setValue(‘Value6′,’Jupiter’);
$section->setValue(‘Value7′,’Saturn’);
$section->setValue(‘Value8′,’Uranus’);
$section->setValue(‘Value9′,’Neptun’);
$section->setValue(‘Value10′,’Pluto’);
$section->setValue(‘weekday’,date(‘l’));
$section->setValue(‘time’,date(‘H:i’));
$fname=”Test-Template-PHPWORD.docx”;
header(‘Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document’); // mime type
header(‘Content-Disposition: attachment;filename=”‘.$fname.’”‘); // tell browser what’s the file name
header(‘Cache-Control: max-age=0′); // no cache
$objWriter = PHPWord_IOFactory::createWriter($PHPWord,’Word2007′);
$objWriter->save(‘php://output’);
}
}
mas gimana caranya load hasilnya ke view.. baru pelajari nie..
I’m sorry to disturb you,how to generate html file to word?
is there anything method or material?
Look forward to your reply! thanks!