AppyPodBackend

OpenOffice Utils via Template Backend

This package uses appy.pod to generate documents using a template in OpenDocument Format .odt/ods.

Following template api, a std way to use it is as follows:

from django.template import loader
tmpl = loader.get_template('example/simple.odt')
pdf_content = tmpl.render(context={...})

Substitution in templates

Variable substituition and template logic is done as described in appy documentation. appy.pod package returns an OpenDocument document w/o type conversion (an odt template remains an odt templates). Conversion to pdf is delegated to LibreOffice.

Conversion to PDF

There are 2 main modes that we can convert to pdf using libreoffice:

  1. locally, invoking a Python interpreter that has uno package and setting a port to connect to an instance of libreoffice.

  2. connetting to an external service (eg.: a Gotenborg docker). This can be forced by setting POD_CONVERTER to the base url of a gotenberg server.

AppyPod Template

thx.appy.backend.Template accepts some more parameters than default django template, and has a .save_as method that can be very handy.

Merging files

The function func:convert_to_pdf that uses gotenberg service is able to merge files according to following rules:

  • if a list of template (eg: odt) is provided, all the files are converted in a single pdf. Pdf input files are ignored

  • if a list of pdf files are provided and merge = True all pdf files are converted. OpenDocument input files are ignored

API

class thx.appy.backend.AppyPodEngine(params)[sorgente]

Initialize the template engine.

params is a dict of configuration settings.

from_string(template_code)[sorgente]

Create and return a template for the given source code.

This method is optional.

get_template(template_name)[sorgente]

Load and return a template for the given name.

Raise TemplateDoesNotExist if no such template exists.

class thx.appy.backend.Template(template, backend)[sorgente]
render(context=None, request=None, output_format='pdf', forceOoCall=False, external=None)[sorgente]

False render function.

Parametri
  • context – context (default: self.context)

  • request – the current request (optional)

  • output_format – output file format (.odt, .ods, .pdf - default)

Ritorna

generated file stream if created, else None

save_as(output_path=None, output_format=None, context=None, forceOoCall=False, external=None)[sorgente]

Save the template into file

Parametri

output_path – the output path

Ritorna

the open handler of the generated file

from django.template import loader

templ = loader.get_template('admin/fax/cover.odt', using='appy')
templ.save_as('/tmp/fax.pdf', context={...})
thx.appy.backend.convert_to_pdf(files, output_filename=None, merge=False, timeout=10)[sorgente]

Convert input to pdf. If many files are handled one single merged file is returned

Parametri
  • file – filename or open file handler (can be a list/tuple)

  • output_filename – name of the output file (optional)

  • merge – (boolean). Suggest that we need to generate an output that is the mewrge of input files (that ned to be .pdf)

  • timeout – timeout for the connection to the server (default settings.POD_CONVERTER_TIMEOUT)