原文地址:http://www.cnblogs.com/ygj0930/p/7151732.html
一:概述
報表是使用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中生成二維碼,然后在生成報表時嵌入:

還可以使用查詢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