odoo13 报价单 PDF 中添加图片添加图片

odoo中数据的存储,XML 都是在 postgresql数据库中。

--dev xml -c odoo.conf

添加这行代码,可以在更改了odoo中XML文件后不必每次升级模块。

原图:

 效果图:

报价单,属于销售模块的控件。需要找到销售模块的XML文件。

1: sale 销售模块文件,sale_views.xml 中找到该控件位置添加字段,将会在前端显示。

2:在销售模块执行文件中添加字段,和字段更该时需要调用的函数

 3:当你选择产品时,会调用产品数据库,定义一个函数,返回当前产品的图片。

至此:重新运行程序,报价单,添加图片成功:

如何在PDF中添加图片呢?我用的是比较简单暴力的方法

在odoo 生产PDF之前拦截odoo生成的HTML修改其中参数,给到PDF 

效果图

 

此处就是odoo生成好的HTNL即将传给PDF ,直接上代码。

        html = self.with_context(context).render_qweb_html(res_ids, data=data)[0]

        # Ensure the current document is utf-8 encoded.
        html = html.decode('utf-8')
        navigation_bar_list = ['<th name="th_description" class="text-left">Description</th>',
                               '\n\t\t\t\t\t\t<th name="th_quantity" class="text-right">产品图片</th>\n ']
        html = re.sub(navigation_bar_list[0], '{}'.format(''.join(navigation_bar_list)), html)
        soup = BeautifulSoup(html, 'html.parser')
        sale_tbody = soup.find(attrs={'class': 'sale_tbody'})
        data = self._get_rendering_context(res_ids, data)  # 获取订单ID
        order = self.env['sale.order.line']  # 获取订单产品
        order_product = order.search([('order_id', '=', data['doc_ids'][0])]) # 查询当前每个产品
        order_product_id = re.findall('\d+', str(order_product))
        for index, st in enumerate(sale_tbody.find_all(name='tr')):
            td_name = str(st.find_all('td', limit=1)[0])
            invalid_symbol = re.sub('\[', '\[', td_name)
            invalid_symbol = re.sub('\(', '\(', invalid_symbol)
            invalid_symbol = re.sub('\)', '\)', invalid_symbol)
            product_id = order.search([('id', '=', order_product_id[index])])
            product = product_id.with_context()
            product_imag_128 = product.get_product_id()
            product_img = f''' <td name="td_quantity" class="text-right"> <img style="max-height: 80px;" alt="None" 
            src="data:image/jpg;base64,{product_imag_128.decode(encoding='utf-8')}"/></td> '''
            invalid_symbol_list = [td_name, product_img]
            html = re.sub(invalid_symbol, '{}'.format(''.join(invalid_symbol_list)), html)
        bodies, html_ids, header, footer, specific_paperformat_args = self.with_context(context)._prepare_html(html)

思路分析;

        此时可以通过爬虫取出odoo生成的数据。

        定义一段HTML 添加 产品图片字段:

    navigation_bar_list = ['<th name="th_description" class="text-left">Description</th>',
                       '\n\t\t\t\t\t\t<th name="th_quantity" class="text-right">产品图片</th>\n ']

        正则替换原生HTML 并添加字段

    html = re.sub(navigation_bar_list[0], '{}'.format(''.join(navigation_bar_list)), html)
    soup = BeautifulSoup(html, 'html.parser')
    sale_tbody = soup.find(attrs={'class': 'sale_tbody'})  在PDF表中通过class获取字段
通过本类方法获取当前将被打印的订单号data = self._get_rendering_context(res_ids, data)获取订单ID
获取到订单后,连接销售行数据库order = self.env['sale.order.line']连接销售数据库
获取订单中的产品IDorder_product = order.search([('order_id', '=', data['doc_ids'][0])])获取订单产品id
正则提取ID号
order_product_id=re.findall('\d+',str(order_product))
正则提取ID号
获取订单中所有的产品遍历
sale_tbody.find_all(name='tr')
取每个产品明
td_name = str(st.find_all('td', limit=1)[0])

        同上新增产品图片字段一样,替换每个产品子字段,添加 图片。

        通过销售模块执行文件访问产品数据库,获取相对应的图片返回。到HTML

        需要在销售文件中添加一个查询函数:

        

拓展:

odoo 中 查询数据库:

sql = "select * from 表名"
self.env.cr.execute(sql)  #执行SQL语句
dicts = self.env.cr.dictfetchall()  #获取SQL的查询结果

常用表名:

#  sale_order 取报告单号, 总价格 未税金额  税金设置
#   res_company   取报告公司信息中 公司邮箱电话
#  res_partner  获取客户信息
# ir_attachment 图片仓库
# product_template 产品数据库
# sale_order 订单数据库
# sale_order_line 订单产品数据库

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值