原文地址:http://www.cnblogs.com/ygj0930/p/7151732.htmlhtml
一:概述python
報表是使用qweb定義的,報表的pdf導出是使用wkhtmltopdf來完成的。web
若是須要爲一個模型建立報表,須要定義report及對應模板。url
若是有須要的話還能夠指定特定的紙張格式,spa
若是須要訪問其餘模型,就須要定義Custom Report。code
二:Reportorm
report
標籤可用於定義一個報表:htm
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">
裏的內容。blog
模板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