diff --git a/addons/report/controllers/main.py b/addons/report/controllers/main.py index 52a949c0bd2..19c266508c3 100644 --- a/addons/report/controllers/main.py +++ b/addons/report/controllers/main.py @@ -30,6 +30,7 @@ import logging import tempfile import lxml.html import subprocess +import simplejson from pyPdf import PdfFileWriter, PdfFileReader from werkzeug.test import Client @@ -209,7 +210,7 @@ class Report(http.Controller): """Resolve an internal webpage url and return its content with the help of werkzeug.test.client. - :param url: string representinf the url to resolve + :param url: string representing the url to resolve :param post: a dict representing the query string :returns: a tuple str(html), int(statuscode) """ @@ -427,3 +428,18 @@ class Report(http.Controller): content = merged.read() merged.close() return content + + @http.route('/report/downloadpdf/', type='http', auth="user") + def report_pdf_attachment(self, data, token): + """This function is only used by 'qwebactionmanager.js' in order to trigger the download of + a pdf report. + + :param data: The JSON.stringified report internal url + :returns: Response with a filetoken cookie and an attachment header + """ + url = simplejson.loads(data) + pdf = self._get_url_content(url) + response = self._make_pdf_response(pdf) + response.set_cookie('fileToken', token) + response.headers.add('Content-Disposition', 'attachment; filename=report.pdf;') + return response diff --git a/addons/report/static/src/js/qwebactionmanager.js b/addons/report/static/src/js/qwebactionmanager.js index ba71f029dcd..37f5489ab37 100644 --- a/addons/report/static/src/js/qwebactionmanager.js +++ b/addons/report/static/src/js/qwebactionmanager.js @@ -37,10 +37,22 @@ openerp.report = function(instance) { }); report_url += "?" + $.param(action.datas.form); } - - instance.web.unblockUI(); - window.open(report_url); - return; + if (action.report_type == 'qweb-html') { + // Open the html report in a popup + window.open(report_url); + instance.web.unblockUI(); + return; + } else { + // Trigger the download of the pdf report + var c = openerp.webclient.crashmanager; + this.session.get_file({ + url: '/report/downloadpdf', + data: {data: JSON.stringify(report_url)}, + complete: openerp.web.unblockUI, + error: c.rpc_error.bind(c) + }); + return; + } } else { return self._super(action, options); }