doccy Extension to manipulate with docx files (based on docxgen library)

  1. Requirements
  2. Instalation
  3. Code
  4. Usage
  5. Changelog

Doccy is a wrapper of the docxgen (phpdocx) extension which allows us to easily use already defined docx templates and replace the data dynamically from Yii application.




  1. Copy the extension to the extension folder (for example application.extensions.doccy)

  2. copy to that folder whole docxgen library (from docxgen)


You can download the code here or go to the github page to grab the latest code.


Add to the controller where you want to use extension following behaviour

public function behaviors()
       return array(
           'doccy' => array(
               'class' => 'ext.doccy.Doccy',

and finally use it in your controller

public function actionDownload()
	$this->doccy->newFile('template.docx'); // template.docx must be located in protected/views/report/template.docx where "report" is the name of the controller from which is renderDocx called (alternatively you must configure option "templatePath")
	$this->doccy->phpdocx->assignToHeader("#HEADER1#","HRADIeader 1"); // basic field mapping to header
	$this->doccy->phpdocx->assignToFooter("#FOOTER1#","Footer 1"); // basic field mapping to footer
	$this->doccy->phpdocx->assign("#TITLE1#","Pet shop BOYS list"); // basic field mapping
	$this->doccy->phpdocx->assignBlock("members",array(array("#NAME#"=>"John","#SURNAME#"=>"DOE"),array("#NAME#"=>"Jane","#SURNAME#"=>"DOE"))); // this would replicate two members block with the associated values
	$this->doccy->phpdocx->assignNestedBlock("pets",array(array("#PETNAME#"=>"Rex")),array("members"=>1)); // would create a block pets for john doe with the name rex
	$this->doccy->phpdocx->assignNestedBlock("pets",array(array("#PETNAME#"=>"Rox")),array("members"=>2)); // would create a block pets for jane doe with the name rox
	$this->doccy->phpdocx->assignNestedBlock("toys",array(array("#TOYNAME#"=>"Ball"),array("#TOYNAME#"=>"Frisbee"),array("#TOYNAME#"=>"Box")),array("members"=>1,"pets"=>1)); // would create a block toy for rex
	$this->doccy->phpdocx->assignNestedBlock("toys",array(array("#TOYNAME#"=>"Frisbee")),array("members"=>2,"pets"=>1)); // would create a block toy for rox
	$this->renderDocx("ExampleReport.docx", true); // use $forceDownload=false in order to (just) store file in the outputPath folder.

Please note that you must be familiar with the phpdocx in order to manipulate the data.


2012-11-29 - Initial release

5 0
Yii Version: 1.1
License: MIT
Category: Others
Developed by: Dino
Created on: Nov 29, 2012
Last updated: 5 years ago


show all