Fixed placeholder replacement in pdf worker.

Make it more dynamic by searching recursively.
This commit is contained in:
Emanuel Schütze 2017-02-17 14:54:24 +01:00
parent 0075c23225
commit a804362e16

View File

@ -27,37 +27,35 @@ pdfMake.fonts = {
} }
}; };
// Create PDF on message and return the base64 decoded document // Function to replace layout placeholder
self.addEventListener('message', function(e) { //
var data = JSON.parse(e.data); // Workaround for using table layout functions.
// TODO: Needs improvement of pdfmake's web worker support.
// Currently only functions are allowed for 'layout'.
// But functions cannot be passed to workers (via JSON).
var replacePlaceholder = function (content) {
for (var i = 0; i < content.length; i++) {
if (typeof content[i] === 'object') {
// Workaround for using dynamic footer with page number. // motion meta table border lines
// TODO: Needs improvement of pdfmake's web worker support. if (content[i].layout === "{{motion-placeholder-to-insert-functions-here}}") {
// see https://github.com/bpampuch/pdfmake/issues/38 content[i].layout = {
if (data.footerTpl) { hLineWidth: function(i, node) {
data.footer = function (currentPage, pageCount) { return (i === 0 || i === node.table.body.length) ? 0 : 0.5;
var footerText = data.footerTpl.text },
.replace('{{currentPage}}', currentPage) vLineWidth: function() {
.replace('{{pageCount}}', pageCount); return 0;
return { },
text: footerText, hLineColor: function() {
alignment: data.footerTpl.alignment, return 'white';
margin: data.footerTpl.margin, }
fontSize: data.footerTpl.fontSize,
color: data.footerTpl.color
};
}; };
return true;
} }
// Workaround for using table layout functions.
// TODO: Needs improvement of pdfmake's web worker support.
// Currently only functions are allowed for 'layout'.
// But functions cannot be passed to workers (via JSON).
//
// ballot paper crop marks // ballot paper crop marks
for (var i = 0; i < data.content.length; i++) { if (content[i].layout === "{{ballot-placeholder-to-insert-functions-here}}") {
if (data.content[i].layout === "{{ballot-placeholder-to-insert-functions-here}}") { content[i].layout = {
data.content[i].layout = {
hLineWidth: function(i, node) { hLineWidth: function(i, node) {
if (i === 0){ if (i === 0){
return 0; return 0;
@ -81,22 +79,38 @@ self.addEventListener('message', function(e) {
return 'gray'; return 'gray';
} }
}; };
return true;
} }
// motion meta table border lines replacePlaceholder(content[i]);
if (data.content[i].layout === "{{motion-placeholder-to-insert-functions-here}}") {
data.content[i].layout = {
hLineWidth: function(i, node) {
return (i === 0 || i === node.table.body.length) ? 0 : 0.5;
},
vLineWidth: function() {
return 0;
},
hLineColor: function() {
return 'white';
} }
}
};
// Create PDF on message and return the base64 decoded document
self.addEventListener('message', function(e) {
var data = JSON.parse(e.data);
// Workaround for using dynamic footer with page number.
// TODO: Needs improvement of pdfmake's web worker support.
// see https://github.com/bpampuch/pdfmake/issues/38
if (data.footerTpl) {
data.footer = function (currentPage, pageCount) {
var footerText = data.footerTpl.text
.replace('{{currentPage}}', currentPage)
.replace('{{pageCount}}', pageCount);
return {
text: footerText,
alignment: data.footerTpl.alignment,
margin: data.footerTpl.margin,
fontSize: data.footerTpl.fontSize,
color: data.footerTpl.color
};
}; };
} }
}
replacePlaceholder(data.content);
var pdf = pdfMake.createPdf(data); var pdf = pdfMake.createPdf(data);
pdf.getBase64(function (base64) { pdf.getBase64(function (base64) {
self.postMessage(base64); self.postMessage(base64);