Odoo 打印开发

2018-07-12 2727点热度 0条评论

声明报告Actions

参考链接:QWeb Reports

<report
        id="budget_report"
        model="budget.budget"
        report_type="qweb-pdf"
        string="Budget Report"
        name="contract_manager.budget_report_template"
        file="contract_manager.budget_report_template"
        paperformat="budget_print_format"
        menu="True"
/>

定义纸张

参考链接:Paper Format

<record id="budget_print_format" model="report.paperformat">
	<field name="name">Budget Print format</field>
	<field name="format">A4</field>
	<field name="orientation">Landscape</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">90</field>
</record>

定义模板

<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>

调用report.external_layout将在报告上添加默认的页眉和页脚,正文将是其中的<div class="page">

报告中可以访问的一些特定变量,主要是:

  • docs  当前报告的数据记录集
  • doc_ids 记录的id列表
  • doc_model docs记录的模型
  • time 来自Python标准库的引用
  • user res.user用户打印报告的记录
  • res_company 当前user的公司

自定义报告

使用自定义报告需要具有一个默认的 get_html 函数,如果你希望在模板中包含更多的内容(例如,其他模型记录)来自定义报告,则可以定义此模型,覆盖函数并在字典中传递对象: report.module.report_name render_html docargs

from odoo import api, models

class ParticularReport(models.AbstractModel):
    _name = 'report.module.report_name'
    @api.model
    def render_html(self, docids, data=None):
        report_obj = self.env['report']
        report = report_obj._get_report_from_name('module.report_name')
        docargs = {
            'doc_ids': docids,
            'doc_model': report.model,
            'docs': self,
        }
        return report_obj.render('module.report_name', docargs)

Jalena

原创内容,转载请注明出处! 部分内容来自网络,请遵守法律适用!

文章评论