【问题标题】:Saving variable get_pdf() into fields.binary将变量 get_pdf() 保存到 fields.binary
【发布时间】:2017-12-27 07:14:24
【问题描述】:

我想通过 get_pdf 函数将变量自动保存到 fields.binary (email_attachment_file) 中。

我的代码如下:

 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

而且,modelReport.get_pdf 函数有什么问题吗?

【问题讨论】:

    标签: report odoo odoo-8 qweb


    【解决方案1】:

    使用 encodestring() 代替 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)
    

    【讨论】:

    • 从数据返回的是 %PDF-1.3 1 0 obj > endobj 2 0 obj
    • get_pdf 以字符串格式返回报告,因此您需要使用 base64.encodestring(data) 将其转换为二进制。
    猜你喜欢
    • 1970-01-01
    • 2022-06-15
    • 2020-05-03
    • 2021-11-12
    • 2011-08-14
    • 2018-11-23
    • 2015-06-18
    • 2011-11-10
    • 1970-01-01
    相关资源
    最近更新 更多