1. 程式人生 > 其它 >odoo開發教程十三:qweb報表

odoo開發教程十三:qweb報表

一:概述

報表是使用qweb定義的,報表的pdf匯出是使用wkhtmltopdf來完成的。

如果需要為一個模型建立報表,需要定義report及對應模板

如果有需要的話還可以指定特定的紙張格式

如果需要訪問其他模型,就需要定義Custom Report。

二:Report

report標籤可用於定義一個報表:

id - 生成的資料的id
name (必選) - 報表名用於查詢及描述
model (必選) - 報表所對應的模型
report_type (必選) - qweb-pdf: pdf | qweb-html : html
report_name - 輸出pdf時檔名
groups - Many2many欄位用於指定可以檢視使用該報表的使用者組
attachment_use - 如果設定為true時,該報表會以記錄的附件的形式儲存,一般用於一次生成多次使用的報表
attachment - 用於定義報表名的python表示式,記錄可以通過object物件訪問
paperformat - 用於列印報表的檔案格式的外部id
(預設是公司的格式)(可以自定義格式)
<report
    id="account_invoices"
    model="account.invoice"
    string="Invoices"
    report_type="qweb-pdf"
    name="account.report_invoice"
    file="account.report_invoice"
    attachment_use="True"
    attachment="(object.state in ('open','paid')) and
        ('INV'+(object.number or '').replace('/','')+'.pdf')" //拼接檔名
/>


三:報表模板

<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內容會是<div class="page">裡的內容

模板id需與報表宣告中一致,比如上面的account.report_invoice,由於這是qweb模板,可以在docs物件中取得欄位內容。

四:報表嵌入二維碼

在controller中生成二維碼,然後在生成報表時嵌入:

![]('/report/barcode/QR/%s' % 'My text in qr code')
還可以使用查詢url來傳多個引數:<img t-att-src="'/report/barcode/?
    type=%s&value=%s&width=%s&height=%s'%('QR', 'text', 200, 200)"/>

五:報表格式

檔案格式用report.paperformat記錄來定義:

name (必選) - 用於查詢及區分的名字
description - 格式的描述
format - 一個預定義的格式如(A0-A9,B0-B10等)或自定義,預設是A4
dpi - 輸出的DPI,預設90
margin_top, margin_bottom, margin_left, margin_right - mm為單位的margin值
page_height, page_width - mm為單位的尺寸
orientation - 橫向或縱向 Landscape , Portrait
header_line - boolean,是否顯示標題行
header_spacing - mm為單位的頭部空白
<record id="paperformat_frenchcheck" model="report.paperformat">
    <field name="name">French Bank Check</field>
    <field name="default" eval="True"/>
    <field name="format">custom</field>
    <field name="page_height">80</field>
    <field name="page_width">175</field>
    <field name="orientation">Portrait</field>
    <field name="margin_top">3</field>
    <field name="margin_bottom">3</field>
    <field name="margin_left">3</field>
    <field name="margin_right">3</field>
    <field name="header_line" eval="False"/>
    <field name="header_spacing">3</field>
    <field name="dpi">80</field>
</record>

六:檢視報表

報表是標準的web頁面,所以可以通過連結直接訪問:

html版本報表可以通過 :http://localhost:8069/report/html/報表名/1

pdf版本通過 : http://localhost:8069/report/pdf/報表名/1