Odoo 10 QWeb报表页码不显示问题求助
解决Odoo 10 QWeb报表页码及自定义页眉页脚不显示的问题
我之前在Odoo10开发QWeb报表时也碰到过类似的问题,给你几个针对性的排查和解决方向:
一、页码不显示的修复方案
Odoo10的QWeb报表引擎会自动注入page(当前页码)和nb_pages(总页数)两个变量,但需要放在正确的模板结构里才能生效:
方案1:直接在报表模板内添加页码
确保你的报表模板嵌套在report.html_container和report.internal_layout(或外部布局)中,然后在页脚区域引用变量:
<t t-call="report.html_container"> <t t-foreach="docs" t-as="doc"> <t t-call="report.internal_layout"> <!-- 你的报表内容 --> <div class="footer"> <div class="text-center"> 第 <span t-esc="page"/> 页,共 <span t-esc="nb_pages"/> 页 </div> </div> </t-tcall> </t-tforeach> </t-tcall>
方案2:通过继承外部页脚模板添加页码
如果你想让多个报表复用页码样式,可以继承系统默认的外部页脚模板:
<template id="custom_footer_with_page" inherit_id="report.external_layout_footer"> <xpath expr="//div[@class='footer']" position="replace"> <div class="footer"> <div class="text-center"> Page <span t-esc="page"/> / <span t-esc="nb_pages"/> <!-- 可添加其他自定义页脚内容 --> </div> </div> </xpath> </template>
之后在你的报表模板里调用这个自定义页脚所在的外部布局即可。
二、自定义页眉页脚无效的解决步骤
自定义页眉页脚失效通常是布局继承或调用方式不对,试试下面的方法:
1. 创建完整的自定义布局模板
先定义包含页眉、内容占位、页脚的完整布局:
<template id="custom_full_layout"> <t t-call="report.html_container"> <t t-foreach="docs" t-as="doc"> <div class="page"> <!-- 自定义页眉 --> <div class="header"> <div class="text-left"> <span t-esc="doc.company_id.name"/> | <span t-esc="doc.company_id.phone"/> </div> </div> <!-- 报表内容占位:这里会插入你的报表具体内容 --> <t t-raw="0"/> <!-- 自定义页脚 --> <div class="footer"> <div class="text-center"> Page <span t-esc="page"/> of <span t-esc="nb_pages"/> </div> </div> </div> </t-tforeach> </t-tcall> </template>
2. 在报表模板中调用自定义布局
把你的报表内容嵌套在自定义布局的调用里:
<template id="my_custom_report"> <t t-call="your_module.custom_full_layout"> <!-- 这里是你的报表具体内容 --> <div class="report-content"> <h3 t-esc="doc.name"/> <p t-esc="doc.description"/> <!-- 其他报表元素 --> </div> </t-tcall> </template>
3. 添加PDF渲染的CSS规则
PDF渲染时需要用CSS的@page规则指定页眉页脚的位置,否则可能被隐藏:
@page { size: A4; margin: 1.5cm; @top-center { content: element(header); } @bottom-center { content: element(footer); } } .header { position: running(header); } .footer { position: running(footer); }
把这段CSS加到你的报表模板的<style>标签里,或者关联到模块的CSS文件中。
三、容易忽略的坑
- 清除缓存:修改模板后一定要刷新Odoo的QWeb缓存(路径:设置 -> 技术 -> 用户界面 -> 刷新QWeb模板),同时清空浏览器缓存。
- 检查报表类型:确保你的
ir.actions.report.xml记录中,report_type字段设置为qweb-pdf或qweb-html。 - 避免变量冲突:不要在模板中自定义同名的
page或nb_pages变量,会覆盖系统注入的值。
内容的提问来源于stack exchange,提问作者Ancient




