### 打印報表
Odoo 8附帶了一個基于新的報表引擎 [QWeb](https://www.odoo.com/documentation/9.0/reference/qweb.html#reference-qweb), [Twitter Bootstrap](http://getbootstrap.com/) 和 [Wkhtmltopdf](http://wkhtmltopdf.org/).
報告是一個組合的元素:
* 一個 `ir.actions.report.xml`, 一個 `<report>` 提供了快捷元素,它為報表設置了各種基本參數(默認類型,是否應該將報表保存到數據庫中,…)
~~~ xml
<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')"
/>
~~~
* 一個標準 [QWeb view](https://www.odoo.com/documentation/9.0/reference/views.html#reference-views-qweb) 為實際報告:
~~~ xml
<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>
</div>
</t>
</t>
</t>
~~~
標準的渲染上下文提供了一些元素,最重要的:
``docs``
報告打印的記錄
``user``
用戶打印報表
因為報告是標準的網頁,他們都可以通過一個URL和輸出參數可以通過操縱這個URL,例如*發票*報告的HTML版本 通過[http://localhost:8069/report/html/account.report_invoice/1](http://localhost:8069/report/html/account.report_invoice/1) (如果 `account` 已安裝)和PDF版 通過[http://localhost:8069/report/pdf/account.report_invoice/1](http://localhost:8069/report/pdf/account.report_invoice/1).
Danger
看來你的PDF報告失蹤的風格(即文本出現,但風格/布局不同于HTML版),可能你的[ wkhtmltopdf ](http://wkhtmltopdf.org/)過程無法達到您的Web服務器下載。
如果你看看你的服務器日志,發現CSS樣式不被下載時生成一個PDF格式的報告,當然這是個問題。
wkhtmltopdf ]的[(http://wkhtmltopdf.org/)過程將使用`網站。基地。URL `系統參數為*根路徑*所有鏈接的文件,但此參數自動更新每次管理員登錄。如果您的服務器位于某種代理后面,那將無法訪問。你可以通過添加一個系統參數來解決這個問題:
* `report.url`,指向一個URL可從您的服務器(可能`http://localhost:8069` 或類似的東西)。它將用于這個特殊用途。
* `web.base.url.freeze`, 當設置為 `True`,將停止自動更新 `web.base.url`.
練習
創建會話模型的報告
為每一個會話,它應該顯示會話的名稱,其開始和結束,并列出會議的參加者。
*openacademy/__openerp__.py*
~~~ python
'views/openacademy.xml',
'views/partner.xml',
'views/session_workflow.xml',
'reports.xml',
],
# only loaded in demonstration mode
'demo': [
~~~
*openacademy/reports.xml*
~~~ xml
<openerp>
<data>
<report
id="report_session"
model="openacademy.session"
string="Session Report"
name="openacademy.report_session_view"
file="openacademy.report_session"
report_type="qweb-pdf" />
<template id="report_session_view">
<t t-call="report.html_container">
<t t-foreach="docs" t-as="doc">
<t t-call="report.external_layout">
<div class="page">
<h2 t-field="doc.name"/>
<p>From <span t-field="doc.start_date"/> to <span t-field="doc.end_date"/></p>
<h3>Attendees:</h3>
<ul>
<t t-foreach="doc.attendee_ids" t-as="attendee">
<li><span t-field="attendee.name"/></li>
</t>
</ul>
</div>
</t>
</t>
</t>
</template>
</data>
</openerp>
~~~