Create an ODT document template

This page describe how to build an ODT document template to build documents using ODT generation.

To know how to build a PDF template, see page Create a PDF document template. Building a PDF template require PHP development knowledge, but not building an ODT template.

Prerequisite

 * Dolibarr: 3.1+
 * OpenOffice: 3.2+, LibreOffice, ...

Create your document
Include in your document the tags you want to see. Tags will be replaced during generation of document automatically by Dolibarr. List of all available tags are provided in next chapter.
 * Create from scratch an empty openoffice document and take a provided example. You may find them installed into a subdirectory of directory documents/doctemplates
 * Edit your document using all your Office suite features.

Warning, tags must be rounded with {} or [] for arrays (see later) and must be typed with no stop using Office suite (with no backward and no copy-paste). If not doing that, Office suite add some invisible informations making replacement not possible.

Tags
This is a list of tags that will be replaced with your informations:

Other information
current_date = {current_date} (Dolibarr >= 3.4.0) current_datehour = {current_datehour} (Dolibarr >= 3.4.0) current_server_date = {current_server_date} (Dolibarr >= 3.4.0) current_server_datehour = {current_server_datehour} (Dolibarr >= 3.4.0)

Lines of object
This is how to use arrays for lines of objects (invoices, commercial proposal, orders, etc...). You must create your array in the document and use a begin and end tag to define a line of the array. The line will then be repeated as much as required during generation. [!-- BEGIN row.lines --] ... [!-- END row.lines --]

If you want to display it not in tables row but in block lines : [!-- BEGIN lines --] ... [!-- END lines --]

Then, you can use following tags into your line:

This an example of what you may have in your office suite:

Conditional substitution
Starting from Dolibarr v3.3, you can use conditional substitutions, which in layman terms means that you can decide to print something if a variable is true, or print something else if it's false (or nothing at all) - including text but also any more complex structures like tables and images.

Example: [!-- IF {my_var} --] Print this text if {my_var} is true (can be any value but null/0/empty string) [!-- ELSE {my_var} --] Or print this if it's false (null/0/empty string) [!-- ENDIF {my_var} --]

Note: of course, the ELSE tag and block is optional, you can just use IF/ENDIF if you prefer.

WARNING: the format of this special tag is very precise and picky, be careful to put one space between:
 * IF/ELSE/ENDIF
 * {my var}
 * --]
 * --]

Other personalized tags
If you want to add a field that is not predefined, this is the solution: htdocs/mymodule/core/substitutions/functions_mymodule.lib.php
 * Create a file into the location:
 * Inside this file, just write one function like this:

Function will be called before generating document to complete the array used for substitution and to replace tag myowntag with value $myvalue. You can add as many tags as you need and put all personalized code you want to define values (search into database, into files, calculation, from received parameters or from global variables)...

Warning: First parameter in function parameter declaration must start with & as value is modified by your code and must be returned modified.

To have your substitution function be called, if you have to :
 * Check your substitution file is inside subdirectory module called htdocs/mymodule/core/substitutions.
 * Build your own module (See page Module development) with its descriptor file.
 * Check your module descriptor file contains an entry to declare this module has a substitutions file
 * Enable your module (module must be enabled and disabled each time you change the descriptor).

Note2: this only allows to define global, static ODT variables (ie: to make dynamic variables that changes for every line of product, please see the next chapter).

Other personalized tags for lines
This function is available since Dolibarr v3.3 (future development version).

Similarly to the previous chapter, you have to define a function, and it works about the same, but with a few twists:


 * Create or edit the same file as before (see the previous chapter).
 * Here are the twists: in the file, write another function with almost the same name but with '_lines' appended, and also add a new $lines variable:

Contrary to the previous function which was called only once, this function will be called everytime a line of product will be processed, giving you each time a new $line object that you can process. This allows you to do different substitutions for each product.

Store your document
To have your document to appear in list of available documents, put it into correct subdirectory found into directory documents/doctemplates