Help

3

How to send email with attachment of PDF which is dynamically generated through wizard's input?

Hi All,

I've developed 1 wizard which is sending an email to multiple persons at a time. for each person PDF is generated through get_action method inside the wizard method.

The problem is I am not able to make that file as an attachment to that email.

I know that how we can link the file as an attachment to email using attachments  parameter while using build_mail method. like attachments = [('file_name',pdf_binary_data)]

If i was able to get the binary data of generated PDF than i can make an attachment of generated PDF to the email. the problem is that get action method is not giving the PDF binary data.

Is it possible we can get the binary data of generated reports? report is generated through wizard's input.

If any has idea about this, appreciated.


Best Regards,

Anil Kesariya

Avatar
Discard
2 Answers
4
Avatar
Axel Mendoza
Best Answer

Hi Anil, thanks for the upvotes ;)

The report pdf action will not give you the pdf data, it's more intended for the web client to call the report render method from the web client action manager

You could directly call the pdf generation like this

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

And use it to create and attachment or use it as the attachment data for sending emails

Reference:

https://www.odoo.com/es_ES/forum/ayuda-1/question/how-to-programmatically-create-pdf-attachment-in-odoo-10-0-118073#answer-118077

1 Comment
Avatar
Discard

Hi Axel, perfect answer, yesterday i followed the same and it was works for me. and today morning i see the same in answer :). I appreciate your answer.

0
Avatar
Sopulu Chris
Best Answer

The answers provided here is for odoo 10 or below; 

Here is a sample code done to generate Pdf and save to attachment using render_qweb_pdf for sending mail: 

def generate_report_file(self, id):
        pdf = self.env.ref('mymodule.action_report_labtest').render_qweb_pdf(id)[0]
        pdf = base64.b64encode(pdf)
        return pdf
So, you can return pdf as a binary to any binary field as this;
self.binary_field = self.generate_report_file(idoftherecord.id)

You can attach it to an attachment like this;
report_binary = self.generate_report_file(LabObj.id)
attachmentObj = self.env['ir.attachment'].create({
                'name': attachment_name,
                'type': 'binary',
                'datas': report_binary,
                'datas_fname': attachment_name + '.pdf',
                'store_fname': attachment_name,
                'res_model': self._name,
                'res_id': self.id,
                'mimetype': 'application/x-pdf'
            })

@Maduka Sopulu Chris 

Avatar
Discard