Tutorial 1 : Minimal example simple variables replacements - neeftarah/odtphp GitHub Wiki

The purpose of this tutorial is to show you the basic feature of odtPHP : simple variables replacement.

The PHP code

<?php
require_once('../library/odf.php');

$odf = new odf("tutoriel1.odt");

$odf->setVars('titre', 'PHP');

$message = "PHP  est un langage de scripts libre ...";

$odf->setVars('message', $message);

$odf->exportAsAttachedFile();
?>
  1. After including the odtPHP library, we can create a new object with the constructor of the Odf class. This constructor takes one mandatory parameter : the path (relative or absolute) to the OpenOffice document template.

  2. Then, we can call the method setVars() from the object $odf in order to replace the tag {titre} from the template by the 'PHP' text. The first mandatory parameter of the method setVars() is the tag name in the template (without specifying the delimiters {}). The second mandatory parameter is the text content that will replace this tag within the final document.

  3. We call this method a second time in order to replace the tag {message} from template by the text 'PHP est un langage de scripts libre...'.

  4. Eventually, we call the method exportAsAttachedFile() in order to send the result directly to the client browser.

The OpenOffice model (tutoriel1.odt)

{titre}

{message}

To run this example, copy this template within an OpenOffice document named "tutoriel1.odt". In the template, tags are delimited by two braces {}.

The result

tutorial odtPHP

Note : the texts in the PHP code are shorter than the result in order to make it more readable. Styles (colors, layouts) of this result come from our template.