Tutorial 2: Insert an image in the document - neeftarah/odtphp GitHub Wiki

The purpose of this tutorial is to show how to insert an image into the OpenOffice document generated with odtPHP. Adding an image to the generated document is substantially the same thing as the simple replacement of a tag by text content (tutorial 1). Note that for this example, you will have to indicate valid paths to existing images.

The PHP code

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

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

$odf->setVars('titre','Anaska formation');

$message = "Anaska, leader Français de la formation informatique ...";

$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.

The OpenOffice model (tutoriel2.odt)

{titre}

{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

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.