3

I want to save a variable automatically into a fields.binary (email_attachment_file) from get_pdf function.

My codes below here:

 class example_example(models.Model):
    email_attachment_file   =   fields.Binary('Data (.txt,.pdf)')
    email_filename          =   fields.Char('Filename')

    def generate(self,etc..):
      report_name = "report_name_template"

      datas = {
            'ids':[],
            'model' : etc,
            'form'  : etc
            'context': context
            }

      moddelReport = self.pool.get('report')
      alpha =  modelReport.get_pdf(cr, uid,[],report_name,None,datas,context=context)  

      #alpha = base64.decodestring(alpha)
      #alpha = alpha.decode('unicode_escape').encode('utf-8')

      # --------- how to save alpha variable into fields.binary

And, is there something wrong modelReport.get_pdf function ?

ChesuCR
  • 9,352
  • 5
  • 51
  • 114

1 Answers1

2

Use encodestring() instead of decodestring().

report_obj = self.pool.get('report')
data =  modelReport.get_pdf(cr, uid,[],report_name,None,datas,context=context)
self.email_attachment_file  = base64.encodestring(data)
  • the return from data is %PDF-1.3 1 0 obj << /Kids [ ] /Type /Pages /Count 0 >> endobj 2 0 obj << /Producer (Python PDF Library \055 http\072\057\057pybrary\056net\057pyPdf\057).. etc..., i can't figure it out to find the solution – Yuniar Kurniawan S Jul 24 '17 at 04:11
  • get_pdf return the report as a string format so, you need to convert it in the binary by using base64.encodestring(data). – Emipro Technologies Pvt. Ltd. Jul 24 '17 at 06:12