Skip to main content

A Fusion server that will transform your py3o.template into final LibreOffice documents

Project description

Introduction

py3o.fusion is a web server that provides simple but important services:

  • transform your py3o.template LibreOffice templates into final LibreOffice documents.

  • transform OpenOffice / LibreOffice documents to any supported format

Basically you can fusion a templated OpenOffice / LibreOffice document into any supported format (ODT, DOC, DOCX, PDF)

This is intended to avoid direct dependencies in your own applications. This also opens up the py3o ecosystem to other programming languages than Python.

Deployment

We recommend using the docker images we created. This is by far the quickest to get a full conversion service up and running without hassle.

Just follow the instructions from our page on the docker hub

Using it

You can use any language.

Here is the simplest example possible:

# import the wonderful requests lib
# if you need to intall it just try
# pip install --upgrade requests
import requests

# define where is your py3o.fusion endpoint
url = 'http://localhost:8765/form'

# open up the file and stuff it into a dictionary
# tmpl_file is a required field on the form. If you don't give
# it to the endpoint you'll receive an error back from it.
files = {
    'tmpl_file': open('templates/simple.odt', 'rb')
}

# then prepare the other fields of the form
# those 3 fields are also mandatory and failing to POST
# one of them will get you an error back from the server
#
# In this example you can see we leave the datadictionary
# and the image_mapping empty... This is because we won't
# send a template to the server but a simple plain
# old ODT file
fields = {
    "targetformat": 'PDF',
    "datadict": "{}",
    "image_mapping": "{}",
}

# finally POST our request on the endpoint
r = requests.post(url, data=fields, files=files)

# don't forget to close our orginal odt file
files['tmpl_file'].close()

# see if it is a success or a failure
# ATM the server only returns 400 errors... this may change
if r.status_code == 400:
    # server says we have an error...
    # this means it properly catched the error and nicely
    # gave us some info in a JSON answer...
    # let's use this fact
    print r.json()

else:
    # if we're here it is because we should receive a new
    # file back

    # let's stream the file back here...
    chunk_size = 1024

    # fusion server will stream an ODT file content back
    outname = 'request_out.%s' % 'pdf'
    with open(outname, 'wb') as fd:
        for chunk in r.iter_content(chunk_size):
            fd.write(chunk)

    # warn our dear user his file is ready
    print "Your file: %s is ready" % outname

grab the full odt2pdf.py source from here and the example ODT from here here is a way to do this in one step:

$ mkdir -p templates && wget https://bitbucket.org/faide/py3o.fusion/raw/055770694c0c4c1593aed156149d2d43a6042913/py3o/fusion/static/examples/odt2pdf.py && wget https://bitbucket.org/faide/py3o.fusion/src/055770694c0c4c1593aed156149d2d43a6042913/py3o/fusion/static/examples/templates/simple.odt?at=default && mv simple.odt templates/

Here is a more complicated example that fusions a datadictionary into a templated ODT using py3o.template and gives you back the resulting PDF. You’ll note you can also override an image inside the template:

# you'll need to install requests to make this example work
# pip install --upgrade requests
# should do the trick
import requests
import json

# point the client to your own py3o.fusion server
url = 'http://localhost:8765/form'

# target formats you want... can be ODT, PDF, DOC, DOCX
targetformats = ["ODT", "PDF", "DOC", "DOCX"]


class MyEncoder1(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, Item):
            obj = obj._asdict()
        else:
            obj = super(MyEncoder1, self).default(obj)

        return obj


class Item(object):
    def _asdict(self):
        return self.__dict__


items = list()

item1 = Item()
item1.val1 = 'Item1 Value1'
item1.val2 = 'Item1 Value2'
item1.val3 = 'Item1 Value3'
item1.Currency = 'EUR'
item1.Amount = '12345.35'
item1.InvoiceRef = '#1234'
items.append(item1)

for i in xrange(1000):
    item = Item()
    item.val1 = 'Item%s Value1' % i
    item.val2 = 'Item%s Value2' % i
    item.val3 = 'Item%s Value3' % i
    item.Currency = 'EUR'
    item.Amount = '6666.77'
    item.InvoiceRef = 'Reference #%04d' % i
    items.append(item)

document = Item()
document.total = '9999999999999.999'

data = dict(items=items, document=document)

data_s = json.dumps(data, cls=MyEncoder1)

for targetformat in targetformats:
    # open the files you need
    files = {
        'tmpl_file': open('templates/py3o_example_template.odt', 'rb'),
        'img_logo': open('images/new_logo.png', 'rb'),
    }

    # fusion API needs those 3 keys
    fields = {
        "targetformat": targetformat,
        "datadict": data_s,
        "image_mapping": json.dumps({"img_logo": "logo"}),
    }

    # and it needs to receive a POST with fields and files
    r = requests.post(url, data=fields, files=files)

    # TODO: handle error codes
    if r.status_code == 400:
        # server says we have a problem...
        # let's give the info back to our human friend
        print r.json()

    else:
        chunk_size = 1024
        # fusion server will stream an ODT file content back
        ext = targetformat.lower()
        with open('request_out.%s' % ext, 'wb') as fd:
            for chunk in r.iter_content(chunk_size):
                fd.write(chunk)

    files['tmpl_file'].close()
    files['img_logo'].close()

And voila. You have a file called out.odt that contains the final odt fusionned with your data dictionary.

For the full source code + template file and images just download them from our repo

If you just want to test it rapidly you can also point your browser to the server http://localhost:8765/form and fill the form manually.

Changelog

0.3 sep. 12 2014

  • Added examples that can be downloaded from the feature page of the server itself.

0.2 sep. 11 2014

  • Fixed an error case when the caller specified an invalid image mapping. The error was catched on the server but not sent back the the client.

0.1 sep. 11 2014

  • Initial release

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

py3o.fusion-0.3.zip (699.3 kB view details)

Uploaded Source

py3o.fusion-0.3.tar.gz (687.2 kB view details)

Uploaded Source

Built Distributions

py3o.fusion-0.3-py2.7.egg (702.5 kB view details)

Uploaded Source

py3o.fusion-0.3-py2-none-any.whl (694.8 kB view details)

Uploaded Python 2

File details

Details for the file py3o.fusion-0.3.zip.

File metadata

  • Download URL: py3o.fusion-0.3.zip
  • Upload date:
  • Size: 699.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for py3o.fusion-0.3.zip
Algorithm Hash digest
SHA256 9444b71a15ba521152ac95b81d5b236e0ad693b7f2738f5ecf1f1ff61c9eed0c
MD5 9c0fb6eb3361823f434a28370d6fad2f
BLAKE2b-256 617294fcfb51cf21709c42188aad04471f51862e8181eba65913d3f24b3ee20f

See more details on using hashes here.

Provenance

File details

Details for the file py3o.fusion-0.3.tar.gz.

File metadata

  • Download URL: py3o.fusion-0.3.tar.gz
  • Upload date:
  • Size: 687.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for py3o.fusion-0.3.tar.gz
Algorithm Hash digest
SHA256 04dc07cb95b3e80d1d45f35faa3bbbe3bddc88cabfcc63587f133d961d84dff0
MD5 ea04b39a8bf6c7a64e7fcd76853845cc
BLAKE2b-256 5281ecaedea7015ef12ce22341394e0c90a5f3fd4bbc2c41304a6d701c495bd8

See more details on using hashes here.

Provenance

File details

Details for the file py3o.fusion-0.3-py2.7.egg.

File metadata

File hashes

Hashes for py3o.fusion-0.3-py2.7.egg
Algorithm Hash digest
SHA256 b7ad41f9cc8d25d0994354db0cad2482ca9bf08b286d288af4e6aca7aa127da9
MD5 1a7d34bdf107ecb4c98d2ec12b65640f
BLAKE2b-256 de0ebf2530582891952103c3e3f8b98ca8c1cc135c56eda46021348cb6863670

See more details on using hashes here.

Provenance

File details

Details for the file py3o.fusion-0.3-py2-none-any.whl.

File metadata

File hashes

Hashes for py3o.fusion-0.3-py2-none-any.whl
Algorithm Hash digest
SHA256 2be03010733ea16defdb8e2d2290d37a0fe919154596c77729273611a6848932
MD5 c96938fde0f34acde39793974b7c2b27
BLAKE2b-256 e89aee4497b59f2e1c11f3569f563b6da469b37628b442dd42a05e23b852aa1e

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page