How to create DOCX views

From Joomla! Documentation

The following tutorial will demonstrate how to generate a view as a Microsoft Word document. This example is from a Recipe component and we will add the ability to save the recipe as a Word file. You may note this pattern of steps could also be used to create PDF views in a component with the use of a PDF library such as TCPDF.

Contents

Download PHPdocx

PHPdocx is a PHP library which can help you dynamically generate Word documents. Both a Free and a Pro version are available for download. Visit http://www.phpdocx.com to select the version you prefer and download the package.

We will add the new library within the 'libraries' directory on your website. This would allow you to easily access the library with other components if desired. Create a new subdirectory beneath 'libraries' on your site named phpdocx. Extract the zip file on your computer and upload the contents to the new subdirectory.

Create the DOCX view

In the frontend of the component structure, we will create a docx view for the recipe. Create the file <component>/views/<view>/view.docx.php. Links that include "&tmpl=component&format=docx" will look for this file in the relevant view folder.

Assuming you are replicating the content of default.php to be shown instead in Word, the display() method of our View class will be the same as view.html.php with one important change. Be sure to set $tpl=docx rather than $tpl=null as an argument of the display() function.

Create Word document generator

Rather than naming our template file default.php as we would with an HTML view, we will include the format in the name. Create the file <component>/views/<view>/tmpl/default_docx.php. A simple example which can be done with the Free version of PHPdocx is shown below.

// No direct accessdefined('_JEXEC') or die('Restricted access');// include the PHPdocx libraryrequire_once JPATH_SITE.'/libraries/phpdocx/classes/CreateDocx.inc';$docx=new CreateDocx();// Add a Page Headerif($this->item->params->get('word_header')){$paramsHeader=array('name'=>'images/logo.gif','jc'=>'center','i'=>'single');$docx->addHeader('Document Heading',$paramsHeader);}// Add a Page Footerif($this->item->params->get('word_footer')){$paramsFooter=array('pager'=>'true','pagerAlignment'=>'right');$docx->addFooter('Sitename',$paramsFooter);}// Add the Recipe title$paramsText=array('font'=>'Arial','sz'=12);$docx->addText($this->item->title,$paramsText);// Generate the document$docx->createDocx('tmp/filename');// Force the document to the browser$app= JFactory::getApplication();$app->redirect('tmp/filename.docx');

Obviously, the above example is of little real value, but demonstrates some basic concepts in working with the PHPdocx library.

Notice that the location and filename of the Word document is determined by the argument you provide the createDocx() method. In this case the file is created in the tmp/ directory off our site root and saved as filename.docx.

The Free version of the library does not include the CreateDocxAndDownload() method (which would send the created file to the browser), so I've included the step of using $app->redirect() to get the file.

The CreateDocx.inc file is well commented to help familiarize you with the possible attribute/value pairs of each method. The Pro version of the library allows much more flexibility in content generation, including translation of HTML and CSS styles to WordML and use of the MailMerge function. The image below will give you an idea of what is possible with the expanded library.

Creating a Word icon button

Now that we have our DOCX view in place, we'll want to create an icon (similar to the print and email icons in Articles) with which to call it. Create the file <component>/helpers/icon.php.

The last argument in the JHtml image method above is important as an indicator of where the system will look for the word_icon.gif image. By setting the value to false, we are telling it to look for the specific path and file name we've provided, directly from the site root. In this case, it will be in images/, the default directory for Media Manager.

Add the Icon to the HTML view

To finish up, we open our default.php file and add something similar to the following: