Search by

james2001 / odtphp

james2001

Fill OpenDocument Text (.odt) templates from PHP: variables, repeated segments, images and custom properties

Package info

github.com/james2001/odtphp

pkg:composer/james2001/odtphp

Statistics

Installs: 54

Dependents: 1

Suggesters: 0

Stars: 0

Open Issues: 0

2.0.1 2026-12-01 10:09 UTC

This package is auto-updated.

Last update: 2026-12-01 11:05:24 UTC


README

CI Latest Stable Version Total Downloads License

Fill OpenDocument Text (.odt) templates from PHP: replace {variables}, repeat [!-- BEGIN segment --]…[!-- END segment --] blocks (nested blocks too), insert images and set custom document properties. The generated documents open in LibreOffice, OpenOffice, Microsoft Word, etc.

james2001/odtphp is a maintained fork of cybermonde/odtphp that includes the work of sboden/odtphp.

Versions

Version Branch PHP Based on Status
2.x 2.x 8.1 and later sboden/odtphp 3.2 Active
1.x 1.x 5.3.3 to 8.x cybermonde/odtphp master + sboden/odtphp 2.2 Maintenance (bugfixes)

You are reading the documentation of the 2.x branch.

composer require james2001/odtphp installs the most recent version compatible with your PHP version.

For Symfony applications, see james2001/odtbundle.

Installation

composer require james2001/odtphp

The default ZIP backend needs the zip extension. Without it, use the bundled PclZip backend: new Odf('template.odt', ['ZIP_PROXY' => \Odtphp\Zip\PclZipProxy::class]). Values in another charset than UTF-8 are converted with the mbstring extension.

Usage

In the template, write variables between braces ({title}) and surround repeated blocks with [!-- BEGIN articles --] and [!-- END articles --].

use Odtphp\Odf;

$odf = new Odf('template.odt');
$odf->setVars('title', 'My articles');
$odf->setImage('logo', '/path/to/logo.png');

$articles = $odf->setSegment('articles');
foreach ($items as $item) {
    $articles->setVars('name', $item['name']);
    $articles->setImage('picture', $item['picture_path']);
    $articles->merge();
}
$odf->mergeSegment($articles);

$odf->setCustomProperty('Author', 'John Doe');

$odf->saveToDisk('/path/to/result.odt');
// or send it to the browser: $odf->exportAsAttachedFile('result.odt');

setVars($key, $value, $encode = true, $charset = 'UTF-8') escapes special XML characters unless $encode is false, and converts the value to UTF-8 when $charset is another charset.

Images can be sized in centimeters (setImage()), millimeters (setImageMm()) or pixels (setImagePixel()): setImage($key, $path, $page = -1, $width = null, $height = null, $offsetX = null, $offsetY = null). $page = -1 anchors the image to the text; a page number anchors it to that page with the given offsets.

LibreOffice sometimes stores a tag in several pieces ({na<text:span …>me</text:span>}), for instance after a spell check or when part of it was retyped. odtphp repairs these tags when it opens the template. Images must be local files; give only the width or only the height to keep the aspect ratio.

Custom properties (setCustomProperty()) must already exist in the template (File > Properties > Custom Properties in LibreOffice). Microsoft Word does not refresh them when opening the file.

Configuration keys (second argument of new Odf()):

Key Default Description
ZIP_PROXY Odtphp\Zip\PhpZipProxy ZIP backend (Odtphp\Zip\PclZipProxy also available)
DELIMITER_LEFT { Left delimiter of variables
DELIMITER_RIGHT } Right delimiter of variables
PATH_TO_TMP sys_get_temp_dir() Directory of the temporary working files

Upgrading from 1.x

  • PHP 8.1 or later is required.
  • setVars() expects UTF-8 by default (1.x expected ISO-8859-1). Pass the charset of your values otherwise, e.g. setVars('name', $value, true, 'ISO-8859-1'); the 1.x name 'ISO-8859' is still accepted. Converting from another charset needs the mbstring extension.
  • The classes use native types (declare(strict_types=1) in Odf): classes extending them must declare compatible signatures.
  • New: setImageMm(), setImagePixel(), setCustomProperty(), customPropertyExists(), getAllConfig().

Migrating from another odtphp package

  • From sboden/odtphp 3.x: same classes, replace the package (composer remove sboden/odtphp && composer require james2001/odtphp).
  • From cybermonde/odtphp 1.x: the classes are now namespaced, replace Odf by Odtphp\Odf and Segment by Odtphp\Segment, then read "Upgrading from 1.x".

These packages declare the same classes, so Composer refuses to install them together.

Tests

composer install
vendor/bin/phpunit

Most tests run twice, with the ZipArchive and the PclZip backends, and compare the generated documents with the reference files of tests/src/files/gold_*.

Credits

odtphp was created by Julien Pauli, Olivier Booklage and Vincent Brouté (odtphp.com, Anaska), with Cyril Pierre de Geyer. It includes fixes by Vikas Mahajan, the packaging of cybermonde and the PHP 8 rewrite of sboden. The bundled PclZip library is by Vincent Blavet (LGPL).

License

GPL-3.0, see LICENSE.