Odoo Help


how to print all attachments to an object at once?

Andreas Maertens
on 5/3/13, 9:23 AM 5,186 views

Is there a way to print all attachments stored for a object at once? In my case I want to print all attached files (not only .pdf) of a product on printing delivery orders. I wonder if there already is a way or I need to create a new addon for.


Andreas Maertens

--Andreas Maertens--
| 5 5 7
Finsterwalde, Germany
--Andreas Maertens--

I develop odoo8.0 modules

Andreas Maertens
On 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

--Francesco OpenCode--

| 6 8 9
Grottaglie, Italy
--Francesco OpenCode--

Italian Odoo (OpenERP) Modules Developer LINKEDIN: http://www.linkedin.com/in/francescoapruzzese

Francesco OpenCode
On 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.

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

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

We found our hero,yet. :D

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

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

on 5/14/13, 7:53 PM
Wilbert Tan
On 3/8/16, 12:02 AM

Hi , is there a final solution for this ?

On 5/14/13, 2:38 PM

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

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

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

Hi Andreas, did you post this already somewhere else?

Ray Carnes
on 7/31/13, 4:09 PM

no, there's only that scource snippets.

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

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

Question tools

1 follower(s)


Asked: 5/3/13, 9:23 AM
Seen: 5186 times
Last updated: 4/20/16, 11:55 AM