wordpress hit counter
PHP Library for the Dynamical Generation of Microsoft Word - OpenXML Developer - Blog - OpenXML Developer
Goodbye and Hello

OpenXmlDeveloper.org is Shutting Down

There is a time for all good things to come to an end, and the time has come to shut down OpenXmlDeveloper.org.

Screen-casts and blog posts: Content on OpenXmlDeveloper.org will be moving to EricWhite.com.

Forums: We are moving the forums to EricWhite.com and StackOverflow.com. Please do not post in the forums on OpenXmlDeveloper.org. Instead, please post in the forums at EricWhite.com or at StackOverflow.com.

Please see this blog post for more information about my plans moving forward.  Cheers, Eric

PHP Library for the Dynamical Generation of Microsoft Word

PHP Library for the Dynamical Generation of Microsoft Word

  • Comments 4


By Eduardo Ramos




The Spanish National Research Council asked if we could generate some dynamic reports from the data gathered for its 2009 Action Plan. 


These reports had to be generated both in PDF and Word format because they require editable reports that could be suitably modified and reused for presentations or any other purpose.


Due to technical reasons we were bound to use a LAMP (Linux Apache MySQL PHP) platform. We were aware at that time (although not so many people are) that the latest versions of Word were based on an Open XML standard (WordProcessingML) so we started looking for some PHP classes that will do, at least, part of the work for us in the same way that TCPDF was helping us for the generation of the PDFs.


After a lot of searching we realized that there was nothing appropriate for what we were looking for: a PHP library that would allow you to generate in a reasonably simple manner a Word (.docx) document from scratch on a LAMP server.

So there were two options:

1.    Go back to a client with a ‘No’

2.    Start on building all the required classes and methods in PHP

It was clear to us from the very beginning that if we were to invest effort we should do it so that the outcome would be usable and reusable for us and others.


We made a start by creating a PHP library that would allow a reasonably skilled programmer to build a sophisticated Word document programmatically from data recorded in a MySQL database, or any other similar data source.


The result: PHPDOCX. This PHP library is offered for free under a LGPL license, although there is also a PRO version eligible for support. PHPDOCX allows, with a few lines of code, to create a Word (.docx) document with:

  • Standard paragraphs with widow/orphan control
  • Bulleted or numbered (nested) lists
  • Customized tables
  • An array of editable charts and graphs to display numerical data
  • Table of content to simplify readability within long documents
  • Headers that will reflect corporate branding
  • Footers with automatic page numbering
  • Graphic elements
  • Text boxes
  • Headings
  • Math formulas
  • Sections, and many other features that can be checked out in the website of the project

PHPDOCX, although originally conceived to run on a LAMP server, only requires PHP 4 or 5 (recommended) and could be equally used on a Windows platform with practically any data source.


Sample Code


In order to get a better grasp of the functionality of the library I will now show you how to create a sample Word document that includes a few of the above features.




   * @author 2mdc.com

   * @version





  $objDocx = new cCreateDocx();


  $arrParamsHeaderOne = array(

    'jc' => 'center', //justification

        'color' => 'FF0000', //font color

        'sz' => 16 //size



  $objDocx->fAddHeader('PHPDOCX v1', $arrParamsHeaderOne);


    $arrParamsHeaderTwo = array(

    'jc' => 'center', //justification

        'color' => 'FF0000', //font color

        'sz' => 12 //size



  $objDocx->fAddHeader('By 2mdc.com', $arrParamsHeaderTwo);


  $arrDatos = array(

        '0' => array('sequence 1', 'sequence 2', 'sequence 3'),

        'Category 1' => array(9.3,2.4,2),

        'Category 2' => array(2.5,4.4,1),

        'Category 3' => array(3.5,1.8,0.5),

        'Category 4' => array(1.5,8,1)



  $arrArgs = array('data' => $arrDatos,

    'type' => 'colChart',

    'title' => 'phpdocx Chart Example',

        'color' => 2,

        'textWrap' => 0,

        'sizeX' => 17,'sizeY' => 7,//in cm

        'jc' => 'center',

        'textWrap' => 1





  $arrParamsTitle = array(

        'val' => 1,

    'u' => 'single', //underline

    'jc' => 'left' //justification



  $objDocx->fAddTitle('What is PHPDOCX ?', $arrParamsTitle);


  $objDocx->fAddText('PHPDOCX is a PHP library designed to dynamically generate reports in Word format (WordprocessingML).');

  $strParamsBreak = 'line'; //line break


  $arrParamsImg = array('name' => './angel/softwars.jpg', //img source

    'textWrap'=> 1,

        'jc' => 'center' //justification






 $arrDatsList = array(

    'Required : PHP4 or PHP5',

    'Dynamic generation of simple Microsoft Office Documents (.docx) in PHP at no charge',

    'See features',

    'LGPL license',

    'No support'



  $arrParamsList = array(

    'val' => 2 //numbered list



  $objList = $objDocx->fAddElement('fAddList',array($arrDatsList, $arrParamsList));



  $arrDatsList = array(

    'Required : PHP4 or PHP5',

    'Dynamic generation of sophisticated Microsoft Office Documents (.docx) in PHP at a very affordable price (90 euros)',

    'See features',

    'License conditions',

    'Technical support'



  $arrParamsList = array(

    'val' => 2 //numbered list



  $objList2 = $objDocx->fAddElement('fAddList',array($arrDatsList, $arrParamsList));


  $arrTable = array(







  $arrParamsTable = array(

    'TBLLOOKval' => 'ffff01E0',

    'TBLSTYLEval' => 'TableGrid',

    'TBLWtype' => 'center',

    'TBLWw' => '50',

    'border' => 'none',

          'border_sz' => 0,

        'jc' => 'center',

          'size_col' => 1200000



  $objDocx->fAddTable($arrTable, $arrParamsTable);



    $arrParamsFooter = array(

    'pager' => 'true', //page numbering

    'pagerAlignment' => 'center' //number justification



  $objDocx->fAddFooter('www.phpdocx.com', $arrParamsFooter);


  $arrParamsPage = array('titlePage' => 1, 'orient' => 'normal', 'top' => 0, 'bottom' => 0, 'right' => 0, 'left' => 0); //general properties






You may download the generated document from: http://www.phpdocx.com/samples/sample_phpdocx.docx


I hope that you consider this library of interest and help to “spread the WordTM” J


Attachment: PHP Library for the Dynamical Generation of MS Word_Eduardo Ramos.zip
Page 1 of 1 (4 items)