Přejít na obsah
Menu
You need to be registered to interact with the community.
This question has been flagged
1 Odpovědět
2742 Zobrazení

Hello,


I added a menu item in the action menu of the mrp.production form.

When i click on this item, i call a python method and in this method, i do differents things in particular, i want to print the report exactly if i click on the menu "Production order" in the print menu of the form.

I tried with this line : 

return self.env.ref('mrp.action_report_production_order').report_action(self)

But it doesn't work maybe because i could not find any report action for the report action_report_production_order !?

Maybe i made a mistake or there an other way to print this report from a python method ?

Avatar
Zrušit
Nejlepší odpověď

Hi Vincent,

Yes, you can print a report without using ir.actions.report by using the report method of the report object. Here's an example of how to do it in your case:

pythonCopy codereport = self.env['ir.actions.report']._get_report_from_name('mrp.report_production_order')
pdf = report.render_qweb_pdf(self.ids)
pdfhttpheaders = [('Content-Type', 'application/pdf'), ('Content-Length', len(pdf))]
return request.make_response(pdf, headers=pdfhttpheaders)

In this code, mrp.report_production_order is the technical name of the report you want to print. You can find this name in the "Technical Settings" tab of the report form view.

The render_qweb_pdf method renders the report to a PDF file. self.ids is a list of the record IDs of the current mrp.production object.

Finally, request.make_response creates an HTTP response with the PDF file as the body and sets the appropriate headers to make it display as a PDF in the browser.

You can customize the headers as needed, for example, to set a filename for the PDF.

Regards,

Ksolves Team!

Avatar
Zrušit
Autor

Hi Ksolves Team,
Thanks for your answer.
I've tried it but i've got an error

First, i changed the report name from "mrp.report_production_order" to "mrp.report_mrporder"
and then i added at the beginning of the python file :
"from odoo.http import request

Here is the final code :
def print_of(self):
report = self.env['ir.actions.report']._get_report_from_name('mrp.report_mrporder')
pdf = report.render_qweb_pdf(self.ids)
pdfhttpheaders = [('Content-Type', 'application/pdf'), ('Content-Length', len(pdf))]
return request.make_response(pdf, headers=pdfhttpheaders)

Then, when i try to execute, i've got the following error

Odoo Server Error
Traceback (most recent call last):
File "/home/odoo/src/odoo/odoo/tools/safe_eval.py", line 360, in safe_eval
return unsafe_eval(c, globals_dict, locals_dict)
File "", line 3, in <module>
File "/home/odoo/src/user/safar_module/models/mrp_production.py", line 96, in print_document
self.print_of()
File "/home/odoo/src/user/safar_module/models/mrp_production.py", line 106, in print_of
return request.make_response(pdf, headers=pdfhttpheaders)
File "/usr/local/lib/python3.6/dist-packages/werkzeug/local.py", line 347, in __getattr__
return getattr(self._get_current_object(), name)
AttributeError: 'JsonRequest' object has no attribute 'make_response'