Skip to content

Library for generating documents from templates

Notifications You must be signed in to change notification settings

bezirovoviku/temgen

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Template generator

This is simple template system for document files. It uses text replacement, allows cyclical replacements and basic filters.

Installation

You can install this library using composer

composer require bezirovoviku/temgen

Requirements

LibreOffice (2+) - only for OPDF converter

PhantomJS - only for PPDF converter

Usage

First you will need to include the generator class

use Temgen\Generator;

then use generator class as follows

//Creates generator
$generator = new Generator();
//Adds basic filters
$generator->addFilters();
//Sets temporary folder used to store documents
$generator->setTmp('/tmp');
//Sets path to template file
$generator->setTemplate(new Document('template.html'));
//Generates zip archive from specified data
$generator->generateArchive(json_decode($this->data, true), 'archive.zip');

Creating custom filters

To create custom filter, you will need to implement \Temgen\Generator\Filter interace

class MyFilter implements \Temgen\Generator\Filter
{
	/**
	 * Returns tag used to identify filter
	 *
	 * @return string tag identifing filter
	 */
	public function getTag() {
	  return 'myfilter';
	}
	
	/**
	 * Filter given arguments and return result
	 *
	 * @param \Temgen\Generator $generator generator calling this filter
	 * @param array           $context   replacing context
	 * @param array           $arguments arguments passed to filter
	 * @param string|null     $input     pipe input (if present)
	 * @return string result
	 */
	public function filter($generator, $context, $arguments, $input) {
	  return strtoupper($input);
	}
}

then you will need to add instance of this filter to generator instance, like so:

$generator = new Generator();
$generator->addFilter(new MyFilter());

now, in this generator instance, filter MyFilter will be accessible by {myfilter} syntax

Exporting pdf

This requires libreoffice installed

//Creates docx generator
$generator = new Generator\Docx();
//PDF converter
$converter = new Converter\OPDF();
//Adds basic filters
$generator->addFilters();
//Sets temporary folder used to store documents
$generator->setTmp('/tmp');
//Sets path to template file
$generator->setTemplate(new Document\Docx('template.docx'));
//Generates zip archive from specified data, converts using converter
$generator->generateArchive(json_decode($this->data, true), 'archive.zip', $converter);

About

Library for generating documents from templates

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published