Odoo Help


11 Answers

Andreas Maertens Germany

--Andreas Maertens--
| 6 6 9
Finsterwalde, Germany
--Andreas Maertens--

I develop odoo8.0 modules

Andreas Maertens Germany
5/15/13, 3:57 AM

I made a new report class.

Following imports you'll need:

from osv import osv
from openerp.report.pyPdf import PdfFileWriter
from openerp.report.pyPdf import PdfFileReader
from openerp.report.interface import report_int
from cStringIO import StringIO
from openerp import pooler
from tempfile import mkstemp
import os
import base64
from openerp.report.pyPdf.utils import PdfReadError
import netsvc

Here's an outtake of the report class:

class report_attachment_merge(report_int):

I made extra methodes for several usecases. I'll give you the printing of attachments from the product:

    def create(self, cr, uid, ids, datas, context=None):    
        method = None
        if context['active_model'] == 'product.product': method = self.create_product
        if method is None:
            raise Exception(_('Not implemented Call for your Model'))
            return method(cr, uid, ids, context)

    def create_product(self, cr, uid, ids, context=None):

You'll need instances of PdfFileWriter, PdfFileReader and StringIO, the Product-Class and the Attachments

        stream = StringIO()        
        writer = PdfFileWriter()
        product_obj = pooler.get_pool(cr.dbname).get('product.product')
        attach_obj = pooler.get_pool(cr.dbname).get('ir.attachment')

You get Product ID from context and the related attachments from their:

        product = product_obj.browse(cr, uid, context['active_id'])
        # get datas from related attachments
        attachment_ids = attach_obj.search(cr, uid, [ ('res_model','=','product.product'),
                                                      ('res_id','=', product.id ),
                                                    ] )

Now the clou, write that attachments in temporary files and read them with pdfFileReader. After that, put them together through pdfFileWriter

        for attachment in attach_obj.browse(cr, uid, attachment_ids):
            # make tempfile
            fd, file_name = mkstemp()
                os.write(fd, base64.decodestring(attachment.datas))
            fileObject = file(file_name, 'r')
                reader = PdfFileReader(fileObject)
            except PdfReadError as e:
                _err_logger.error(_('Expected Error occured: %s' % e))
                _err_logger.error(_('This happens, when file to print is not a Pdf-File'))

            # add datas to writer
            for pageNr in range(0,reader.numPages):
        # write datas to stream
        if writer.getNumPages() > 0:
            return stream.getvalue() , 'pdf'
            raise Exception(_('No Pdf-Files to print.'))


You'll get a pdf file back containing all pdfs from attachments.

You may need to call that from a wizard:

class print_product_attachments(osv.osv_memory):

    _name = "print.product.attachments.wizard"

    def print_a_pdf(self, cr, uid, ids, context=None):
        return {
            'type': 'ir.actions.report.xml',
            'report_name': 'attachment_merge',
            'context': context,

    _columns= {}


That's it.

If someone could tell me how to render pdf files from openERP already used models please don't hestitate to tell. ;) This is the weak part of that solution.


Francesco OpenCode Italy

--Francesco OpenCode--

| 7 8 11
Grottaglie, Italy
--Francesco OpenCode--

Italian Odoo Developer. Python lover. Beer addicted.

LINKEDIN: http://www.linkedin.com/in/francescoapruzzese

Francesco OpenCode Italy
5/14/13, 3:10 PM

You can't do this with OpenERP naked. You must delevoled (or search if someone did it, yet) a module that does this.

Andreas Maertens Germany
5/14/13, 3:19 PM

Right! I've done that. As I said to Laurens, you will see a solution here tomorrow. ;)

Francesco OpenCode Italy
5/14/13, 3:21 PM

We found our hero,yet. :D

Laurens Belgium
5/14/13, 7:53 PM

That's great, Andreas! I'm looking forward to it. :)

Wilbert Tan Indonesia
3/8/16, 12:02 AM

Hi , is there a final solution for this ?

Laurens Belgium
5/14/13, 2:38 PM

Andreas, did you wind up finding a solution/fix for this? I'm very interested.

Andreas Maertens Germany
5/14/13, 3:16 PM

I did! I'll give you some source tomorrow, when back @ work

Ray Carnes United States
7/31/13, 4:09 PM

Hi Andreas, did you post this already somewhere else?

Andreas Maertens Germany
8/1/13, 3:33 AM

no, there's only that scource snippets.

Ask a Question
Keep Informed
1 follower(s)
About This Community

This platform is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now