如何更改打印的 qweb 文件名

How to change the printed qweb filename

如何更改打印的qweb文件名取决于源文档字段名? 我在模型中 stock.picking

因此,当我单击打印 -> 交货单时,将打印 qweb,但文件名将取决于源文档字段。 在这里我展示了解释我的意思的图片。

EXAMPLE

尝试在您的报告中添加附件,将报告文件名更改为您想要的名称。

<report
            string="Delivery Slip"
            id="action_report_delivery"
            model="stock.picking"
            report_type="qweb-pdf"
            name="stock.report_deliveryslip"
            file="stock.report_deliveryslip"
            attachment="'Custom Text...'+'.pdf')"
        />

您可以使用配置给动态报​​告名称,但它会在您打印一份报告时应用。

下面是在报告中打印自定义名称的示例。ir.actions.report.xml中创建一个字段,其中用户可以配置报表名称.

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

    download_filename = fields.Char(
        'Download filename')

现在您需要创建两个文件。

  1. 报表控制器

    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 社区为我们提供了报告自定义名称的默认模块。您可以直接安装此模块并设置报告名称,如:${o.name}

这里o表示你的记录。

下面是一个link的odoo社区模块。

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

这可能对你有帮助。

Here is my code which works for different reportname in the same model. So the filename only changes for the certain filename.

> 
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

class SponsorReportController(ReportController):

    @route(['/report/download'], type='http', auth="user")
    def report_download(self, data, token):
        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 == 'pci_stock_picking_report.report_delivery_note':
                partner_obj = http.request.env['stock.picking']
                object = partner_obj.browse(int(docids))
                filename = "No."+(object.origin)
                response.headers.set('Content-Disposition', 'attachment; filename=%s.pdf;' % filename)

        return response