AP² logs

  • development

Use a custom PDF as an OpenERP report

Dom 20 Janeiro 2013
By ap²

In development.

tags: pythonopenerp

(This post applies to versions 6.1 and 7.0 of OpenERP, though it may work with other versions)

Although RML is certainly useful to generate PDF reports, sometimes you need to return some pre-generated PDF instead. Whether it comes from an external service or just from a non-Python tool, it's easy to send that to the user transparently, like it was just any other report.

The way to do this is to create a custom report class that registers with OpenERP to serve a specific report. This is a simple example:

from osv import osv
import report

class my_custom_report(report.interface.report_int):
    def create(self, cr, uid, ids, data, context=None):
        #here you can generate or fetch your PDF
        #As an example, we'll just download one from the web
        url = 'http://mike.pirnat.com/static/zen-of-python.pdf'
        mypdf = urllib.urlopen(url).read() #exercise: add error handling!
        return (mypdf, 'pdf')

class registrar(osv.osv):
    _inherit = 'ir.actions.report.xml'

    def register_all(self, cr):
        my_custom_report('report.zen_of_python')
        super(registrar, self).register_all(cr)

As you can see, it's easy to create this custom report. The important part is that the create() method of your custom report class return a tuple that contains the PDF content in the first position, and an indication of the filetype to OpenERP in the second.

Then, you just need to register your class with the OpenERP reporting subsystem, which will take care of calling with when the user clicks on the appropriate action, like a sidebar menu item.

In this example, our class with provide the report called "report.zen_of_python", but this can be anything, including overriding existing reports such as the Invoices.

Page 1 / 1

Proudly powered by Pelican, which takes great advantage of Python.

The theme is by Smashing Magazine, thanks!