From 45beea3a9e20daf546007e821d6ed95cf013c947 Mon Sep 17 00:00:00 2001 From: Sebastian Gutzeit Date: Wed, 18 Oct 2023 12:34:59 +0200 Subject: [PATCH] App Toolbox: PDF-Download verbessert --- .../static/datenmanagement/js/form.js | 2 +- .../static/datenmanagement/js/list.js | 34 ++++++++++++++++--- toolbox/views.py | 6 ++-- 3 files changed, 34 insertions(+), 8 deletions(-) diff --git a/datenmanagement/static/datenmanagement/js/form.js b/datenmanagement/static/datenmanagement/js/form.js index 849e7a5e..f83be2c0 100644 --- a/datenmanagement/static/datenmanagement/js/form.js +++ b/datenmanagement/static/datenmanagement/js/form.js @@ -33,7 +33,7 @@ function addDeleteFieldButton(field) { * * inserts the passed single field into an array field complex */ -function addField(field, fieldToInsertAfter, buttonsPosition= false) { +function addField(field, fieldToInsertAfter, buttonsPosition = false) { // create wrapper let wrapper = $('
', { class: 'input-group', style: 'margin-top:0.5rem' }); // insert passed single field into created wrapper diff --git a/datenmanagement/static/datenmanagement/js/list.js b/datenmanagement/static/datenmanagement/js/list.js index e92b47b6..4cd735c2 100644 --- a/datenmanagement/static/datenmanagement/js/list.js +++ b/datenmanagement/static/datenmanagement/js/list.js @@ -1,3 +1,25 @@ +/** + * @function + * @name downloadFile + * + * download a file + * + * @param {Blob} file - file + * @param {string} [fileName='file'] - file name + */ +function downloadFile(file, fileName = 'file') { + let href = URL.createObjectURL(file); + let a = Object.assign(document.createElement('a'), { + href, + style: 'display:none', + download: fileName + }); + document.body.appendChild(a); + a.click(); + URL.revokeObjectURL(href); + a.remove(); +} + /** * @function * @name formatData @@ -32,7 +54,8 @@ function formatData(data, brReplacement) { * @param {string} host - host */ function fetchPdf(url, csrfToken, host){ - const response = fetch( + let fileName = ''; + fetch( url, { method: 'POST', headers: { @@ -44,9 +67,12 @@ function fetchPdf(url, csrfToken, host){ referrerPolicy: 'no-referrer', body: JSON.stringify(window.renderParams) } - ); - response.then(response => response.blob()) - .then(myblob => window.open(URL.createObjectURL(myblob))); + ) + .then(response => { + fileName = response.headers.get('Content-Disposition').split('filename=')[1]; + return response.blob(); + }) + .then(file => downloadFile(file, fileName)); } /** diff --git a/toolbox/views.py b/toolbox/views.py index 4db2e15b..ef9f98a6 100644 --- a/toolbox/views.py +++ b/toolbox/views.py @@ -254,7 +254,6 @@ def renderpdf(request): params['datenthema'].lower() == 'baudenkmale' and params['suitable'].template.name == 'Denkmalliste' ): - print('sepp') data = baudenkmalefull(params['pks'], onlyactive=True) else: data = dict() @@ -266,13 +265,14 @@ def renderpdf(request): data, params['suitable'].template.templatefile, pdfdir=joinpath('toolbox', 'mkpdf')) + filename = params['suitable'].template.name.lower() if rendersuccess: ret = FileResponse(responsefile, status=200) - ret['Content-Disposition'] = f'attachment, filename={params["datenthema"]}' + ret['Content-Disposition'] = f'attachment; filename={filename}.pdf' ret['Content-Type'] = 'application/pdf' else: ret = FileResponse(responsefile, status=409) ret.reason_phrase = 'keine PDF-Datei erzeugt, wahrscheinlich ist das Template defekt' - ret['Content-Disposition'] = f'attachment, filename={params["datenthema"]}.errorlog' + ret['Content-Disposition'] = f'attachment; filename={filename}.log' ret['Content-Type'] = 'text/plain' return ret