https://www.odoo.com/documentation/8.0/reference/reports.html
Reports are written in HTML/QWeb, like all regular views in Odoo. You can usethe usual QWeb control flow tools. The PDF renderingitself is performed by wkhtmltopdf.
If you want to create a report on a certain model, you will need to definethis Report and theReport template it will use. If you wish, you can alsospecify a specific Paper Format for thisreport. Finally, if you need access to more than your model, you can define aCustom Reports class that gives you access to moremodels and records in the template.
Report
Every report must be declared by a report action.
For simplicity, a shortcut <report>
element is available to define areport, rather than have to set up the action and its surroundings manually. That <report>
can take the following attributes:
- the generated record's external id
- only useful as a mnemonic/description of the report when looking for onein a list of some sort
- the model your report will be about
-
either
qweb-pdf
for PDF reports orqweb-html
for HTML - the name of your report (which will be the name of the PDF output)
-
Many2many
field to the groups allowed to view/usethe current report -
if set to True, the report will be stored as an attachment of the recordusing the name generated by the
attachment
expression; you can usethis if you need your report to be generated only once (for legal reasons,for example) -
python expression that defines the name of the report; the record isacessible as the variable
object
id
name
(mandatory)
model
(mandatory)
report_type
(mandatory)
report_name
groups
attachment_use
attachment
Warning
The paper format cannot currently be declared via the <report>
shortcut, it must be added afterwards using a <record>
extension on thereport action itself:
<record id="<report_id>" model="ir.actions.report.xml">
<field name="paperformat_id" ref="<paperformat>"/>
</record>
Example:
<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')"
/>
Report template
Minimal viable template
A minimal template would look like:
<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>
Calling external_layout
will add the default header and footer on yourreport. The PDF body will be the content inside the <divclass="page">
. The template's id
must be the name specified in thereport declaration; for example account.report_invoice
for the abovereport. Since this is a QWeb template, you can access all the fields of thedocs
objects received by the template.
There are some specific variables accessible in reports, mainly:
- records for the current report
-
list of ids for the
docs
records -
model for the
docs
records -
a reference to
time
from the Python standard library -
a function to translate a part of a report. It must be used as follow:
<t t-foreach="doc_ids" t-as="doc_id"> <t t-raw="translate_doc(doc_id, doc_model, 'partner_id.lang', account.report_invoice_document')"/> </t>
-
res.user
record for the user printing the report -
record for the current
user
's company
docs
doc_ids
doc_model
time
translate_doc
user
res_company
If you wish to access other records/models in the template, you will needa custom report.
Translatable Templates
If you wish to translate reports (to the language of a partner, for example),you need to define two templates:
- The main report template
- The translatable document
You can then call translate_doc from your main template to obtain thetranslated document. If you wish to see the details of the translation in thebackend, you can go to
.For example, let's look at the Sale Order report from the Sale module:
<!-- Main template -->
<template id="sale.report_saleorder">
<t t-call="report.html_container">
<t t-foreach="doc_ids" t-as="doc_id">
<t t-raw="translate_doc(doc_id, doc_model, 'partner_id.lang', 'sale.report_saleorder_document')"/>
</t>
</t>
</template>
<!-- Translatable template -->
<template id="report_saleorder_document">
<t t-call="report.external_layout">
<div class="page">
<div class="oe_structure"/>
<div class="row">
<div class="col-xs-6">
<strong t-if="o.partner_shipping_id == o.partner_invoice_id">Invoice and shipping address:</strong>
<strong t-if="o.partner_shipping_id != o.partner_invoice_id">Invoice address:</strong>
<div t-field="o.partner_invoice_id" t-field-options="{"no_marker": true}"/>
<...>
<div class="oe_structure"/>
</div>
</t>
</template>
The main template calls translate_doc with partner_id.lang
as a parameter,which means it uses a custom report model to access a res.partner
record.
Barcodes
Barcodes are images returned by a controller and can easily be embedded inreports thanks to the QWeb syntax:
<img t-att-src="'/report/barcode/QR/%s' % 'My text in qr code'"/>
More parameters can be passed as a query string
<img t-att-src="'/report/barcode/?
type=%s&value=%s&width=%s&height=%s'%('QR', 'text', 200, 200)"/>
Useful Remarks
- Twitter Bootstrap and FontAwesome classes can be used in your reporttemplate
- Local CSS can be put directly in the template
-
Global CSS can be inserted in the main report layout by inheriting itstemplate and inserting your CSS:
<template id="report_saleorder_style" inherit_id="report.style"> <xpath expr="."> <t> .example-css-class { background-color: red; } </t> </xpath> </template>
- If it appears that your PDF report is missing the styles, please checkthese instructions.
Paper Format
Paper formats are records of report.paperformat
and can contain thefollowing attributes:
- only useful as a mnemonic/description of the report when looking for onein a list of some sort
- a small description of your format
-
either a predefined format (A0 to A9, B0 to B10, Legal, Letter,Tabloid,...) or
custom
; A4 by default. You cannot use a non-customformat if you define the page dimensions. - output DPI; 90 by default
- margin sizes in mm
- page dimensions in mm
- Landscape or Portrait
- boolean to display a header line
- header spacing in mm
name
(mandatory)
description
format
dpi
margin_top
,
margin_bottom
,
margin_left
,
margin_right
page_height
,
page_width
orientation
header_line
header_spacing
Example:
<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>
Custom Reports
The report model has a default get_html
function that looks for a modelnamed report.module.report_name
. If it exists, it will use it tocall the QWeb engine; otherwise a generic function will be used. If you wishto customize your reports by including more things in the template (likerecords of others models, for example), you can define this model, overwritethe function render_html
and pass objects in the docargs
dictionnary:
from openerp import api, models
class ParticularReport(models.AbstractModel):
_name = 'report.module.report_name'
@api.multi
def render_html(self, data=None):
report_obj = self.env['report']
report = report_obj._get_report_from_name('module.report_name')
docargs = {
'doc_ids': self._ids,
'doc_model': report.model,
'docs': self,
}
return report_obj.render('module.report_name', docargs)
Reports are web pages
Reports are dynamically generated by the report module and can be accesseddirectly via URL:
For example, you can access a Sale Order report in html mode by going tohttp://<server-address>/report/html/sale.report_saleorder/38
Or you can access the pdf version athttp://<server-address>/report/pdf/sale.report_saleorder/38