james2001 / odtbundle
Symfony bundle to fill ODT (OpenDocument Text) templates with the odtphp library
Requires
- php: >=8.1
- james2001/odtphp: ^2.0
- symfony/config: ^6.4 || ^7.0 || ^8.0
- symfony/dependency-injection: ^6.4 || ^7.0 || ^8.0
- symfony/http-kernel: ^6.4 || ^7.0 || ^8.0
Requires (Dev)
- phpunit/phpunit: ^10.5.62 || ^11.5.50 || ^12.5.8
- symfony/framework-bundle: ^6.4 || ^7.0 || ^8.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Symfony bundle that fills ODT (OpenDocument Text) templates: variables, repeated segments and images, thanks to the odtphp library.
Which version should I use?
| Bundle | Branch | PHP | Symfony | ODT library |
|---|---|---|---|---|
| 3.x | 3.x |
8.1+ | 6.4, 7.x, 8.x | james2001/odtphp 2.x |
| 2.x | 2.x |
7.2.5 – 8.x | 4.4, 5.4 | james2001/odtphp 1.x or 2.x |
| 1.x | 1.x |
5.3.3 – 7.4 | 2.3 – 3.4 | cybermonde/odtphp 1.x |
You don't have to pick by hand: composer require james2001/odtbundle installs
the newest version compatible with your PHP and Symfony versions.
This branch (3.x) is the active one, for Symfony 6.4, 7.x and 8.x applications.
Installation
composer require james2001/odtbundle
With Symfony Flex the bundle is enabled automatically. Otherwise add it to
config/bundles.php:
return [ // ... James2001\Bundle\OdtBundle\James2001OdtBundle::class => ['all' => true], ];
The PHP zip extension is used by default to read and write ODT files.
Usage
Write a template in LibreOffice with {tags}, and [!-- BEGIN name --] /
[!-- END name --] around the part to repeat, then inject the factory:
use James2001\Bundle\OdtBundle\Factory; class InvoiceController extends AbstractController { public function download(Factory $odtFactory): Response { $odf = $odtFactory->createOdtObject($this->getParameter('kernel.project_dir').'/templates/invoice.odt'); $odf->setVars('title', 'My document'); $articles = $odf->setSegment('articles'); foreach ($items as $item) { $articles->setVars('articleTitle', $item->getTitle()); $articles->merge(); } $odf->mergeSegment($articles); $file = tempnam(sys_get_temp_dir(), 'odt'); $odf->saveToDisk($file); return $this->file($file, 'invoice.odt')->deleteFileAfterSend(); } }
odtphp 2.x expects UTF-8 strings in setVars(). The factory is also available as
the public james2001_odt.factory service. See the
odtphp README for every available method.
Upgrading from 2.x
- PHP 8.1 and Symfony 6.4 are the new minimums, and
james2001/odtphp2.x is required. If your application ran on PHP 8.0 or older, it used odtphp 1.x:setVars()now expects UTF-8 by default instead of ISO-8859-1. - The bundle class is now an
AbstractBundleand isfinal: theDependencyInjection\James2001OdtExtensionandDependencyInjection\Configurationclasses are gone. Thejames2001_odt.factoryservice and theFactoryautowiring alias don't change.
Upgrading from 1.x
Apply these changes, then the ones of "Upgrading from 2.x" above:
- The ODT classes now come from
james2001/odtphp:\OdfbecomesOdtphp\Odfand\SegmentbecomesOdtphp\Segment. The methods are the same. setVars()expects UTF-8 by default (cybermonde/odtphp expected ISO-8859-1).Factory::createOdtObject()andFactory::createSegment()have typed arguments and return types.- The sources moved to
src/, andResources/config/services.xmlis gone: the service is declared by the extension. The service idjames2001_odt.factorydoesn't change.
Tests
composer install vendor/bin/phpunit
License
This bundle is released under the MIT license (see LICENSE). The odtphp library it relies on is released under the GPL-3.0.