odoo qweb 打印pdf有三种方式:
1.常用的纯xml文件,如下(忽略menu)
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<template id="report_sale_order_inprice_detail">
<t t-call="dx_base_report.external_layout">
<div t-if="docs.state=='sale' or docs.state == 'done'" class="page">
<table class="table table-bordered">
<tr>
<td align="center" colspan="3">
<strong><h4>销售订单</h4></strong>
</td>
</tr>
<tr>
<td width="33%"><strong>订 单 号:</strong><span t-field="docs.name"/></td>
<td width="33%"><strong>下单客户:</strong><span t-field="docs.partner_id.name"/></td>
<td width="33%"><strong>销售订单:</strong><span t-field="docs.sale_approve_id"/></td>
</tr>
</table>
</div>
<div t-if="docs.state != 'sale' and docs.state != 'done'" class="page">
<h3 class="text-center">请打印销售单单据</h3>
</div>
</t>
</template>
<template id="report_sale_order_inprice">
<t t-call="report.html_container">
<t t-foreach="docs" t-as="o">
<t t-set="o" t-value="o.with_context({'lang':'zh_CN'})"/>
<t t-set="cn" t-value="True"/>
<t t-call="dx_sale_order_report.report_sale_order_inprice_detail" t-lang="'zh_CN'"/>
</t>
</t>
</template>
</odoo>
2. 打印的部分内容需要自定义,但是希望使用原qweb中的docs,这时我们需要调用report_sxw的__init__方法来添加新属性,代码如下(忽略menu):
# -*- coding: utf-8 -*-
##############################################################################
#
#
##############################################################################
from odoo.report import report_sxw
from odoo import models,api
class sale_delivery_py(report_sxw.rml_parse):
def __init__(self, cr, uid, name, context):
super(sale_delivery_py, self).__init__(cr, uid, name, context=context)
self.localcontext.update({
'get_product_name':self.get_product_name,
})
self.context = context
#获取产品的属性值
@api.multi
def get_product_name(self, para):
prodt=self.objects.env['product.product'].search([('id','=',para)])
name = prodt.name+'('
for index,tt in enumerate(prodt.attribute_value_ids):
if index<len(prodt.attribute_value_ids)-1:
name=name+tt.name+','
else:
name=name+tt.name+')'
return name
class report_sale_delivery(models.AbstractModel):
_name = 'report.sale_delivery_print.report_sale_delivery'
_inherit = 'report.abstract_report'
_template = 'sale_delivery_print.report_sale_delivery'
_wrapped_report_class = sale_delivery_py
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
调用的xml文件如下:
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<template id="report_sale_delivery">
<t t-call="report.html_container">
<div style="font-size:9px;" t-if="docs.state in ('done','assigned','partially_available')" class="page">
<div class="row">
<div class="col-xs-12 text-center">
<strong>
<h4 t-esc="docs.picking_type_id.name">单</h4>
</strong>
</div>
</div>
<div class="row">
<div class="col-xs-4">
<strong>出货单据号:</strong>
<p t-esc="docs.origin"/>
</div>
</div>
<table class="table table-bordered">
<thead>
<tr>
<th>行号</th>
<th>销售订单号</th>
<th>客户订单号</th>
<th>产品名称</th>
<th>客户产品编号</th>
</tr>
</thead>
<t t-set="i" t-value="1"/>
<t t-set="m" t-value="1"/>
<t t-set="fzz" t-value="0"/>
<tbody>
<tr style="page-break-inside : avoid" t-foreach="docs.pack_operation_product_ids"
t-as="product">
<t t-set="fzx" t-value="0"/>
<tr style="page-break-inside : avoid" t-foreach="product.pack_lot_ids" t-as="lot">
<td>
<span t-esc="i"/>
<t t-set="i" t-value="i+1"/>
</td>
<t t-set="m" t-value="lot_size"/>
<td>
<span t-esc="product.linked_move_operation_ids[0].move_id.procurement_id.sale_line_id.order_id.name"/>
</td>
<td>
<span t-esc="docs.client_order_ref"/>
</td>
<td>
<span t-esc="get_product_name(product.product_id.id)"/><!--自定义属性的调用-->
</td>
<td>
</tbody>
</table>
</div>
<div class="footer">
<div class="text-right">
<small>
<span>第</span>
<span class="page"/>
<span>页 共</span>
<span class="topage"/>
<span>页</span>
</small>
</div>
</div>
<div t-if="docs.state not in ('done','assigned','partially_available')" class="page">
<h3 class="text-center">请打印可用、部分可用、完成单据</h3>
</div>
</t>
</template>
</odoo>
xml调用:
3.有些场景需要数据重构,详情请见文章:odoo自定义报表
https://blog.csdn.net/chasenksky/article/details/79375612