Odoo Help

Welcome!

This community 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.

4

how to print all attachments to an object at once?

By
Andreas Maertens
on 5/3/13, 9:23 AM 3,680 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.

4

Andreas Maertens

--Andreas Maertens--
912
| 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'))
        else:
            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()
            try:
                os.write(fd, base64.decodestring(attachment.datas))
            finally:
                os.close(fd)
            fileObject = file(file_name, 'r')
            try:
                reader = PdfFileReader(fileObject)
            except PdfReadError as e:
                _err_logger.error(_('Expected Error occured: %s' % e))
                _err_logger.error(__name__)
                _err_logger.error(_('This happens, when file to print is not a Pdf-File'))
                break

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

report_attachment_merge('report.attachment_merge')

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= {}

print_product_attachments()

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.

2

Francesco OpenCode

--Francesco OpenCode--
3608
| 5 7 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. :)

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

Hi , is there a final solution for this ?

0
Laurens
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?

Bista Solutions US, 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

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

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