Insert fields into PDF name

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.

You must be logged in to post a comment.