Dtcloud报表编写

136 阅读2分钟

报表模块

最小可行的模板

最小的模板看起来像:

<template id="report_invoice">
  <t t-call="report.html_container">
    <t t-foreach="docs" t-as="o">
      <t t-call="report.external_layout">
        <div class="page">
           <h2>Report title</h2>
           <p>This object's name is <span t-field="o.name"/></p>
        </div>
      </t>
    </t>
  </t>
</template>

调用external_layout 将在报表中添加默认页眉和页脚。PDF正文将是

内部的内容。模版的 id 必须是报表声明中指定的名称; 例如上面报表中的 account.report_invoice 。由于这是一个QWeb模板,你可以访问所有被模版接收的docs对象的字段。 报表中有一些特定的变量可以访问,主要是:

  • docs : 当前报告的记录
  • doc_ids : docs记录的ID列表
  • doc_model : docs记录模型
  • time : time来自Python标准库的引用
  • user : res.user 用户打印报告的记录
  • res_company :记录当前user的公司

如果您希望访问模板中的其他记录/模型,则需要一个自定义报表

可翻译的模版

如果您希望将报表转换为合作伙伴的语言,则需要定义两个模板:

  • 主报表模板
  • 翻译的文件

然后你可以从你的主模版中调用翻译文件,这个模版带有一个设置语言代码(例如fr或en_US)或者记录字段的属性t-lang。你也将需要重新浏览与适当的上下文相关的记录,如果你使用的是可译的字段(如国家名、销售条件等)。 警告: 如果你的报告模板不使用翻译的记录字段,再浏览其他语言记录是不必要的,会影响性能。

条码

条码是由一个控制器返回的图片,可以很容易地嵌入到QWeb语法报告:

<img t-att-src="'/report/barcode/QR/%s' % 'My text in qr code'"/>

可以将更多参数作为查询字符串传递

<img t-att-src="'/report/barcode/?
type=%s&value=%s&width=%s&height=%s'%('QR', 'text', 200, 200)"/>

有用的备注

  • Twitter Bootstrap和FontAwesome类可以用在你的报告模板
  • 本地的CSS可以直接放在模板中
  • 通过继承模板并插入CSS,可以在主报表布局中插入全局CSS:
<template id="report_saleorder_style" inherit_id="report.style">
  <xpath expr=".">
    <t>
     .example-css-class {
       background-color: red;
      }
    </t>
  </xpath>
</template>

报表格式

报表格式用report.paperformat记录来定义,字段有: 1)name (必选) 用于查找及区分的名字。 2)description 格式的描述。 3)format 一个预定义的纸张大小格式如(A0-A9,B0-B10等)或自定义custom,默认是A4。 4)dpi 输出的DPI,默认90。 5)margin_top, margin_bottom, margin_left, margin_right 以 mm 为单位的margin值。 6)page_height, page_width 以mm为单位的页面宽高尺寸值。 7)orientation 纸张横向或纵向打印。 8)Landscape , Portrait header_line boolean类型,是否显示标题行。 9)header_spacing 以 mm为单位的头部空白尺寸。

<record id="paperformat_euro_landscape"model="report.paperformat">
  <field name="name">European A4 Landscape</field>
  <field name="default" eval="True" />
  <field name="format">A4</field>
  <field name="page_height">0</field>
  <field name="page_width">0</field>
  <field name="orientation">Landscape</field>
  <field name="margin_top">40</field>
  <field name="margin_bottom">23</field>
  <field name="margin_left">7</field>
  <field name="margin_right">7</field>
  <field name="header_line" eval="False" />
  <field name="header_spacing">35</field>
  <field name="dpi">90</field>
</record>