You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Odoo 15报表PDF中系统参数样式不生效问题求助

Odoo 15 报表PDF视图动态样式不生效问题解决

问题场景

已完成以下配置,HTML预览时样式正常,但PDF打印视图中颜色样式完全失效:

  • 配置系统参数(XML记录):
<odoo>
  <data noupdate="0">
    <record id="paleta_color1" model="ir.config_parameter">
      <field name="key">paleta_color1</field>
      <field name="value">#025aa4</field>
    </record>
  </data>
</odoo>

(尝试过RGB、十六进制两种颜色格式)

  • 报表中获取参数值:
<t t-set="col" t-value="env['ir.config_parameter'].get_param('paleta_color1', '')" />
  • 通过全局<style>绑定动态样式:
<style>
         .color {
             color:<t t-esc="col" />;
         }
         .fondo {
             border-left : 2px solid <t t-esc="col" />;
         }
         .fond {
             background-color: <t t-esc="col" />;
         }
</style>

问题根源

Odoo PDF报表依赖wkhtmltopdf渲染,该工具对QWeb模板中动态注入的全局CSS支持有限,<style>标签内的t-esc变量无法被正确解析渲染。

可行解决方案

方案1:用t-attf-style写内联样式

直接在标签上通过t-attf-style属性拼接动态样式,绕过全局样式的渲染限制:

<!-- 文字颜色示例 -->
<span t-attf-style="color: {{ col }};" class="color">测试文本</span>

<!-- 左侧边框示例 -->
<div t-attf-style="border-left: 2px solid {{ col }};" class="fondo">带边框容器</div>

<!-- 背景色示例 -->
<div t-attf-style="background-color: {{ col }};" class="fond">带背景容器</div>

方案2:拼接完整CSS后用t-raw输出

如果需要保留全局样式结构,可先把动态CSS内容拼接成字符串,再用t-raw输出:

<t t-set="custom_css" t-value="''"/>
<t t-set="custom_css" t-value="custom_css + '.color { color:' + col + '; }'"/>
<t t-set="custom_css" t-value="custom_css + '.fondo { border-left: 2px solid ' + col + '; }'"/>
<t t-set="custom_css" t-value="custom_css + '.fond { background-color: ' + col + '; }'"/>

<style>
    <t t-raw="custom_css"/>
</style>

方案3:检查wkhtmltopdf版本

确保使用Odoo 15推荐的wkhtmltopdf版本(0.12.5带patched qt),旧版本存在CSS解析兼容性问题,会导致动态样式失效。

验证步骤

修改后先预览HTML报表确认样式正常,再打印PDF查看效果。若仍有问题,可开启Odoo资产调试模式(访问报表时加?debug=assets参数),检查渲染后的HTML源码是否正确包含了动态颜色值。

内容的提问来源于stack exchange,提问作者Juan_jg

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.02 09:05:14