How to add custom header and footer in QWEB reports in odoo - BOOKC -->

How to add custom header and footer in QWEB reports in odoo

Friday, October 19, 2018

In this article I am going to teach you how to add custom header and footer in QWEB reports in odoo. You guys already know that how to customize the default Odoo header and footer in QWEB reports by changing the company setting. Sometime you want to customize report header and footer according to your desire instead of showing company information, to do that we need to edit the QWEB view. Here we will inherit/override existing template (xml) like report layout (report.external_layout_header and report.external_layout_footer) instead of directly changing in source code.



How to add custom header and footer in QWEB reports in odoo


Inherit or Override QWEB report header and footer


To customize report header and footer we need inherit/override these two report templates.
1- report.external_layout_header  
2- report.external_layout_footer

You just need to inherit or override above templates and simply apply bootstrap or CSS classes for your header and footer on your custom template according to your need and requirement.

Follow these steps to override report layouts.

  1. Create a new XML file and name it (custom_header.xml)
  2. Create a new XML file and name it (custom_footer.xml)
  3. Add this file into __manifest__.py or (__openerp__.py) file

custom_header.xml

In below code we have inherited or override "external_layout_header" template. And customize that template according to our need using Bootstrap and CSS.

<?xml version='1.0' encoding='utf-8'?>
<openerp>
<data>
<template id="monthly_report_header" inherit_id="report.external_layout_header">
 <xpath expr="//div[@class='header']" position="replace">
     <div class="header">
         <div class="row">
             <div class="col-xs-3">
                 
             </div>
             <div class="col-xs-9 text-right" style="margin-top:20px;">
              <h5>Custom Header</h5>
             </div>
         </div>
         <div class="row zero_min_height">
             <div class="col-xs-12">
                 <div style="border-bottom: 1px solid black;"/>
             </div>
         </div>        
     </div>
 </xpath>
</template>
</data>
</openerp>      
 

custom_footer.xml

In below code we have inherited or override "external_layout_footer" template. And customize that template according to our need using Bootstrap and CSS.


<?xml version='1.0' encoding='utf-8'?>
<openerp>
<data>
 <template id="monthly_report_footer" inherit_id="report.external_layout_footer">
 <xpath expr="//div[@class='footer']" position="replace">
    <div class="footer">
        <div class="text-center" style="border-top: 1px solid black;">
            <ul class="list-inline">
                <li>Developed By: Hassan Enterprises</li>

                <li>&amp;bull;</li>
                <li>Phone: +92 321 1234567</li>

                <li>&amp;bull;</li>
                <li>Email: abc@gmail.com</li>


            </ul>


            <ul class="list-inline">
                <li>Page:</li>
                <li><span class="page"/></li>
                <li>/</li>
                <li><span class="topage"/></li>
            </ul>
        </div>
    </div>    
 </xpath>
</template>
</data>
</openerp>      
 

Now we can use that template in our Odoo QWEB reports to display our customize header and footer.


<?xml version='1.0' encoding='utf-8'?>
<openerp>
<data>
    <report
        id="report_monthly_pensioners"
        model="dgmsw.employee.salary"
        string="Monthly Report"
        name="dgmsw.monthly_report"
        file="dgmsw.report_monthly_pensioners"
        report_type="qweb-pdf" />

    <template id="monthly_report">
 
                <!--Calling our customize report header template-->
    
                <t t-call="report.external_layout_header"/>
                </t>
 
                <t t-call="report.html_container">
                    <div class="page"> 
                         <div class="row">
                              <!--Your report logic goes here-->   
                         </div>
                    </div>
                </t>
    
                <!--Calling our customize report footer template-->
    
                <t t-call="report.external_layout_footer"/>
                </t>   
    </template>
</data>
</openerp>      
 

How to add custom header and footer in QWEB reports in odoo

In above code we have used "report.html_container" that's why we explicitly use "report.external_layout_header" and "report.external_layout_footer". If we will use "report.external_layout" than there is no need to call header and footer template.