PDF报告¶
使用 Studio,您可以 编辑现有的 PDF 报告。
默认布局¶
报告的默认布局在Studio之外管理。转到 公司 部分,点击 配置文档布局。布局设置是公司特定的,但适用于所有报告。
,然后在小技巧
您可以在右侧的报告预览中查看不同设置如何影响报告布局,或通过点击 下载 PDF 预览 下载示例发票 PDF。
使用以下设置:
布局: 提供了四种布局:
字体: 提供了七种字体:Lato、Roboto、Open Sans、Montserrat、Oswald、Raleway 和 Tajawal(支持阿拉伯语和拉丁语脚本)。前往 Google Fonts 网站 预览这些字体。
公司标志: 点击 编辑 按钮以上传或更改标志。这会将标志添加到 公司 模型中的公司记录中,您可以通过进入 ,然后在 公司 部分点击 更新信息 来访问该记录。
颜色: 更改用于构建报告的主要和次要颜色。默认颜色是根据徽标的颜色自动生成的。
布局背景: 以下背景可供选择:
空白: 什么都不显示。
几何: 在背景中显示了一个展示几何形状的图像。
自定义: 上传自定义背景图片。
公司标语: 这会显示在 外部报告 的页眉中。你可以添加多行文本。
公司详情:这些内容会显示在 外部报告 的页眉中。您可以添加多行文本。
纸张格式: 用于定义报告的默认纸张大小。您可以选择 A4 (21 厘米 x 29.7 厘米)、US Letter (21.59 厘米 x 27.54 厘米) 或 二维码页面。这也可以在 Studio 中的 纸张格式 字段中为单个报告进行定义。
创建新的 PDF 报告¶
要为模型创建新报告,请访问该模型,点击 切换 Studio 按钮,然后点击 报告。点击 新建,在打开的弹出窗口中,选择报告类型。这仅用于确定页眉和页脚中显示的内容:
创建报告后,您可以开始 编辑它。
编辑 PDF 报告¶
要访问某个模型可用的报告,请进入该模型(例如销售订单),点击 切换 Studio 按钮,然后点击 报告。选择一个现有报告以打开它,或 创建一个新的。
小技巧
或者,您也可以打开 Studio,点击 报告,并搜索特定的报告或模型。
重要
强烈建议 复制 标准报告并在复制的版本中进行更改。要复制报告,请将鼠标指针悬停在报告右上角,点击垂直省略号图标 (⋮),然后选择 复制。

选项¶
一旦您选择或创建了一个报告,您可以使用屏幕左侧的选项来:
Report editor¶
报告编辑器允许您修改报告的格式和内容。
小技巧
您可以使用相关按钮或快捷键
CTRL Z
和CTRL Y
来 撤销 或 重做 更改。离开报表或手动使用 保存 按钮时,更改会自动保存。
您可以通过点击屏幕左侧的 重置报告 按钮将报告重置为标准版本。
重要
编辑报告的页眉和页脚会影响所有标准和自定义报告。
条件块¶
虚线矩形代表 条件块 (if/else 语句)。这些用于根据特定条件显示/隐藏内容。点击块以查看条件。

选择一个值以预览其对应的输出,并在必要时进行编辑。

注解
条件只能在 XML 中编辑。
Other content¶
报告中有两种类型的文本内容:
静态文本,即未以蓝色高亮显示的文本,可直接在编辑器中修改。
动态文本,即用蓝色高亮显示的文本,在生成报告时会被字段值替换,例如销售订单号或报价日期。
您可以使用命令向报告添加内容(例如,字段、列表、表格、图像、横幅等)。输入 /
打开 powerbox,然后输入命令名称或从列表中选择。
要在报告中添加静态文本,请在所需位置输入文本。
如需进行更高级的更改,您可以 直接在 XML 中编辑报告。
Add a field¶
要添加字段,请键入 /
并选择 字段 命令。在打开的列表中,选择或搜索字段;如果需要,点击字段名称旁边的右箭头以访问相关字段列表。然后,指定默认值并按 Enter
。

添加数据表¶
数据表用于显示 关系字段。要添加数据表,输入 /
,选择 动态表格 命令,并选择要在表格中显示的关系。
注解
只有 one2many
或 many2many
类型的关系可以显示为数据表格。
表格添加完成后,您可以使用表格工具添加列。将光标定位在列上方,然后点击紫色矩形并选择一个选项。

然后,您可以在列中插入 您选择的字段。打开的对话框显示了该字段的源对象(例如,标签 模型)以及可用字段的列表。

注解
默认行会自动遍历字段内容,为每个字段值在报告中生成一行(例如,每个标签一行)。您可以使用表格工具在生成行的上方或下方添加静态内容行。
您还可以通过 修改报告的XML 来添加数据表。
格式化¶
要在报告中格式化文本,请先选中它,然后使用 文本编辑器 中的选项进行格式化。
编辑报告的 XML¶
警告
直接修改 XML 可能会导致在 升级 期间出现报告问题。如果发生这种情况,只需将您的更改从旧数据库复制到升级后的数据库中即可。
要编辑报告的XML,请在左侧面板中点击 编辑源代码。
示例¶
有时,由于复杂的结构,表格无法被正确识别。在这种情况下,您仍然可以在报告 XML 中手动修改它们。例如,对于销售订单,您可以在 XML 中找到以下结构(为文档目的简化):
<!-- table root element --> <table> <!-- thead = table header, the row with column titles --> <thead> <!-- table row element --> <tr> <!-- table header element --> <th>Name</th> <th>Price</th> </tr> </thead> <!-- table body, the main content --> <tbody> <!-- we create a row for each subrecord with t-foreach --> <tr t-foreach="record.some_relation_ids" t-as="line"> <!-- for each line, we output the name and price as table cells --> <td t-out="line.name"/> <td t-out="line.price"/> </tr> </tbody> </table>要修改表格,必须确保每行具有相同数量的数据单元格。例如,在上述情况下,您需要在标题部分添加一个单元格(例如,列标题),并在正文部分添加另一个单元格,其中包含字段内容(通常使用
t-out
或t-field
指令)。<table> <!-- table root element --> <thead> <!-- thead = table header, the row with column titles --> <tr> <!-- table row element --> <th>Name</th> <!-- table header element --> <th>Price</th> <th>Category</th> </tr> </thead> <tbody> <!-- table body, the main content --> <tr t-foreach="record.some_relation_ids" t-as="line"> <!-- we create a row for each subrecord with t-foreach --> <td t-out="line.name"/> <!-- for each line, we output the name and price as table cells --> <td t-out="line.price"/> <td t-out="line.category_id.display_name"/> </tr> </tbody> </table>注解
单元格可以跨越多行或多列。欲了解更多信息,请访问 Mozilla 开发者网络网站。
例如,您可以修改 报价单/订单 报告,添加一列以在主表中显示产品类别:
<table class="table table-sm o_main_table table-borderless mt-4"> <!-- In case we want to repeat the header, remove "display: table-row-group" --> <thead style="display: table-row-group"> <tr> <th name="th_description" class="text-start">Description</th> <th>Product Category</th> <th name="th_quantity" class="text-end">Quantity</th> <th name="th_priceunit" class="text-end">Unit Price</th> [...] <tr t-att-class="'bg-200 fw-bold o_line_section' if line.display_type == 'line_section' else 'fst-italic o_line_note' if line.display_type == 'line_note' else ''"> <t t-if="not line.display_type"> <td name="td_name"><span t-field="line.name">Bacon Burger</span></td> <td t-out="line.product_id.categ_id.display_name"/> <td name="td_quantity" class="text-end"> <span t-field="line.product_uom_qty">3</span> <span t-field="line.product_uom">units</span> <span t-if="line.product_packaging_id">![]()
要在 XML 中添加表格,您需要知道您希望访问和显示的字段和对象的名称。例如,让我们添加一个详细说明销售订单上标签的表格:
<!-- table root element -->
<table class="table">
<!-- thead = table header, the row with column titles -->
<thead>
<!-- table row element -->
<tr>
<!-- table header element -->
<th>ID</th>
<th>Name</th>
</tr>
</thead>
<!-- table body, the main content -->
<tbody>
<!-- we create a row for each subrecord with t-foreach -->
<tr t-foreach="doc.tag_ids" t-as="tag">
<!-- for each line, we output the name and price as table cells -->
<td t-out="tag.id"/>
<td t-out="tag.name"/>
</tr>
</tbody>
</table>

注解
手动添加表格时,使用 Bootstrap 类 对其进行样式设置,例如上面示例中包含的 table
类。
如果你想根据特定条件显示/隐藏内容,可以手动在报告 XML 中添加 if/else
控制语句。
例如,如果您希望在报价中没有标签时隐藏自定义数据表,可以使用 t-if
属性来定义条件,该条件随后会被评估为 True
或 False
。如果报价中没有标签,表格将不会显示。
<!-- table root element -->
<table class="table" t-if="len(doc.tag_ids) > 0">
<!-- thead = table header, the row with column titles -->
<thead>
<!-- table row element -->
<tr>
<!-- table header element -->
<th>ID</th>
<th>Name</th>
</tr>
</thead>
<!-- table body, the main content -->
<tbody>
<!-- we create a row for each subrecord with t-foreach -->
<tr t-foreach="doc.tag_ids" t-as="tag">
<!-- for each line, we output the name and price as table cells -->
<td t-out="tag.id"/>
<td t-out="tag.name"/>
</tr>
</tbody>
</table>
如果你想在 t-if
语句评估为 False
时显示另一个块,可以使用 t-else
语句来指定。t-else
块必须在文档结构中直接跟在 t-if
块之后。t-else
属性中无需指定任何条件。举个例子,让我们展示一条简短的消息,说明报价单上没有标签:
<!-- table root element -->
<table class="table" t-if="len(doc.tag_ids) > 0">
<!-- thead = table header, the row with column titles -->
<thead>
<!-- table row element -->
<tr>
<!-- table header element -->
<th>ID</th>
<th>Name</th>
</tr>
</thead>
<!-- table body, the main content -->
<tbody>
<!-- we create a row for each subrecord with t-foreach -->
<tr t-foreach="doc.tag_ids" t-as="tag">
<!-- for each line, we output the name and price as table cells -->
<td t-out="tag.id"/>
<td t-out="tag.name"/>
</tr>
</tbody>
</table>
<div class="text-muted" t-else="">No tag present on this document.</div>
通过使用 t-if/t-else
标记,报告编辑器识别出这些部分是互斥的,并应显示为条件块:

您可以使用编辑器切换条件以预览其输出:

如果您希望有多个选项,也可以使用 t-elif
指令来添加中间条件。例如,这就是销售订单报告的标题如何根据基础文档的条件而变化。
<h2 class="mt-4">
<span t-if="env.context.get('proforma', False) or is_pro_forma">Pro-Forma Invoice # </span>
<span t-elif="doc.state in ['draft','sent']">Quotation # </span>
<span t-else="">Order # </span>
<span t-field="doc.name">SO0000</span>
</h2>
标题 Pro-Forma Invoice 的使用取决于某些上下文条件。如果这些条件未满足且文档的状态为 draft
或 sent
,则使用 Quotation。如果这些条件均未满足,报告的标题为 Order。
在报告中处理图像可能具有挑战性,因为对图像大小和行为的精确控制并不总是显而易见。您可以使用报告编辑器插入图像字段(通过使用 /Field 命令),但在 XML 中使用 t-field
指令和相应的 t-options
属性插入它们可以提供更好的大小和位置控制。
例如,以下代码将行的产品的 image_128
字段输出为 64 像素宽的图像(高度根据图像的宽高比自动调整)。
<span t-field="line.product_id.image_128" t-options-widget="image" t-options-width="64px"/>
以下选项适用于图像小部件:
width
: 图像的宽度,通常以像素或 CSS 长度单位(例如rem
)表示(留空则为自动宽度)。height
: 图像的高度,通常以像素或 CSS 长度单位(例如rem
)表示(留空则为自动高度)。class
: 应用于img
标签的 CSS 类;可用的 Bootstrap 类。alt
: 图像的替代文本style
: 样式属性;它允许你比使用 Bootstrap 类 更自由地覆盖样式。
这些属性必须包含字符串,即引号内的文本,例如 t-options-width="'64px'"
(或者,也可以是一个有效的 Python 表达式)。
注解
图像部件不能用于 img
标签。相反,应在 span
(用于内联内容)或 div
(用于块内容)节点上设置 t-field
指令。
例如,让我们在报价表中添加一列产品图片:
<table class="table table-sm o_main_table table-borderless mt-4">
<thead style="display: table-row-group">
<tr>
<th>Image</th>
<th name="th_description" class="text-start">Description</th>
<th>Product Category</th>
<th name="th_quantity" class="text-end">Quantity</th>
<th name="th_priceunit" class="text-end">Unit Price</th>
[...]
<t t-foreach="lines_to_report" t-as="line">
<t t-set="current_subtotal" t-value="current_subtotal + line.price_subtotal"/>
<tr t-att-class="'bg-200 fw-bold o_line_section' if line.display_type == 'line_section' else 'fst-italic o_line_note' if line.display_type == 'line_note' else ''">
<t t-if="not line.display_type">
<td>
<span t-field="line.product_template_id.image_128"
t-options-widget="'image'"
t-options-width="'64px'"
t-options-class="'rounded-3 shadow img-thumbnail'"
/>
</td>
<td name="td_name"><span t-field="line.name">Bacon Burger</span></td>
<td t-out="line.product_id.categ_id.display_name"/>
t-options-width
属性将图像宽度限制为 64 像素,而 t-options-class
中使用的 Bootstrap 类创建了一个类似缩略图的边框,带有圆角和阴影。
