Odtphp Documentation
Odtphp Documentation
The purpose of this tutorial is to show you the basic feature of odtPHP : simple variables
replacement.
$odf->setVars('titre', 'PHP');
$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.
{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
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.
$odf->setVars('titre','Anaska formation');
$odf->setVars('message', $message);
$odf->setImage('image', './images/anaska.jpg');
$odf->exportAsAttachedFile();
?>
1 - We create an object Odf by specifying the path to our template. Then, we call two times the
method setVars() of the created object in order to replace tags {titre} and {message} by text content.
2 - The method setImage() allows us to add an image to the OpenOffice document. The first
parameter of this method is the name of a tag somewhere in our template (without specifying the
delimiters{}). The second parameter is the path (relative or absolute) to an image stored on the
server. The tag will be replaced by the image in the document generated by odtPHP.
3 - When calling the method exportAsAttachedFile() , the library compress for us the desired
images into the document and send the result to the client browser.
{message}
{image}
To run this example, copy this template within an OpenOffice document named "tutoriel2.odt". In
the template, tags are delimited by two braces {}.
The result
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.
Tutorial 3 : Repeat a part of the document (Segment)
This tutorial purpose is to show how to repeat a part of your template with segments. We will see in
this example how to display a list of articles in our final OpenOffice document.
$odf->setVars('message', $message);
$listeArticles = array(
array( 'titre' => 'PHP',
'texte' => 'PHP est un langage de scripts (...)',
),
array( 'titre' => 'MySQL',
'texte' => 'MySQL est un systme de gestion de base de
donnes (...)',
),
array( 'titre' => 'Apache',
'texte' => 'Apache HTTP Server, souvent appel Apache
(...)',
),
)
$article = $odf->setSegment('articles');
foreach($listeArticles AS $element) {
$article->titreArticle($element['titre']);
$article->texteArticle($element['texte']);
$article->merge();
}
$odf->mergeSegment($article);
$odf->exportAsAttachedFile();
?>
1 - We create an Odf object by specifying the path to our template. Then, we call two times the
method setVars() of the created object in order to replace {titre} and {message} tags by text content.
2 - For the second step, we initialize in an array a data set that we want to fully display. The array
provides a list of articles consisting of a title and a text.
3 We initialize the segment "articles" with a call to the method setSegment() which takes as
argument the name of the segment and returns an object of type Segment.
4 - Then, We can loop on our data array. At each loop, we replace the tags {titreArticle} and
{texteArticle} of segment by content of the current line of the array. For that, we directly access to
the methods titreArticle() and texteArticle() of the object $article. You can also perform this
replacement in a classic way by calling the method setVar() of the object $article.
5 - Eventually, when we have added all our desired data, we can add the segment to the document
by calling the method mergeSegment() of the object $odf which takes as parameter our segment
$article.
{message}
{texteArticle}
The result
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.
Tutorial 4 : Imbricate several segments
The purpose of this tutorial is to show how to imbricate several segments within others segments.
Imbricating segments is a very simple task with odtPHP.
$categorie = $odf->setSegment('categories');
for ($j = 1; $j <= 2; $j++) {
$categorie->setVar('TitreCategorie', 'Catgorie ' . $j);
for ($i = 1; $i <= 3; $i++) {
$categorie->articles->titreArticle('Article ' . $i);
$categorie->articles->date(date('d/m/Y'));
$categorie->articles->merge();
}
for ($i = 1; $i <= 4; $i++) {
$categorie->commentaires->texteCommentaire('Commentaire '
. $i);
$categorie->commentaires->merge();
}
$categorie->merge();
}
$odf->mergeSegment($categorie);
$odf->exportAsAttachedFile();
?>
1 - We create an Odf object by specifying the path to our template. Then, we call the method
setVars() of the created object in order to replace the tag {titre} by text content.
2 We initialize the segment "categories" with a call to the method setSegment() which takes for
argument the name of the segment and returns an object of type Segment.
3 For this example, we want to add to the generated document two categories. So, we loop a first
time from 1 to 2. At each loop, we indicate a title to our current category with a call to the method
setVar().
4 In the loop, each child segment is accessible through the property of the same name of the father
segment. For instance, the segment "articles" is a child of segment "categories". So, we can access
to it directely with $categorie->articles. Thus, we can replace variables of this child segment with
the method that has the same name of the tag to replace. For instance : $categorie->articles-
>titreArticle(). Alternatively, we can also call the method setVar() which will perform the same
replacement.
5 For each child and for the segment "categories", we call the method merge() in order to merge
data for each loop. Only one call to mergeSegment() is required at the end to add all our segments
to the generated document.
The OpenOffice model (tutoriel4.odt)
{titre}
Articles :
[!-- BEGIN articles --]
- {titreArticle}
Date de publication : {date}[!-- END articles --]
Commentaires :
[!-- BEGIN commentaires --]
Texte : {texteCommentaire}[!-- END commentaires --]
[!-- END categories --]
To run this example, copy this template within an OpenOffice document named "tutoriel4.odt". In
the template, we can imbricate segments easily. The segment "categories" contains a segment
"articles" and another segment "commentaires".
The result
require_once('../library/odf.php');
$odf->setVars('message', $message);
$listeArticles = array(
array( 'titre' => 'PHP',
'texte' => 'PHP, est un langage de scripts (...)',
'image' => './images/php.gif'
),
array( 'titre' => 'MySQL',
'texte' => 'MySQL est un systme de gestion de base de
donnes (...)',
'image' => './images/mysql.gif'
),
array( 'titre' => 'Apache',
'texte' => 'Apache HTTP Server, souvent appel Apache
(...)',
'image' => './images/apache.gif'
)
);
$article = $odf->setSegment('articles');
foreach($listeArticles AS $element) {
$article->titreArticle($element['titre']);
$article->texteArticle($element['texte']);
$article->setImage('image', $element['image']);
$article->merge();
}
$odf->mergeSegment($article);
$odf->exportAsAttachedFile();
?>
1 - We create an Odf object by specifying the path to our template. Then, we call two times the
method setVars() of the created object in order to replace {titre} and {message} tags by text content.
2 - For the second step, we initialize in an array a data set that we want to fully display. The array
provides a list of articles consisting of a title, a text and an image.
3 We initialize the segment "articles" with a call to the method setSegment() which takes as
argument the name of the segment and returns an object of type Segment.
4 - Then, We can loop on our data array. At each loop, we replace the tags {titreArticle} and
{texteArticle} of segment by content of the current line of the array. For that, we directly access to
the methods titreArticle() and texteArticle() of the object $article. You can also perform this
replacement in a classic way by calling the method setVar() of the object $article. In the same way,
we call the method setImage() in order to replace the tag image by an image of our choice. When
we have made these replacements, we merge the segment by calling the method merge() of the
object $article.
5 - Eventually, when we have added all our desired data, we can add the segment to the document
by calling the method mergeSegment() of the object $odf which takes as parameter our segment
$article.
{message}
{texteArticle}
{image}
$odf->setVars('message', $message);
$listeArticles = array(
array( 'titre' => 'PHP',
'texte' => 'PHP est un langage de scripts (...)',
),
array( 'titre' => 'MySQL',
'texte' => 'MySQL est un systme de gestion de base de
donnes (...)',
),
array( 'titre' => 'Apache',
'texte' => 'Apache HTTP Server, souvent appel Apache
(...)',
),
);
$article = $odf->setSegment('articles');
foreach($listeArticles AS $element) {
$article->titreArticle($element['titre']);
$article->texteArticle($element['texte']);
$article->merge();
}
$odf->mergeSegment($article);
$odf->exportAsAttachedFile();
?>
1 - We create an Odf object by specifying the path to our template. Then, we call two times the
method setVars() of the created object in order to replace {titre} and {message} tags by text content.
2 - For the second step, we initialize in an array a data set that we want to fully display. The array
provides a list of articles consisting of a title and a text.
3 We initialize the segment "articles" with a call to the method setSegment() which takes as
argument the name of the segment (without the prefix "row.") and returns an object of type
Segment.
4 - Then, We can loop on our data array. At each loop, we replace the tags {titreArticle} and
{texteArticle} of segment by content of the current line of the array. For that, we directly access to
the methods titreArticle() and texteArticle() of the object $article. You can also perform this
replacement in a classic way by calling the method setVar() of the object $article.
5 - Eventually, when we have added all our desired data, we can add the segment to the document
by calling the method mergeSegment() of the object $odf which takes as parameter our segment
$article.
To run this example, copy this template within an OpenOffice document named "tutoriel6.odt". This
template requires to create a table under OpenOffice. Nmaes of segments within a line of array must
be prefixed by "row." in order to be handled correctly.
The result
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.
$config = array(
'ZIP_PROXY' => 'PhpZipProxy', // Make sure you have Zip extens
ion loaded
'DELIMITER_LEFT' => '#', // Yan can also change delimiters
'DELIMITER_RIGHT' => '#'
);
$odf->setVars('message', $message);
$odf->exportAsAttachedFile();
?>
Change the behaviour of odtPHP is very easy. you just have to give a configuration array for the
second parameter of the Odf class constructor. The keys of this array are configuration names to
modify : ZIP_PROXY, DELIMITER_LEFT and DELIMITER_RIGHT.
ZIP_PROXY is used to modify the proxy to use for handling Zip compression/decompression
within odtPHP. By default, 'PclZip' is used. It consists of an interface with the PclZip library. You
can also use 'PhpZipProxy'. In this case, odtPHP will directly use Zip extension from PHP. Note that
you will have to activate this extension in your PHP configuration. Moreover, note that this
extension is bugged since PHP 5.2.7.
DELIMITER_LEFT and DELIMITER_RIGHT are used to personalize delimiters of tags for the
template. By default, these delimiters are braces : {}.
#message#
Here, we have choosen to use the character # as delimiter of tags.
The result
The result
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.
$encode is a boolean. If this parameter is equal to true, special HTML characters (<, >, ", &) will be
encoded in entities. By default, this parameter equals to true. If you set it to false, you will be able
to insert raw XML data.
$charset is name of a characters set. By default, this parameter equals to 'ISO-8859'. Data are so
automatically encoded in UTF-8 before adding them in the XML document. If you set this
parameter to 'UTF-8', then data will not be encoded a second time in UTF-8. This can be useful to
provide to the template data that are already encoded in UTF-8.
$odf->saveToDisk('fichier.odt');
?>
The method exportAsAttachedFile() allows you to send the result directly to the client browser. Be
careful, before calling this method, make sure that HTTP headers have not been already sent.
La mthode saveToDisk() enregistre le rsultat sur le serveur. Elle prend en paramtre un chemin de
fichier valide. Notez que si vous appelez saveToDisk() sans paramtre, odtPHP effectuera
simplement une sauvegarde interne du document en cours de gnration.
The method saveToDisk() saves the result on the server. It takes as parameter a valid path to the file.
Note that if you call saveToDisk() without parameter, odtPHP will just perform an internal saving of
the current document.