Skip to content

Commit fff7379

Browse files
[ADD] report_pdf_zip_download
1 parent e685488 commit fff7379

File tree

14 files changed

+218
-0
lines changed

14 files changed

+218
-0
lines changed

report_pdf_zip_download/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
from . import controllers
2+
from . import models
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# © 2017 Creu Blanca
2+
# Copyright 2024 Quartile (https://www.quartile.co)
3+
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
4+
{
5+
"name": "Report PDF ZIP Download",
6+
"category": "Report",
7+
"version": "12.0.1.0.0",
8+
"author": "Quartile, Odoo Community Association (OCA)",
9+
"website": "https://github.com/OCA/reporting-engine",
10+
"license": "AGPL-3",
11+
"depends": ["base"],
12+
"data": [
13+
"views/ir_actions_report_views.xml",
14+
"views/web_client_templates.xml",
15+
],
16+
"installable": True,
17+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from . import main
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# Copyright 2024 Quartile (https://www.quartile.co)
2+
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
3+
4+
import zipfile
5+
from odoo.http import content_disposition
6+
from odoo import http
7+
from odoo.http import request
8+
from odoo.addons.web.controllers.main import ReportController
9+
from io import BytesIO
10+
from datetime import datetime
11+
import json
12+
from odoo.tools.safe_eval import safe_eval
13+
import time
14+
15+
16+
class ExtendedReportController(ReportController):
17+
18+
@http.route()
19+
def report_routes(self, reportname, docids=None, converter=None, **data):
20+
report = request.env['ir.actions.report']._get_report_from_name(reportname)
21+
report_name = report.report_file
22+
doc_ids = []
23+
if converter == "zip":
24+
if docids:
25+
doc_ids = [int(i) for i in docids.split(',')]
26+
context = dict(request.env.context)
27+
if data.get('options'):
28+
data.update(json.loads(data.pop('options')))
29+
if data.get('context'):
30+
data['context'] = json.loads(data['context'])
31+
if data['context'].get('lang'):
32+
del data['context']['lang']
33+
context.update(data['context'])
34+
attachments = []
35+
for doc_id in doc_ids:
36+
pdf_content, _ = report.with_context(context).render_qweb_pdf(
37+
[doc_id], data=data
38+
)
39+
if report.print_report_name:
40+
obj = request.env[report.model].browse(doc_id)
41+
report_name = safe_eval(report.print_report_name,
42+
{'object': obj, 'time': time})
43+
report_name = report_name.replace('/', '_')
44+
pdf_name = f'{report_name}.pdf'
45+
attachments.append((pdf_name, pdf_content))
46+
# Generate the ZIP file
47+
zip_filename = f"{datetime.now().strftime('%Y%m%d_%H%M%S')}.zip"
48+
bitIO = BytesIO()
49+
with zipfile.ZipFile(bitIO, "w", zipfile.ZIP_DEFLATED) as zip_file:
50+
for pdf_name, pdf_content in attachments:
51+
zip_file.writestr(pdf_name, pdf_content)
52+
zip_content = bitIO.getvalue()
53+
headers = [
54+
('Content-Type', 'application/zip'),
55+
('Content-Disposition', content_disposition(zip_filename))
56+
]
57+
return request.make_response(
58+
zip_content,
59+
headers=headers
60+
)
61+
return super(ExtendedReportController, self).report_routes(
62+
reportname, docids, converter, **data
63+
)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from . import ir_actions_report
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Copyright 2024 Quartile (https://www.quartile.co)
2+
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
3+
4+
from odoo import models, fields
5+
6+
7+
class IrActionsReport(models.Model):
8+
_inherit = "ir.actions.report"
9+
10+
zip_download = fields.Boolean(
11+
help="If enabled, the report will be downloaded as a zip "
12+
"file for multiple records."
13+
)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
To enable the ZIP file feature, please check the 'Zip Download' option under the 'Advanced Properties' tab for the specific report.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
* `Quartile <https://www.quartile.co>`__:
2+
3+
* Aung Ko Ko Lin
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This module generates one PDF report per record, and the reports are compiled into a ZIP file if the user prints multiple records at once. If the user prints a single record, it will follow Odoo's standard behavior and will generate just a single PDF file.
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
// © 2017 Creu Blanca
2+
// Copyright 2024 Quartile (https://www.quartile.co)
3+
// License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
4+
odoo.define("batch_report_pdf_zip.report", function (require) {
5+
"use strict";
6+
7+
var core = require("web.core");
8+
var ActionManager = require("web.ActionManager");
9+
var crash_manager = require("web.crash_manager");
10+
var framework = require("web.framework");
11+
var session = require("web.session");
12+
var _t = core._t;
13+
14+
ActionManager.include({
15+
16+
_downloadReportZIP: function (url, actions) {
17+
framework.blockUI();
18+
var def = $.Deferred();
19+
var type = "zip";
20+
var cloned_action = _.clone(actions);
21+
22+
if (_.isUndefined(cloned_action.data) ||
23+
_.isNull(cloned_action.data) ||
24+
(_.isObject(cloned_action.data) && _.isEmpty(cloned_action.data)))
25+
{
26+
if (cloned_action.context.active_ids) {
27+
url += "/" + cloned_action.context.active_ids.join(',');
28+
}
29+
} else {
30+
url += "?options=" + encodeURIComponent(JSON.stringify(cloned_action.data));
31+
url += "&context=" + encodeURIComponent(JSON.stringify(cloned_action.context));
32+
}
33+
34+
var blocked = !session.get_file({
35+
url: url,
36+
data: {
37+
data: JSON.stringify([url, type]),
38+
},
39+
success: def.resolve.bind(def),
40+
error: function () {
41+
crash_manager.rpc_error.apply(crash_manager, arguments);
42+
def.reject();
43+
},
44+
complete: framework.unblockUI,
45+
});
46+
if (blocked) {
47+
var message = _t('A popup window with your report was blocked. You ' +
48+
'may need to change your browser settings to allow ' +
49+
'popup windows for this page.');
50+
this.do_warn(_t('Warning'), message, true);
51+
}
52+
return def;
53+
},
54+
55+
_triggerDownload: function (action, options, type) {
56+
var self = this;
57+
var reportUrls = this._makeReportUrls(action);
58+
if (type === "zip") {
59+
return this._downloadReportZIP(reportUrls[type], action).then(function () {
60+
if (action.close_on_report_download) {
61+
var closeAction = {type: 'ir.actions.act_window_close'};
62+
return self.doAction(closeAction, _.pick(options, 'on_close'));
63+
} else {
64+
return options.on_close();
65+
}
66+
});
67+
}
68+
return this._super.apply(this, arguments);
69+
},
70+
71+
_makeReportUrls: function (action) {
72+
var reportUrls = this._super.apply(this, arguments);
73+
reportUrls.zip = '/report/zip/' + action.report_name;
74+
return reportUrls;
75+
},
76+
77+
_executeReportAction: function (action, options) {
78+
var self = this;
79+
if (action.context.active_ids && action.context.active_ids.length > 1) {
80+
if (action.report_type === 'qweb-pdf' && action.zip_download === true) {
81+
return self._triggerDownload(action, options, 'zip');
82+
}
83+
}
84+
return this._super.apply(this, arguments);
85+
}
86+
});
87+
88+
});

0 commit comments

Comments
 (0)