Search code examples
pdfodooreportodoo-14qweb

Custom filename QWEB report [ODOO 14.0]


I need a big help.

I need to change the report filename when the pdf is downloaded.

This is the button:

<button
   string="Generate Report"
   name="generate_report"
   type="object"
   class="btn-primary"
 />

This is the "generate_report" python function:

def generate_report(self):
        data = {
            'blabla': self.employee_id.name,
            'bla': self.employee_id.place_of_birth
        }

        return self.env.ref('module.action_generate_report').report_action(self, data=data)

And this is the action

<record id="action_generate_report" model="ir.actions.report">
    <field name="name">Generate Report</field>
    <field name="model">custom.module</field>
    <field name="report_type">qweb-pdf</field>
    <field name="report_name">module.generate_report_template</field>
    <field name="report_file">module.generate_report_template</field>
    <field name="print_report_name">"Report - %s" % object.name</field>
    <field name="paperformat_id" ref="paperformat_margin" />
</record>

If i use the "Print" button it works correctly, but if i use the custom button the solution above not work, everytime the file is downloaded, it has the name "Generate Report.pdf"

How to solve this problem?

Thank you


Solution

  • When you define the data parameter, Odoo will not add the active_ids to the report urls:

    if (_.isUndefined(action.data) || _.isNull(action.data) ||
       (_.isObject(action.data) && _.isEmpty(action.data))) {
       if (action.context.active_ids) {
           var activeIDsPath = '/' + action.context.active_ids.join(',');
           reportUrls = _.mapObject(reportUrls, function (value) {
               return value += activeIDsPath;
           });
       }
       reportUrls.html += '?context=' + encodeURIComponent(JSON.stringify(session.user_context));
    }
    

    and then will fail to get the docids from it in the report_download controller:

    pattern = '/report/pdf/' if type == 'qweb-pdf' else '/report/text/'
    reportname = url.split(pattern)[1].split('?')[0]
    
    docids = None
    if '/' in reportname:
        reportname, docids = reportname.split('/')
    

    which is used to compute the report file name.

    If docids is None, Odoo will not compute the report file name from print_report_name expression and will use the report name (report.name)

    if docids:
        ids = [int(x) for x in docids.split(",")]
        obj = request.env[report.model].browse(ids)
        if report.print_report_name and not len(obj) > 1:
            report_name = safe_eval(report.print_report_name, {'object': obj, 'time': time})
            filename = "%s.%s" % (report_name, extension)
    

    You don't need to use data parameter, current record fields are accessible throw docs variable. To fix this issue remove the data parameter or set it to None or {}