PDF报表添加谷歌字体 - xiaohao0576/odoo-doc GitHub Wiki
- 在Google Fonts网站,找到需要添加的字体,点击
Get Fonts -> Get embed code
,选择@import类型 - 复制有@import的那一行,比如
@import url('https://fonts.googleapis.com/css2?family=Fasthand&display=swap');
- 删除掉
&display=swap
,因为&符号在XML文档中需要转义才能保存,并且Odoo报表使用wkhtmltopdf,也不需要swap模式 - 在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在线版中测试通过的
官方文档: https://www.odoo.com/documentation/18.0/developer/reference/backend/reports.html#custom-fonts
因为odoo在向wkhtmltopdf发送html前,会调用_prepare_html
方法,只取出class="article"
元素中的内容,然后使用web.minimal_layout
重新生成html文件,最后再发给wkhtmltopdf渲染,所以放到article之外的内容,都不会被发送到wkhtmltopdf