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
1 changed files with 62 additions and 48 deletions

View File

@ -27,37 +27,35 @@ pdfMake.fonts = {
}
};
// Create PDF on message and return the base64 decoded document
self.addEventListener('message', function(e) {
var data = JSON.parse(e.data);
// Function to replace layout placeholder
//
// 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.
// 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
};
// motion meta table border lines
if (content[i].layout === "{{motion-placeholder-to-insert-functions-here}}") {
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';
}
};
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
for (var i = 0; i < data.content.length; i++) {
if (data.content[i].layout === "{{ballot-placeholder-to-insert-functions-here}}") {
data.content[i].layout = {
if (content[i].layout === "{{ballot-placeholder-to-insert-functions-here}}") {
content[i].layout = {
hLineWidth: function(i, node) {
if (i === 0){
return 0;
@ -81,22 +79,38 @@ self.addEventListener('message', function(e) {
return 'gray';
}
};
return true;
}
// motion meta table border lines
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';
replacePlaceholder(content[i]);
}
}
};
// 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);
pdf.getBase64(function (base64) {
self.postMessage(base64);