Archived

How to create DOCX views

From Joomla! Documentation

Revision as of 22:23, 26 April 2022 by M-b-o (talk | contribs) (M-b-o moved page J2.5:How to create DOCX views to Archived:How to create DOCX views)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

This page has been archived. This page contains information for an unsupported Joomla! version or is no longer relevant. It exists only as a historical reference, it will not be improved and its content may be incomplete and/or contain broken links.

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.

Download PHPdocx[edit]

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[edit]

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.

class RecipesViewRecipe extends JView
{
	function display($tpl='docx')
	{
		// Get data from model
		$item = $this->get('Item');
		$state = $this->get('State');
		
		// Check for errors
		if (count($errors = $this->get('Errors'))) {
			JError::raiseError(500, implode('<br />', $errors));
			return false;
		}
		
		// Add params
		$this->params = $this->state->get('params');
		
		// Assign to template
		$this->item = $item;
		$this->state = $state;
		
		parent::display($tpl);
	}
}

Create Word document generator[edit]

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 access
 defined('_JEXEC') or die('Restricted access');

 // include the PHPdocx library
 require_once JPATH_SITE.'/libraries/phpdocx/classes/CreateDocx.inc';
 
 $docx = new CreateDocx();
 
 // Add a Page Header
 if ($this->item->params->get('word_header')) {
 	$paramsHeader = array('name'=>'images/logo.gif', 'jc'=>'center', 'i'=>'single');
	$docx->addHeader('Document Heading', $paramsHeader);
 }
 
 // Add a Page Footer
 if ($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.

Phpdocx-example.jpg

Creating a Word icon button[edit]

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.

 // No Direct Access
 defined('_JEXEC') or die('Restricted access');
 
 // Component HTML Helper
 class JHtmlIcon
 {
 	static function msword($item, $params, $attribs = array())
	{
		$url = 'index.php?option=<component>&view=<view>&id='.$item->id.'&tmpl=component&format=docx';
		$text = JHtml::_('image', 'images/word_icon.gif', JText::_('<component>_ICON_MSWORD'), NULL, false);
		$attribs['title'] = JText::_('<component>_ICON_MSWORD');
		
		$output = JHtml::_('link', JRoute::_($url), $text, $attribs);
		return $output;
	}
 }

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[edit]

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

 <?php if ($params->get('show_word_icon')) : ?>
 	<ul class="actions">
		<li class="word-icon">
			<?php echo JHtml::_('icon.msword', $this->item, $params); ?>
		</li>
	</ul>
 <?php endif; ?>

Contributors[edit]

Denise McLaurin