PDF报表添加谷歌字体 - xiaohao0576/odoo-doc GitHub Wiki

  1. Google Fonts网站,找到需要添加的字体,点击Get Fonts -> Get embed code,选择@import类型
  2. 复制有@import的那一行,比如@import url('https://fonts.googleapis.com/css2?family=Fasthand&display=swap');
  3. 删除掉&display=swap,因为&符号在XML文档中需要转义才能保存,并且Odoo报表使用wkhtmltopdf,也不需要swap模式
  4. 在Qweb模板中添加<style>样式,一定要在class="article"的html元素中添加,示例如下:
<data inherit_id="web.basic_layout">
  <xpath expr=".article" position="inside">
    <style>
    @import url('https://fonts.googleapis.com/css2?family=Fasthand');
    
    .my-style {
      font-family: "Fasthand", cursive;
      font-weight: 400;
      font-style: normal;
    }
    </style>        
  </xpath>
        <t t-name="studio_report_document">
                    <div class="page"><div class="oe_structure"/>
                    <div class="my-style">
                        <p>ខ្ញុំស្រលាញ់កម្ពុជា។</p>
                    </div>
                    </div>
                </t>
 </data>

下图是在Odoo 18在线版中测试通过的

Screenshot 2025-06-28 02 47 51

官方文档: https://www.odoo.com/documentation/18.0/developer/reference/backend/reports.html#custom-fonts

为什么需要 class="article" ?

因为odoo在向wkhtmltopdf发送html前,会调用_prepare_html方法,只取出class="article"元素中的内容,然后使用web.minimal_layout重新生成html文件,最后再发给wkhtmltopdf渲染,所以放到article之外的内容,都不会被发送到wkhtmltopdf

参考源码: https://github.com/odoo/odoo/blob/6ba8d4944d5fe988746a1c640b34bf053c50ca5b/odoo/addons/base/models/ir_actions_report.py#L410

⚠️ **GitHub.com Fallback** ⚠️