0
votes

I want to print report by clicking on print button on website.

But it shows some error:

File "/home/priya/workspace/ODOO11/odoo-11.0/odoo/http.py", line 829, in dispatch r = self._call_function(**self.params) File "/home/priya/workspace/ODOO11/odoo-11.0/odoo/http.py", line 342, in _call_function return checked_call(self.db, *args, **kwargs) File "/home/priya/workspace/ODOO11/odoo-11.0/odoo/service/model.py", line 97, in wrapper return f(dbname, *args, **kwargs) File "/home/priya/workspace/ODOO11/odoo-11.0/odoo/http.py", line 335, in checked_call result = self.endpoint(*a, **kw) File "/home/priya/workspace/ODOO11/odoo-11.0/odoo/http.py", line 936, in call return self.method(*args, **kw) File "/home/priya/workspace/ODOO11/odoo-11.0/odoo/http.py", line 515, in response_wrap response = f(*args, **kw) File "/home/priya/repo/rp-group/rpg_quotation/controllers/web_page.py", line 1442, in update_quotation res = self.print_quotation_software_report(data, int(quotation_id)) File "/home/priya/repo/rp-group/rpg_quotation/controllers/web_page.py", line 2699, in print_quotation_software_report pdf = request.env.ref('rpg_quotation.rpg_quotation_software_setwise__report').report_action(self, data=data, config=False) File "/home/priya/workspace/ODOO11/odoo-11.0/odoo/addons/base/ir/ir_actions_report.py", line 703, in report_action context = dict(self.env.context, active_ids=active_ids)

UnboundLocalError: local variable 'active_ids' referenced before assignment

My js code:

$(document).on('click', Quotation.elements.print_quotation_software_selector, function() {
 var self = $(this);
 var data = {
   'xpath': null,
   'cmd': 'print_quotation_software_report'
 };
 Quotation.methods.xhr(data, function(r) {
 });

});

My Python code:

def print_quotation_software_report(self,data,quotation_id):
 order_id = quotation_id
 if quotation_id:
   pdf = request.env.ref('rpg_quotation.rpg_quotation_software_setwise__report').report_action(self, data=data, config=False)
   pdfhttpheaders = [('Content-Type', 'application/pdf'), ('Content-Length', len(pdf))]
   return request.make_response(pdf, headers=pdfhttpheaders)

Here,

rpg_quotation is the module name and rpg_quotation_software_setwise__report is report id.

2

2 Answers

1
votes

You can try this

Printed the report by giving a href to the button like this,

<a t-attf-href="'/report/pdf/account.report_invoice/%s' % i.id">
<button type="button" class="btn btn-primary btn-md o_website_form_send">Print Invoice</button>

In the i.id i have the id of the invoice. This is the format, report/type_of_the_report/module_name.template_name/id

To print the report from the controller,

@http.route('/school/card', methods=['POST', 'GET'], csrf=False, type='http', auth="user", website=True)
def print_id(self, **kw):
    student_id = kw['stud_id']
    if student_id:
        pdf = request.env['report'].sudo().get_pdf([student_id], 'module_name.report_name', data=None)
        pdfhttpheaders = [('Content-Type', 'application/pdf'), ('Content-Length', len(pdf))]
        return request.make_response(pdf, headers=pdfhttpheaders)
    else:
        return request.redirect('/')
0
votes

Maybe it's because you are passing self as the value for docids positional argument for the report_action method call instead of quotation_id to remove your actual error.

But that report_action method call will not return the pdf file data. You will need to change it to:

pdf = request.env.ref('rpg_quotation.rpg_quotation_software_setwise__report').sudo().render_qweb_pdf([quotation_id])[0]

See an example at:

https://github.com/odoo/odoo/blob/b29b545fe8464610ce04ac8be11e0356962d10d9/addons/sale/controllers/portal.py#L196