18. PDF 报表
适用版本
Odoo 20 社区版 · 对应官方 How-to:Build PDF Reports
第 18 课:PDF 报表
18.1 目标
看板解决了"看",报表解决"带走"。成交时打印一张房源信息卡,字段来自数据库,排版由模板决定,不需要人工抄写。
这一章要做完四件事,并回答三个问题:
- 一份报表由哪几块组成,分别放在哪些文件里?
report_name到底该写什么,为什么它和打印失败最相关?- 报表能不能先在浏览器里看 HTML,再决定打印?
18.2 Odoo 的做法
18.2.1 四件套
| 组成 | 存放位置 | 作用 |
|---|---|---|
| QWeb 模板 | report/*.xml,<template id="..."> | 决定版式和显示哪些字段 |
| 报表动作 | ir.actions.report 记录 | 把模板、模型、输出格式绑在一起 |
| 入口 | 表单按钮或侧边栏"打印"菜单 | 用户点它触发报表 |
| manifest | data 列表 | 保证模板与动作被加载 |
<template> 在加载时会被转换成一条 ir.ui.view,type 是 qweb,key 是「模块名.xmlid」。这个 key 就是报表动作里 report_name 要填的值。
18.2.2 最小模板
<template id="report_estate_property">
<t t-call="web.html_container">
<t t-foreach="docs" t-as="o">
<t t-call="web.external_layout">
<div class="page">
<h2><span t-field="o.name"/></h2>
<span t-field="o.expected_price"/>
</div>
</t>
</t>
</t>
</template>docs 是框架传进来的记录集,批量打印时逐条循环。web.html_container 提供 HTML 外壳,web.external_layout 带上公司页眉页脚与版式,两者在 Odoo 20 里都还在。t-field 会按字段类型格式化(金额带币种、日期本地化),纯表达式用 t-out。
版本变化(Odoo 20)
输出指令用 t-out,老模板里的 t-esc / t-raw 会被升级脚本替换并可能留下"未知指令"告警。QWeb 报表仍支持 web.html_container,不要因为看到旧教程被删就照着改。
18.2.3 报表动作的关键字段
字段定义在 odoo/addons/base/models/ir_actions_report.py:
| 字段 | 说明 |
|---|---|
model | 报表针对的模型,这里是 estate.property |
report_type | qweb-pdf(默认)/ qweb-html / qweb-text |
report_name | 必须等于模板的 key,例如 estate20.report_estate_property |
binding_model_id | 绑定模型后,侧边栏才出现"打印"菜单 |
binding_type | 设为 report 才会进打印菜单 |
print_report_name | 默认文件名表达式,只能用 object 和 time 两个变量 |
attachment_use | 是否复用上次生成的附件 |
本机源码里没有 qweb-xlsx 这个取值;写它会在字段校验阶段失败。
18.2.4 打印入口
视图按钮用 type="action" 指向报表动作:
<button name="%(estate20.action_report_estate_property)d"
type="action" string="打印房源卡" icon="fa-print"/>Python 里则用 report_action:
return self.env.ref('estate20.action_report_estate_property').report_action(self)它返回的动作字典 type 是 ir.actions.report,前端据此走报表路由。%(xmlid)d 是 XML 数据里的写法,加载时会被替换成数据库 id,不要在按钮上直接写 xmlid 字符串。由于替换发生在加载时,manifest 的 data 里必须把报表文件排在引用它的视图文件之前。
18.2.5 报表即网页
同一份模板既能出 PDF 也能出 HTML。登录后访问:
/report/html/estate20.report_estate_property/<记录id>
/report/pdf/estate20.report_estate_property/<记录id>先把 report_type 当 qweb-html 看排版,调好之后再切 qweb-pdf,比反复下载 PDF 快得多。Odoo 20 的 PDF 引擎是可插拔的:未安装对应引擎模块却强制指定 qweb-pdf-xxx 时,会抛 Unknown PDF engine。
18.3 开发实践
18.3.1 写模板与动作
report/estate_property_report.xml:
<odoo>
<template id="report_estate_property">
<t t-call="web.html_container">
<t t-foreach="docs" t-as="o">
<t t-call="web.external_layout">
<div class="page">
<h2><span t-field="o.name"/></h2>
<p>状态:<span t-field="o.state"/></p>
<table class="table table-sm">
<tr>
<td>期望售价</td>
<td class="text-end"><span t-field="o.expected_price"/></td>
</tr>
<tr>
<td>成交价</td>
<td class="text-end"><span t-field="o.selling_price"/></td>
</tr>
</table>
<p t-if="o.best_price">最高出价:<span t-field="o.best_price"/></p>
</div>
</t>
</t>
</t>
</template>
<record id="action_report_estate_property" model="ir.actions.report">
<field name="name">房源信息卡</field>
<field name="model">estate.property</field>
<field name="report_type">qweb-pdf</field>
<field name="report_name">estate20.report_estate_property</field>
<field name="binding_model_id" ref="model_estate_property"/>
<field name="binding_type">report</field>
<field name="print_report_name">'房源卡-' + object.name</field>
</record>
</odoo>model_estate_property 是 Odoo 为模型自动生成的 XML ID,不需要自己建。
18.3.2 挂入口并升级
在房源表单的 <header> 里加按钮:
<button name="%(estate20.action_report_estate_property)d"
type="action" string="打印房源卡" icon="fa-print"/>把它加进 manifest 的 data:
'data': [
# ... 原有安全文件
'report/estate_property_report.xml', # 必须排在引用它的视图之前
# ... 原有视图文件
],配套快照在 D:/workspace/winodoo/samples/ch18;报表文件排在视图之前,直接升级即可,不必手工调整加载顺序。
cd D:\odoo20
venv\Scripts\activate
python odoo-bin -c odoo.conf -u estate2018.3.3 验证
SELECT name, report_name, report_type, binding_type
FROM ir_act_report_xml WHERE report_name LIKE 'estate20.%';
SELECT name, key, type FROM ir_ui_view
WHERE key = 'estate20.report_estate_property';第二条查得到且 type = 'qweb',模板才算真的加载了。再在 shell 里直接渲染 HTML,不必下载 PDF:
rec = env['estate.property'].search([], limit=1)
action = env.ref('estate20.action_report_estate_property')
html = action._render_qweb_html(rec.ids)[0]
print(len(html), rec.name in str(html)) # 长度 > 0,且 True
quit()交给 AI(第 18 章 · PDF 报表)
把下面这段发给任意 AI 对话(ChatGPT / Claude / Cursor 均可):
我们用的是 Odoo 20 社区版,模块 estate20,模型 estate.property 有 name / state / expected_price / selling_price / best_price。请生成一份房源信息卡报表:① QWeb 模板用 web.html_container 与 web.external_layout,遍历 docs,字段用 t-field,空值用 t-if 判断;② ir.actions.report 的 report_type=qweb-pdf、report_name=estate20.report_estate_property、绑定 estate.property 且进打印菜单;③ print_report_name 用 object.name;④ 表单按钮用 type="action" 指向该动作。要求给出 manifest 里要加的文件路径和 HTML 预览路由。
18.4 总结
验收清单
-u estate20无ParseError,ir_ui_view里模板 key 是estate20.report_estate_property- 房源表单上出现"打印房源卡"按钮,点击弹出 PDF
- 列表勾选多条后,侧边栏"打印"菜单里有"房源信息卡"
/report/html/estate20.report_estate_property/<id>能在浏览器里看到排版- 打印出的文件名包含房源名称
- shell 里
_render_qweb_html返回的 HTML 包含房源名称
18.5 常见坑与排错
| 现象 | 原因 | 处理 |
|---|---|---|
打印报 Template not found | report_name 与模板 key 不一致 | 改成 模块名.模板xmlid |
| 侧边栏没有"打印"菜单 | 没写 binding_model_id 或 binding_type | 两个字段都补上 |
| 按钮点了没反应 | name 里 xmlid 拼错或没用 %(...)d | 检查动作 XML ID |
升级报 External ID not found | 报表文件在视图之后加载,按钮引用还不存在 | 把 report/*.xml 排到 views/*.xml 之前 |
print_report_name 报变量不存在 | 表达式里用了 record 等别的变量 | 只能用 object 和 time |
残留 t-esc 导致表达式不渲染 | 旧模板写法 | 改成 t-out |
指定 qweb-xlsx 报错 | 20 的核心报表动作没有这个类型 | 用 qweb-pdf / qweb-html / qweb-text |
PDF 报 Unknown PDF engine | 指定了未安装的引擎模块 | 用 qweb-pdf 让系统选默认引擎 |
| 管理员点打印被带去布局配置 | 公司没设外部报表版式且 config=True | 先设版式,或用 config=False 绕过 |
t-field 用在 <t> 节点上报错 | t-field 必须挂在具体 HTML 标签上 | 改成 <span t-field="..."/> |
参考与致谢:本章知识结构参考 Odoo 官方文档 Build PDF Reports (Odoo S.A.,CC BY-SA 4.0)。报表动作字段对照、HTML 预览路由与排错表均为本站基于 Odoo 20 社区版独立编写并实测。 本站非 Odoo 官方站点,内容不代表 Odoo S.A. 观点。