Skip to content

Commit

Permalink
App Toolbox: PDF-Download verbessert
Browse files Browse the repository at this point in the history
  • Loading branch information
gdmhrogut committed Oct 18, 2023
1 parent 9811c60 commit 45beea3
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 8 deletions.
2 changes: 1 addition & 1 deletion datenmanagement/static/datenmanagement/js/form.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = $('<div/>', { class: 'input-group', style: 'margin-top:0.5rem' });
// insert passed single field into created wrapper
Expand Down
34 changes: 30 additions & 4 deletions datenmanagement/static/datenmanagement/js/list.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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: {
Expand All @@ -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));
}

/**
Expand Down
6 changes: 3 additions & 3 deletions toolbox/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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

0 comments on commit 45beea3

Please sign in to comment.