Hey!
I tried to add custom layout functions to a table which is contained in a footer:
var dd = {
pageMargins: [ 30, 60, 30, 80 ],
content: [
'First paragraph',
'Another paragraph, this time a little bit longer to make sure, this line will be divided into at least two lines'
],
header: 'simple text',
footer: {
style: 'footer',
layout: {
hLineWidth: function (i, node) {return 0;},
vLineWidth: function (i, node) {return 0;},
fillColor: function (i, node) {return 'green';},
paddingLeft: function(i, node) {return 10;}
},
// layout: 'headerLineOnly',
table: {
widths: ['25%', '25%', '25%', '25%'],
headerRows: 1,
body: [
['foo foo bar bar' , 'foo foo bar bar', 'foo foo bar bar', 'foo foo bar bar'],
[
'foo foo bar bar\n' +
'foo foo bar bar \n' +
'foo foo bar bar\n' +
'foo foo bar bar',
{text: [
{text: 'foo foo bar bar\n', bold: true},
'foo foo bar bar'
]},
'foo foo bar bar\n' +
'foo foo bar bar\n' +
'foo foo bar bar',
'foo foo bar bar\n' +
'foo foo bar bar\n' +
'foo foo bar bar\n' +
'foo foo bar bar\n' +
'foo foo bar bar'
]
]
}
},
styles: {
footer: {
fontSize: 6,
margin: [15, 20, 15, 10]
}
}
};
but I only get in this case a 'simple' table.
If I use a predefined layout (in this case 'headerLineOnly'), I get the corresponding result.
Is my code wrong? Or is it impossible? Or a bug?
Thanks in advance!
just do it like this:
pdfMake.tableLayouts = {
myCustomLayout: {
hLineWidth: function (i, node) {return 0;},
vLineWidth: function (i, node) {return 0;},
fillColor: function (i, node) {return 'green';},
paddingLeft: function(i, node) {return 10;}
}
};
var dd = {
pageMargins: [ 30, 60, 30, 80 ],
content: [
'First paragraph',
'Another paragraph, this time a little bit longer to make sure, this line will be divided into at least two lines'
],
header: 'simple text',
footer: {
style: 'footer',
layout: 'myCustomLayout',
table: {
widths: ['25%', '25%', '25%', '25%'],
headerRows: 1,
body: [
['foo foo bar bar' , 'foo foo bar bar', 'foo foo bar bar', 'foo foo bar bar'],
[
'foo foo bar bar\n' +
'foo foo bar bar \n' +
'foo foo bar bar\n' +
'foo foo bar bar',
{text: [
{text: 'foo foo bar bar\n', bold: true},
'foo foo bar bar'
]},
'foo foo bar bar\n' +
'foo foo bar bar\n' +
'foo foo bar bar',
'foo foo bar bar\n' +
'foo foo bar bar\n' +
'foo foo bar bar\n' +
'foo foo bar bar\n' +
'foo foo bar bar'
]
]
}
},
styles: {
footer: {
fontSize: 6,
margin: [15, 20, 15, 10]
}
}
}
Thanks for your answer!
Your code works fine if I paste it into the playground. But if I apply the above to my server code, then again a 'simple' table will be created:
// routes.js
...
var pdfMake = require('pdfmake');
var pdfG = require('pdfG.js');
...
pdfG.createPdfDocument(data, function (dd, err) {
...
var printer = new pdfMake(pdfG.fonts);
printer.tableLayouts = {
myCustomLayout: {
hLineWidth: function (i, node) {return 0;},
vLineWidth: function (i, node) {return 0;},
fillColor: function (i, node) {return 'green';},
paddingLeft: function(i, node) {return 10;}
}
};
var pdfDoc = printer.createPdfKitDocument(dd);
res.contentType('application/pdf');
pdfDoc.pipe(res);
pdfDoc.end();
});
...
// pdfG.js
...
function createPdfDocument(data, callback) {
var dd = {
...
footer: {
...
layout: 'myCustomLayout',
...
}
...
};
...
callback(dd);
}
you have to put it into the options of createPdfKitDocument (second argument)
tested - works: https://glitch.com/edit/#!/pdfmake-issue-1452 (Preview: https://pdfmake-issue-1452.glitch.me/)
Thank you!
Most helpful comment
you have to put it into the options of
createPdfKitDocument(second argument)