Skip to Content
Menu
This question has been flagged
2 Replies
12635 Views

Hello

I want to generate report through API.

E.g.

I I create API like /api/generate_pdf with type="htpp" and method="GET".after accessing this API able to generate report.

I know that how report is created in odoo but I don't have idea from API(controller) report is generated.


Avatar
Discard
Best Answer

Hi,

Please see the answer given for this question: https://www.odoo.com/forum/help-1/question/how-to-print-report-from-website-121485

@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('/')
In Latest version get_pdf is changed into render_qweb_pdf. See this sample from odoo13.

@http.route(['/shop/print'], type='http', auth="public", website=True, sitemap=False)
def print_saleorder(self, **kwargs):
sale_order_id = request.session.get('sale_last_order_id')
if sale_order_id:
pdf, _ = request.env.ref('sale.action_report_saleorder').sudo().render_qweb_pdf([sale_order_id])
pdfhttpheaders = [('Content-Type', 'application/pdf'), ('Content-Length', u'%s' % len(pdf))]
return request.make_response(pdf, headers=pdfhttpheaders)

else:
return request.redirect('/shop')
Thanks 
Avatar
Discard
Author

Thanks it work but If I want to create report from external application(written in other language) then is it possible to generate report from odoo v12.

Best Answer

Api to get pdf file preview:

@http.route(['/schedule_pdf'], type='http', auth="public", website=True, sitemap=False)
def schedule_pdf(self, **kw):         
​if kw.get('id'):      
​as_model = request.env['alwathba.schedule'].sudo().browse([int(kw.get('id'))])
​         content = base64.b64decode(str(as_model.pdf_file.decode("utf-8")))     
​pdfhttpheaders = [('Content-Type', 'application/pdf'),('Content-Length', u'%s' % len(content))]      
​return request.make_response(content, headers=pdfhttpheaders)
 



to access pdf: https://yourcompany.com/schedule_pdf?id=14

Avatar
Discard
Related Posts Replies Views Activity
3
Dec 24
2647
2
Jun 23
3007
0
Mar 22
1459
0
Aug 21
4155
0
Apr 18
7473