Skip to content

Document templates with odtPHP

World Wide Web Server edited this page Jul 4, 2012 · 7 revisions

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.

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

[b]2[/b]. 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.

[b]3[/b]. In the plugin folder create the file "odf_pi.php", and put this content inside [code] <?php if (!defined('BASEPATH')) exit('No direct script access allowed');

require_once("odf/odf.php"); [/code]

[b]4[/b]. 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 [code] define('TMP_PATH', str_replace(SELF, '', FILE) . 'tmp/'); [/code] just beneath this line [code] define('BASEPATH', $system_folder.'/'); [/code]

[b]5[/b]. Open file "odf.php", you've got this array at the beginning of the file [code] protected $config = array( 'ZIP_PROXY' => 'PclZipProxy', 'DELIMITER_LEFT' => '{', 'DELIMITER_RIGHT' => '}', 'PATH_TO_TMP' => null ); [/code] replace [code] 'PATH_TO_TMP' => null [/code] with [code] 'PATH_TO_TMP' => TMP_PATH [/code]

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 [code] {title} [/code]

[code]

$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'); [/code]

Clone this wiki locally