Skip to Content
Menu
This question has been flagged
11 Replies
17736 Views

How to change name of resultant file name in odoo webkit report?

 

for example : now is printing --> sale.report_saleorder.pdf 

but i want in this way --> Sale Order.pdf

Thank In advance

 

 

Avatar
Discard
Best Answer

This is a complete code that change behaviour to force your own file name without breaking compatibility with other reports and without changing Odoo core. Check you edit value for 'nameofreporttochange' with name of report (you will find it into console because it is output into log).

Just put this into a file   myfile.py and declare this file into manifest of your module.

from openerp.addons.web.http import Controller, route, request
from openerp.addons.web.controllers.main import _serialize_exception
from openerp.osv import osv
from openerp.addons.report.controllers.main import ReportController
from openerp import http
import simplejson
import logging


# Surcharge la fonction report_download afin de pouvoir modifier le
# content assist pour forcer la valeur du 'Content-Disposition' et forcer le nom du fichier telecharge.
class SponsorReportController(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 == 'nameofreporttochange':
                filename = 'My new filename'
                if docids.isnumeric():
                    partner_obj = http.request.env['res.partner']
                    object = partner_obj.browse(int(docids))
                    filename = filename + " - " + object.name
            
            response.headers.set('Content-Disposition', 'attachment; filename=%s.pdf;' % filename)
            
        return response

Avatar
Discard
Best Answer

Here is our solution to include the sale order name (SO000012, SO000013, etc.) in the file name of the sale orders reports pdf via « Print » buttons.

Content of control_pt.py :

import pdb
from openerp.addons.web.http import Controller, route, request
from openerp.addons.report.controllers.main import ReportController
from openerp.osv import osv
from openerp import http
import simplejson

class PTReportController(ReportController):
    @route(['/report/download'], type='http', auth="user")
    def report_download(self, data, token):
        order_obj = http.request.env['sale.order']
        requestcontent = simplejson.loads(data)
        url, type = requestcontent[0], requestcontent[1]
# url = u'/report/pdf/sale.report_saleorder/37'
# type = u'qweb-pdf'
        assert type == 'qweb-pdf'
        reportname = url.split('/report/pdf/')[1].split('?')[0]
# reportname = u'sale.report_saleorder/37'
        reportname, docids = reportname.split('/')
# reportname = u'sale.report_saleorder'
# docids = 37
        assert docids
        object = order_obj.browse(int(docids))

        name = object.name
# name = 2014-DE000020
        filename = 'PT_' + object.name
# filename = PT_2014-DE000020

        response = ReportController().report_download(data, token)
        response.headers.set('Content-Disposition', 'attachment; filename=%s.pdf;' % filename)
        return response

Avatar
Discard
Best Answer

Hi Nikunj,

This code prints the report with the dynamic pdf names without affecting other reports of the same module or base :

from odoo.addons.report.controllers.main import ReportController 
from odoo import http import simplejson import logging class PrintReport(ReportController):     @http.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 = super(PrintReport,self).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 == 'module_name.report_template_name':                 filename = 'module_template.report_template_name'                 if docids.isdigit():                     obj = http.request.env['model.name']                     object = obj.browse(int(docids))                     filename = object.field_name + " " + object.field_name                 else:                     filename = 'File_name'        ### will print multiple reports with the respective name given                 response.headers.set('Content-Disposition', 'attachment; filename=%s.pdf;' % filename)         return response

Hope this will help you.

Thanks.

Avatar
Discard
Best Answer


you just have to overwrite controller method. report_download()

Avatar
Discard
Best Answer

You can check out my solution at stackoverflow http://stackoverflow.com/questions/30485464/how-to-set-pdf-name-in-qweb-report-odoo/32152798#32152798

Avatar
Discard

Yes, sorry, fixed the link.

Best Answer

Yes... but it is sometype of hacking.... why odoo not give that because... in python if we take unicodecharacter in file name it gives parsing error...  if you want to try that fix name like "čćžšđ" and it give error... and odoo is support multi language... this is crotian character... if some one put this type of name it become problem.. so odoo not give that..

Avatar
Discard
Best Answer

Nikunj,

The tecnical workaround is big and needs customization.

For quicker solution, go to odoo/addons/report/controllers/main.py line 123, change reortname!

Thanks.

Avatar
Discard

It is how i'm trying to do now. But how to get the report_filename there?

Best Answer

An inbetween solution can be to override the ReportController for the /report/download URL.

This is what I did:

from openerp.addons.web.http import Controller, route, request
from openerp.addons.web.controllers.main import _serialize_exception
from openerp.osv import osv
from openerp.addons.report.controllers.main import ReportController
from openerp import http
import simplejson


class SponsorReportController(ReportController):

    @route(['/report/download'], type='http', auth="user")
    def report_download(self, data, token):
        partner_obj = http.request.env['res.partner']
        requestcontent = simplejson.loads(data)
        url, type = requestcontent[0], requestcontent[1]
        assert type == 'qweb-pdf'
        reportname = url.split('/report/pdf/')[1].split('?')[0]

        reportname, docids = reportname.split('/')
        assert docids
        object = partner_obj.browse(int(docids))
        response = ReportController().report_download(data, token)
        filename = object.report_filename

        response.headers.set('Content-Disposition', 'attachment; filename=%s.pdf;' % filename)
        return response

As you can see I have copied some of the code from odoo/addons/report/controllers/main.py to get the ID of the document, then I invoke the report_filename on the object and overwrite the Content-Disposition header in the response. This is a little cleaner than messing with the core code. Hope this helps you.

 

Avatar
Discard
Related Posts Replies Views Activity
4
Nov 24
5238
1
Mar 24
399
3
Sep 23
22661
0
Feb 24
1718
1
Oct 22
2795