Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

0

change printed file name in webkit report in odoo?

By
Nikunj Nakum
on 8/12/14, 2:51 AM 5,782 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

 

 

1
Laurent Destailleur
On 1/8/15, 5:24 AM

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

1

Pascal Tremblay

--Pascal Tremblay--
1312
| 5 2 7
Alma, Canada
--Pascal Tremblay--


Pascal Tremblay
On 12/27/14, 10:20 AM

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

0
Jeroen Vet
On 8/23/15, 4:26 AM

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

wrong link?

Fabian Tribrunner
on 8/23/15, 1:49 PM

Yes, sorry, fixed the link.

Shanghai eXcec Business Consulting, Jeroen Vet
on 8/24/15, 9:46 PM
0
Balvant
On 7/18/15, 3:36 AM

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..

0

Serpent Consulting Services Pvt. Ltd.

--Serpent Consulting Services Pvt. Ltd.--
4341
| 6 6 8
Gandhinagar, India
--Serpent Consulting Services Pvt. Ltd.--

Serpent Consulting Services Pvt. Ltd. Your Odoo/OpenERP Solution, just an email away!

Serpent Consulting Services Pvt. Ltd.
On 8/12/14, 11:27 AM

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.

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

Pascal Tremblay
on 12/26/14, 2:56 PM
0

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.

 

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!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

5 follower(s)

Stats

Asked: 8/12/14, 2:51 AM
Seen: 5782 times
Last updated: 8/24/15, 9:45 PM