Skip to Content
Meniu
Trebuie să fiți înregistrat pentru a interacționa cu comunitatea.
Această întrebare a fost marcată
11 Răspunsuri
15269 Vizualizări

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.

Imagine profil
Abandonează
Autor Cel mai bun răspuns

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.

Imagine profil
Abandonează
Cel mai bun răspuns

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

Imagine profil
Abandonează
Autor

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

We found our hero,yet. :D

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

Cel mai bun răspuns

Hi , is there a final solution for this ?

Imagine profil
Abandonează
Cel mai bun răspuns

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

Imagine profil
Abandonează
Autor

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

Hi Andreas, did you post this already somewhere else?

Autor

no, there's only that scource snippets.

Related Posts Răspunsuri Vizualizări Activitate
0
oct. 21
2816
2
nov. 17
7158
8
mai 25
43159
1
feb. 23
2519
2
nov. 21
7991