Merge pull request #3746 from FinnStutzenstein/pdfImageErrors

Throw errors, if the pds generation fails, if an image could not be l…
This commit is contained in:
Emanuel Schütze 2018-05-16 17:24:52 +02:00 committed by GitHub
commit 8d7bcedd32
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 70 additions and 31 deletions

View File

@ -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');
});
},
};

View File

@ -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');
});
},
};

View File

@ -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: '<i class="fa fa-exclamation-triangle fa-lg spacer-right"></i>' +
gettextCatalog.getString('Error while generating PDF file') +
': <code>' + gettextCatalog.getString('Cannot load image') + ' ' + url + '</code>',
});
};
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);
});
}
};

View File

@ -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(
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"></i>' + gettextCatalog.getString('Error while generating ZIP file') +
': <code>' + error + '</code>', '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');
});
}
},

View File

@ -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');
}
);
}