From ecb76fd888cfe57241bbd7d8e7dc16b3c0185aed Mon Sep 17 00:00:00 2001 From: FinnStutzenstein Date: Wed, 16 May 2018 13:49:45 +0200 Subject: [PATCH] Throw errors if pdf generation fails, if an image could not be loaded (fixes #3730) --- openslides/agenda/static/js/agenda/pdf.js | 5 +- .../assignments/static/js/assignments/pdf.js | 17 ++++--- openslides/core/static/js/core/pdf.js | 24 ++++++---- openslides/motions/static/js/motions/pdf.js | 47 +++++++++++++------ openslides/users/static/js/users/pdf.js | 8 +++- 5 files changed, 70 insertions(+), 31 deletions(-) diff --git a/openslides/agenda/static/js/agenda/pdf.js b/openslides/agenda/static/js/agenda/pdf.js index 4233c3e80..2f435b8db 100644 --- a/openslides/agenda/static/js/agenda/pdf.js +++ b/openslides/agenda/static/js/agenda/pdf.js @@ -82,13 +82,16 @@ angular.module('OpenSlidesApp.agenda.pdf', ['OpenSlidesApp.core.pdf']) 'AgendaContentProvider', 'PdfMakeDocumentProvider', 'PdfCreate', - function (gettextCatalog, AgendaContentProvider, PdfMakeDocumentProvider, PdfCreate) { + 'Messaging', + function (gettextCatalog, AgendaContentProvider, PdfMakeDocumentProvider, PdfCreate, Messaging) { return { export: function (items) { var filename = gettextCatalog.getString('Agenda') + '.pdf'; var agendaContentProvider = AgendaContentProvider.createInstance(items); PdfMakeDocumentProvider.createInstance(agendaContentProvider).then(function (documentProvider) { PdfCreate.download(documentProvider, filename); + }, function (error) { + Messaging.addMessage(error.msg, 'error'); }); }, }; diff --git a/openslides/assignments/static/js/assignments/pdf.js b/openslides/assignments/static/js/assignments/pdf.js index 8768e2dfd..feeb31c99 100644 --- a/openslides/assignments/static/js/assignments/pdf.js +++ b/openslides/assignments/static/js/assignments/pdf.js @@ -514,7 +514,7 @@ angular.module('OpenSlidesApp.assignments.pdf', ['OpenSlidesApp.core.pdf']) return imageMap; }; - return $q(function (resolve) { + return $q(function (resolve, reject) { var imageSources = [ logoBallotPaperUrl, ]; @@ -524,7 +524,7 @@ angular.module('OpenSlidesApp.assignments.pdf', ['OpenSlidesApp.core.pdf']) getContent: getContent, getImageMap: getImageMap, }); - }); + }, reject); }); }; @@ -618,8 +618,10 @@ angular.module('OpenSlidesApp.assignments.pdf', ['OpenSlidesApp.core.pdf']) 'BallotContentProvider', 'PdfMakeBallotPaperProvider', 'PdfCreate', + 'Messaging', function (gettextCatalog, AssignmentContentProvider, AssignmentCatalogContentProvider, - PdfMakeDocumentProvider, BallotContentProvider, PdfMakeBallotPaperProvider, PdfCreate) { + PdfMakeDocumentProvider, BallotContentProvider, PdfMakeBallotPaperProvider, PdfCreate, + Messaging) { return { export: function (assignments, singleAssignment) { var filename = singleAssignment ? @@ -629,11 +631,10 @@ angular.module('OpenSlidesApp.assignments.pdf', ['OpenSlidesApp.core.pdf']) if (singleAssignment) { assignments = [assignments]; } - var assignmentContentProviderArray = []; // Convert the assignments to content providers - angular.forEach(assignments, function(assignment) { - assignmentContentProviderArray.push(AssignmentContentProvider.createInstance(assignment)); + var assignmentContentProviderArray = _.map(assignments, function (assignment) { + return AssignmentContentProvider.createInstance(assignment); }); var documentProviderPromise; @@ -648,6 +649,8 @@ angular.module('OpenSlidesApp.assignments.pdf', ['OpenSlidesApp.core.pdf']) } documentProviderPromise.then(function (documentProvider) { PdfCreate.download(documentProvider, filename); + }, function (error) { + Messaging.addMessage(error.msg, 'error'); }); }, createBallotPdf: function (assignment, pollId) { @@ -663,6 +666,8 @@ angular.module('OpenSlidesApp.assignments.pdf', ['OpenSlidesApp.core.pdf']) BallotContentProvider.createInstance(assignment, thePoll, pollNumber).then(function (ballotContentProvider) { var documentProvider = PdfMakeBallotPaperProvider.createInstance(ballotContentProvider); PdfCreate.download(documentProvider, filename); + }, function (error) { + Messaging.addMessage(error.msg, 'error'); }); }, }; diff --git a/openslides/core/static/js/core/pdf.js b/openslides/core/static/js/core/pdf.js index db73fbc25..48825a94a 100644 --- a/openslides/core/static/js/core/pdf.js +++ b/openslides/core/static/js/core/pdf.js @@ -8,7 +8,8 @@ angular.module('OpenSlidesApp.core.pdf', []) * General layout functions for building PDFs with pdfmake. */ .factory('PDFLayout', [ - function() { + 'gettextCatalog', + function(gettextCatalog) { var PDFLayout = {}; var BallotCircleDimensions = { yDistance: 6, @@ -89,10 +90,17 @@ angular.module('OpenSlidesApp.core.pdf', []) // returns a promise for converting an image in data URL format with size information PDFLayout.imageURLtoBase64 = function(url) { - var promise = new Promise(function(resolve) { + var promise = new Promise(function(resolve, reject) { var img = new Image(); img.crossOrigin = "Anonymous"; - img.onload = function() { + img.onerror = function () { + reject({ + msg: '' + + gettextCatalog.getString('Error while generating PDF file') + + ': ' + gettextCatalog.getString('Cannot load image') + ' ' + url + '', + }); + }; + img.onload = function () { var canvas = document.createElement("canvas"); canvas.width = img.width; canvas.height = img.height; @@ -348,7 +356,7 @@ angular.module('OpenSlidesApp.core.pdf', []) return imageMap; }; - return $q(function (resolve) { + return $q(function (resolve, reject) { var imageSources = [ logoHeaderUrl, logoFooterUrl @@ -363,11 +371,11 @@ angular.module('OpenSlidesApp.core.pdf', []) getDocument: getDocument, getImageMap: getImageMap, }); - }); + }, reject); }); }; return { - createInstance: createInstance + createInstance: createInstance, }; } ]) @@ -1114,11 +1122,11 @@ angular.module('OpenSlidesApp.core.pdf', []) } }); - return $q(function (resolve) { + return $q(function (resolve, reject) { //resolve promises to get base64 $q.all(imagePromises).then(function() { resolve(imageMap); - }); + }, reject); }); } }; diff --git a/openslides/motions/static/js/motions/pdf.js b/openslides/motions/static/js/motions/pdf.js index dd059274b..1b51fb797 100644 --- a/openslides/motions/static/js/motions/pdf.js +++ b/openslides/motions/static/js/motions/pdf.js @@ -450,7 +450,7 @@ angular.module('OpenSlidesApp.motions.pdf', ['OpenSlidesApp.core.pdf']) return imageMap; }; - return $q(function (resolve) { + return $q(function (resolve, reject) { ImageConverter.toBase64(getImageSources()).then(function (_imageMap) { imageMap = _imageMap; converter = PdfMakeConverter.createInstance(_imageMap); @@ -461,7 +461,7 @@ angular.module('OpenSlidesApp.motions.pdf', ['OpenSlidesApp.core.pdf']) getCategory: getCategory, getImageMap: getImageMap, }); - }); + }, reject); }); }; @@ -623,7 +623,7 @@ angular.module('OpenSlidesApp.motions.pdf', ['OpenSlidesApp.core.pdf']) return imageMap; }; - return $q(function (resolve) { + return $q(function (resolve, reject) { ImageConverter.toBase64(getImageSources()).then(function (_imageMap) { imageMap = _imageMap; converter = PdfMakeConverter.createInstance(_imageMap); @@ -631,7 +631,7 @@ angular.module('OpenSlidesApp.motions.pdf', ['OpenSlidesApp.core.pdf']) getContent: getContent, getImageMap: getImageMap, }); - }); + }, reject); }); }; @@ -786,7 +786,7 @@ angular.module('OpenSlidesApp.motions.pdf', ['OpenSlidesApp.core.pdf']) return imageMap; }; - return $q(function (resolve) { + return $q(function (resolve, reject) { var imageSources = [ logoBallotPaperUrl, ]; @@ -796,7 +796,7 @@ angular.module('OpenSlidesApp.motions.pdf', ['OpenSlidesApp.core.pdf']) getContent: getContent, getImageMap: getImageMap, }); - }); + }, reject); }); }; return { @@ -1014,14 +1014,17 @@ angular.module('OpenSlidesApp.motions.pdf', ['OpenSlidesApp.core.pdf']) var motionContentProviderArray = []; var motionContentProviderPromises = _.map(motions, function (motion) { var version = (singleMotion ? params.version : motion.active_version); - return MotionContentProvider.createInstance( - motion, version, params - ).then(function (contentProvider) { - motionContentProviderArray.push(contentProvider); + return $q(function (resolve, reject) { + MotionContentProvider.createInstance( + motion, version, params + ).then(function (contentProvider) { + motionContentProviderArray.push(contentProvider); + resolve(); + }, reject); }); }); - return $q(function (resolve) { + return $q(function (resolve, reject) { $q.all(motionContentProviderPromises).then(function() { var documentProviderPromise; if (singleMotion) { @@ -1032,8 +1035,8 @@ angular.module('OpenSlidesApp.motions.pdf', ['OpenSlidesApp.core.pdf']) } documentProviderPromise.then(function (documentProvider) { resolve(documentProvider); - }); - }); + }, reject); + }, reject); }); }, export: function (motions, params, singleMotion) { @@ -1044,6 +1047,8 @@ angular.module('OpenSlidesApp.motions.pdf', ['OpenSlidesApp.core.pdf']) this.getDocumentProvider(motions, params, singleMotion).then( function (documentProvider) { PdfCreate.download(documentProvider, params.filename); + }, function (error) { + Messaging.addMessage(error.msg, 'error'); } ); }, @@ -1073,12 +1078,12 @@ angular.module('OpenSlidesApp.motions.pdf', ['OpenSlidesApp.core.pdf']) usedFilenames.push(filename); filename += '.pdf'; - return $q(function (resolve) { + return $q(function (resolve, reject) { // get documentProvider for every motion. self.getDocumentProvider(motion, params, true).then(function (documentProvider) { docMap[filename] = documentProvider; resolve(); - }); + }, reject); }); }); $q.all(docPromises).then(function () { @@ -1097,6 +1102,8 @@ angular.module('OpenSlidesApp.motions.pdf', ['OpenSlidesApp.core.pdf']) 'spacer-right">' + gettextCatalog.getString('Error while generating ZIP file') + ': ' + error + '', 'error'); }); + }, function (error) { + Messaging.createOrEditMessage(messageId, error.msg, 'error'); }); }, createPollPdf: function (motion, version) { @@ -1106,6 +1113,8 @@ angular.module('OpenSlidesApp.motions.pdf', ['OpenSlidesApp.core.pdf']) PollContentProvider.createInstance(title, id).then(function (pollContentProvider) { var documentProvider = PdfMakeBallotPaperProvider.createInstance(pollContentProvider); PdfCreate.download(documentProvider, filename); + }, function (error) { + Messaging.addMessage(error.msg, 'error'); }); }, exportPersonalNote: function (motion, filename) { @@ -1117,7 +1126,11 @@ angular.module('OpenSlidesApp.motions.pdf', ['OpenSlidesApp.core.pdf']) MotionPartialContentProvider.createInstance(motion, content).then(function (contentProvider) { PdfMakeDocumentProvider.createInstance(contentProvider).then(function (documentProvider) { PdfCreate.download(documentProvider, filename); + }, function (error) { + Messaging.addMessage(error.msg, 'error'); }); + }, function (error) { + Messaging.addMessage(error.msg, 'error'); }); }, exportComment: function (motion, commentId, filename) { @@ -1134,7 +1147,11 @@ angular.module('OpenSlidesApp.motions.pdf', ['OpenSlidesApp.core.pdf']) MotionPartialContentProvider.createInstance(motion, content).then(function (contentProvider) { PdfMakeDocumentProvider.createInstance(contentProvider).then(function (documentProvider) { PdfCreate.download(documentProvider, filename); + }, function (error) { + Messaging.addMessage(error.msg, 'error'); }); + }, function (error) { + Messaging.addMessage(error.msg, 'error'); }); } }, diff --git a/openslides/users/static/js/users/pdf.js b/openslides/users/static/js/users/pdf.js index d1c77f618..820d15db3 100644 --- a/openslides/users/static/js/users/pdf.js +++ b/openslides/users/static/js/users/pdf.js @@ -294,7 +294,9 @@ angular.module('OpenSlidesApp.users.pdf', ['OpenSlidesApp.core.pdf']) 'UserAccessDataListContentProvider', 'PdfMakeDocumentProvider', 'PdfCreate', - function (gettextCatalog, UserListContentProvider, UserAccessDataListContentProvider, PdfMakeDocumentProvider, PdfCreate) { + 'Messaging', + function (gettextCatalog, UserListContentProvider, UserAccessDataListContentProvider, + PdfMakeDocumentProvider, PdfCreate, Messaging) { return { exportUserList: function (users) { var filename = gettextCatalog.getString('List of participants') + '.pdf'; @@ -302,6 +304,8 @@ angular.module('OpenSlidesApp.users.pdf', ['OpenSlidesApp.core.pdf']) PdfMakeDocumentProvider.createInstance(userListContentProvider).then( function (documentProvider) { PdfCreate.download(documentProvider, filename); + }, function (error) { + Messaging.addMessage(error.msg, 'error'); } ); }, @@ -313,6 +317,8 @@ angular.module('OpenSlidesApp.users.pdf', ['OpenSlidesApp.core.pdf']) PdfMakeDocumentProvider.createInstance(userAccessDataListContentProvider, true).then( function (documentProvider) { PdfCreate.download(documentProvider, filename, true); + }, function (error) { + Messaging.addMessage(error.msg, 'error'); } ); }