Zum Inhalt springen
Menü
Sie müssen registriert sein, um mit der Community zu interagieren.
Diese Frage wurde gekennzeichnet
11 Antworten
28344 Ansichten

I have a custom reports, which needs to be added as an attachment, no matter if Save as Attachment is set or not.
Here is code, I generate PDF with:

pdf = self.env['report'].sudo().get_pdf([invoice.id], 'account.report_invoice')

Here is how I try to create an attachment:.

self.env['ir.attachment'].create({
                        'name': 'Cats',
                        'type': 'binary',
                        'datas': pdf,
                        'mimetype': 'application/x-pdf'
                        })

It creates new attachment, but instead of expected PDF, there is just some binary file which could not be recognized into actual PDF. So is there a way to generate real PDF as attachment?

Avatar
Verwerfen
Beste Antwort

Here is how:

import base64
pdf = self.env['report'].sudo().get_pdf([invoice.id], 'account.report_invoice')
self.env['ir.attachment'].create({
    'name': 'Cats',
    'type': 'binary',
    'datas': base64.encodestring(pdf),
'res_model': 'account.invoice',
'res_id': invoice.id,

    'mimetype': 'application/x-pdf'
})

The res_model and res_id in the create vals dict are needed to associate the attachment to the record


Avatar
Verwerfen

could you please also upvote it the answer?

Hi Axel Mendoza,

is it doing using override the action_invoice_send function ??

No, that code is intended to be used anywhere you need to do that, it's not something that need to be used inside an override.

Hello Axel, can I use this .get_pdf to print the report? If yes,how can I do that?

Indeed... the get_pdf method of the report model will returns the pdf printed report file, that code just show how to use it by code, if you like to get the pdf report printed to be downloaded it's better to return the report action dict in order to get the odoo report download flow used. Always depending on where you put the code, normally in a button

hi Axel..how to implement your solution in odoo v11?

The same code will work

Sure, Glad to read that

Just a heads-up that in Python 3 you should now use base64.encodebytes instead of base64.encodestring(pdf)

Beste Antwort

Thank you for great answer!

Avatar
Verwerfen
Verknüpfte Beiträge Antworten Ansichten Aktivität
1
Aug. 20
8623
1
Apr. 20
6763
0
Juni 25
275
1
Juni 25
639
2
Mai 25
1208