Odoo - OpenERP - How to Insert fields into PDF name when downloading & saving ?

Support
Support
5000
| 2 1 2
Asked on 8/15/15, 7:32 AM
0
vote
4025 Views

All reports are composed with the help of HTML/Qweb by using Report Template in Odoo, . The PDF conversion itself is taken care by wkhtmltopdf.If user need to create a report for a particular model, user will have to define this report and the report template it is going to use. If user wish, he/she can also declare a specific paper format for this report. Lastly, if user requires access to more than one model, he/she can define a custom reports class that will give user access to more classes and records in the template.

 

In Odoo, mostly reports while getting downloaded in PDF format are shown with combination of module_name and report_id defined in the report template. Once we had a requirement where we were asked to alter this behaviour. We wanted to have a specific name to every PDF getting downloaded. Lets assume,we are printing a Sale Order then it should be downloaded as Order-Sale Order Number i.e. Order-SO001.We had to build a function in order to meet this requirement. We added below code snippet in the function.

Code Snippet:

class NewReportController(ReportController):

@route([‘/report/download’], type=’http’, auth=”user”)
def report_download(self, data, token):
logging.info(“report_download”)
requestcontent = simplejson.loads(data)
url, type = requestcontent[0], requestcontent[1]
logging.info(url)
logging.info(type)
response = ReportController().report_download(data, token)
if len(url.split(‘/report/pdf/’)) > 1 and type == ‘qweb-pdf':
reportname = url.split(‘/report/pdf/’)[1].split(‘?’)[0]
reportname, docids = reportname.split(‘/’)
logging.info(reportname)
assert docids
logging.info(docids)
if reportname == ‘sale.report_saleorder':
filename = ‘Order’
partner_obj = http.request.env[‘sale.order’]
object = partner_obj.browse(int(docids))
filename = filename + “-” + object.name
response.headers.set(‘Content-Disposition’, ‘attachment; filename=%s.pdf;’ % filename)
return response

Example:

if reportname == ‘sale.report_saleorder':
filename = ‘Order’
partner_obj = http.request.env[‘sale.order’]
object = partner_obj.browse(int(docids))
filename = filename + “-” + object.name
response.headers.set(‘Content-Disposition’, ‘attachment; filename=%s.pdf;’ % filename)

Now save your file and restart openerp server. Try to print the report for the model and report_id mentioned in the above code. It will label PDF as syntax defined in the above code snippet. You can repeat this procedure for every module for which you want to alter the existing behaviour.

        

                  Before                                                         After

Support
Support
5000
| 2 1 2
Answered on 8/15/15, 7:34 AM
0
vote

Your answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

Ask a Question

Keep Informed

About This Forum

This forum is for HiTechnologia Employees & just Odoo general knowledge purpose only.

Read Guidelines

Question tools

3 follower(s)

Stats

Asked: 8/15/15, 7:32 AM
Seen: 4025 times
Last updated: 8/15/15, 7:34 AM