如何在odoo11中从网站打印报告?

时间:2018-10-16 07:04:17

标签: odoo-11

我想通过单击网站上的“打印”按钮来打印报告。

但是它显示了一些错误:

  

文件829行中的文件“ /home/priya/workspace/ODOO11/odoo-11.0/odoo/http.py”       r = self._call_function(** self.params)     _call_function中的文件“ /home/priya/workspace/ODOO11/odoo-11.0/odoo/http.py”,第342行       返回checked_call(self.db,* args,** kwargs)     包装中的文件“ /home/priya/workspace/ODOO11/odoo-11.0/odoo/service/model.py”,第97行       返回f(dbname,* args,** kwargs)     在checked_call中,文件“ /home/priya/workspace/ODOO11/odoo-11.0/odoo/http.py”第335行       结果= self.endpoint(* a,** kw)     在致电中,文件“ /home/priya/workspace/ODOO11/odoo-11.0/odoo/http.py”,第936行       返回self.method(* args,** kw)     在response_wrap中,文件“ /home/priya/workspace/ODOO11/odoo-11.0/odoo/http.py”,第515行       响应= f(* args,** kw)     文件“ /home/priya/repo/rp-group/rpg_quotation/controllers/web_page.py”,行1442,在update_quotation中       res = self.print_quotation_software_report(data,int(quotation_id))     文件“ /home/priya/repo/rp-group/rpg_quotation/controllers/web_page.py”,行2699,在print_quotation_software_report中       pdf = request.env.ref('rpg_quotation.rpg_quotation_software_setwise__report')。report_action(self,data = data,config = False)     在report_action中的文件“ /home/priya/workspace/ODOO11/odoo-11.0/odoo/addons/base/ir/ir_actions_report.py”,第703行       context = dict(self.env.context,active_ids = active_ids)

UnboundLocalError: local variable 'active_ids' referenced before assignment

我的js代码:

$(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) {
 });

});

我的Python代码:

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)

在这里

rpg_quotation是模块名称,rpg_quotation_software_setwise__report是报告ID。

2 个答案:

答案 0 :(得分:1)

您可以尝试

通过将href设置为这样的按钮来打印报告,

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

在i.id中,我具有发票的ID。 这是格式 report / type_of_the_report / module_name.template_name / id

要从控制器打印报告,

@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('/')

答案 1 :(得分:0)

也许是因为您将self作为report_action方法调用而不是quotation_id的docids位置参数的值来传递,以消除您的实际错误。

但是该report_action方法调用将不会返回pdf文件数据。您需要将其更改为:

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

查看示例:

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