Skip to Content
Menu
This question has been flagged
1 Reply
2644 Views

Hello,

I've a binary field in my model. I've attached to this field a pdf by upload. If I click on the link, I can download the pdf and read it in a pdf viewer. If I change my field to the widget pdf_viewer, I can see the pdf directly in my view.

But when I want to attach this pdf to a mail template, the PDF can't be read after receiving the email.

This is my code :

registreds = self.env['x_training_registred'].search([('x_registred_ids', '=', record.x_name)])
for registred in registreds:
  template_obj = self.env['mail.template'].browse(77)
  attach_obj = self.env['ir.attachment']
  attachment_ids = []
  #result_rfq_quote = base64.b64decode(record.x_studio_training_plan)
  if record.x_studio_training_plan:
    attach_data = {
        'name': record.x_studio_training_plan_filename,
        'datas': record.x_studio_training_plan,
        'datas_fname': record.x_studio_training_plan_filename,
        'type': 'binary',
        'mimetype': 'application/x-pdf'
        }
    attach_id = attach_obj.create(attach_data)
    attachment_ids.append(attach_id.id)
  if attachment_ids:
    template_obj.write({'attachment_ids': [(6, 0, attachment_ids)]})

If I use b64decode, my pdf size is 0 ko. If I insert the field without decoding the file size is 3ko. The real size is 34ko.

What's wrong with my code ?

Thanks for your help.
Avatar
Discard
Author Best Answer

Solve by changing of point of view.

I now use Document module and upload my pdf in this module.

I add a field many2many_tags on my own module to associate document with it.

I call this field when I want to send my mail like this.

template_obj = env['mail.template'].browse(77)
  attachment_ids = []
  attachment_ids.extend(record.x_studio_attachment_ids.ids)
  template_obj.write({'attachment_ids': [(6, 0, attachment_ids)]})
Avatar
Discard