Skip to content

Document templates with odtPHP

Derek Jones edited this page · 7 revisions
Clone this wiki locally

Category:Plugin

If you want to generate documents from your data (database, xml or any other source), I've found that odtPHP does a very good job. So here is a simple tutorial on how to use this library.

1. Download odtPHP from http://www.odtphp.com/index.php?i=download

2. Extract the zip file somewhere where you like, you will see there will be two folders "library" and "tests". Rename the "library" folder to "odtPHP" and put it in your codeigniter "plugin" folder.

3. In the plugin folder create the file "odf_pi.php", and put this content inside


<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

require_once("odf/odf.php");

4. For the library to work, you need a temporary folder where odfPHP can write to when generating the document. Create a folder in the root of your application, name it for example "tmp", then open your index.php file of your CI app, and put this line


define('TMP_PATH', str_replace(SELF, '', __FILE__) . 'tmp/');

just beneath this line


define('BASEPATH', $system_folder.'/');

5. Open file "odf.php", you've got this array at the beginning of the file


protected $config = array(
        'ZIP_PROXY' => 'PclZipProxy',
        'DELIMITER_LEFT' => '{',
        'DELIMITER_RIGHT' => '}',
        'PATH_TO_TMP' => null
       );

replace


'PATH_TO_TMP' => null

with


'PATH_TO_TMP' => TMP_PATH

With this final step, the configuration is done. Now here is a simple function on how to use the plugin.

When creating the template with Open Office, make sure you put the variables to be filled, like in this example


{title}

function code to export the generated document



$this->load->plugin('odf');

//path of the template file
$odf = new odf("assets/odt/template.odt");

//fill the template with the variables
$odf->setVars('title', 'Hello World!!!');

//export the file
$odf->exportAsAttachedFile('export.odt');
Something went wrong with that request. Please try again.