How to set PDF name in qWeb report, Odoo?

2019-02-13 12:54发布

I'm making reports using qWeb in Odoo 8. Those generated PDF files are saved with a "default" name. I would like to set a specific name to every generated file (not after file was saved, but in "generation" time).

Is that possible? If it is, how to do it?

Thanks in advance.

8条回答
smile是对你的礼貌
2楼-- · 2019-02-13 13:44

addons\report\controllers\main.py

Line : 127

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

change it with

invoicename="ma_facture" #create a variable you can add date for example stfftime("%d-%m-%Y")
response.headers.add('Content-Disposition', 'attachment; filename=%s.pdf;' % invoicename)
查看更多
时光不老,我们不散
3楼-- · 2019-02-13 13:44

You can give dynamic report name using configuration, but it will apply when you print one report.

Below is the example to Print custom name in report.Create one field in ir.actions.report.xml, in which user can configure report name.

from openerp import models, fields
class IrActionsReportXml(models.Model):
    _inherit = 'ir.actions.report.xml'

    download_filename = fields.Char(
        'Download filename')

Now you need to create two files.

  1. Report Controller

    from openerp import http
    from openerp.addons.mail.models import mail_template
    from openerp.addons.report.controllers.main import ReportController
    from openerp.addons.web.controllers.main import content_disposition
    
    
    class ReportController(ReportController):
        @http.route([
            '/report/<path:converter>/<reportname>',
            '/report/<path:converter>/<reportname>/<docids>',
        ])
        def report_routes(self, reportname, docids=None, converter=None, **data):
            response = super(ReportController, self).report_routes(
                reportname, docids=docids, converter=converter, **data)
            if docids:
                docids = [int(i) for i in docids.split(',')]
            report_xml = http.request.session.model('ir.actions.report.xml')
            report_ids = report_xml.search(
                [('report_name', '=', reportname)])
            for report in report_xml.browse(report_ids):
                if not report.download_filename:
                    continue
                objects = http.request.session.model(report.model)\
                    .browse(docids or [])
                generated_filename = mail_template.mako_template_env\
                    .from_string(report.download_filename)\
                    .render({
                        'objects': objects,
                        'o': objects[:1],
                        'object': objects[:1],
                        'ext': report.report_type.replace('qweb-', ''),
                    })
                response.headers['Content-Disposition'] = content_disposition(
                    generated_filename)
            return response
    
        @http.route(['/report/download'])
        def report_download(self, data, token):
            response = super(ReportController, self).report_download(data, token)
            # if we got another content disposition before, ditch the one added
            # by super()
            last_index = None
            for i in range(len(response.headers) - 1, -1, -1):
                if response.headers[i][0] == 'Content-Disposition':
                    if last_index:
                        response.headers.pop(last_index)
                    last_index = i
            return response
    

2.Report.py

    import json
    from openerp import http
    from openerp.addons.web.controllers import main
    from openerp.addons.mail.models import mail_template


    class Reports(main.Reports):
        @http.route('/web/report', type='http', auth="user")
        @main.serialize_exception
        def index(self, action, token):
            result = super(Reports, self).index(action, token)
            action = json.loads(action)
            context = dict(http.request.context)
            context.update(action["context"])
            report_xml = http.request.env['ir.actions.report.xml']
            reports = report_xml.search([
                ('report_name', '=', action['report_name']),
                ('download_filename', '!=', False)])
            for report in reports:
                objects = http.request.session.model(context['active_model'])\
                    .browse(context['active_ids'])
                generated_filename = mail_template.mako_template_env\
                    .from_string(report.download_filename)\
                    .render({
                        'objects': objects,
                        'o': objects[0],
                        'object': objects[0],
                    })
                result.headers['Content-Disposition'] = main.content_disposition(
                    generated_filename)
            return result

Odoo community Providing us a default module for report custom name. you can directly install this module and set report name like : ${o.name}

Here o means your record.

Below is a link of odoo community module for V8 and V9.

https://www.odoo.com/apps/modules/9.0/report_custom_filename/

https://www.odoo.com/apps/modules/8.0/report_custom_filename/ This may help you.

查看更多
登录 后发表回答