Search code examples
javascriptpdfmake

Build table dynamically with PDFmake


I found an example of how to fill the table with pdfmake dynamically, which has 2 columns. Now I tried add another column 'height' to the table but I don't know how to modify it.

function buildTableBody(data, columns) {
    var body = [];

    body.push(columns);

    data.forEach(function(row) {
        var dataRow = [];

        columns.forEach(function(column) {
            dataRow.push(row[column].toString());
        })

        body.push(dataRow);
    });

    return body;
}

function table(data, columns) {
    return {
        table: {
            headerRows: 1,
            body: buildTableBody(data, columns)
        }
    };
}

function Pdftest(){
  var externalDataRetrievedFromServer = [
    { name: 'Bartek', age: 34, height: 1.78 },
    { name: 'John', age: 27, height: 1.79 },
    { name: 'Elizabeth', age: 30, height: 1.80 },
  ];

    var dd = {
    content: [
        { text: 'Dynamic parts', style: 'header' },
        table(externalDataRetrievedFromServer, ['name', 'age', 'height'])
    ]
}
pdfMake.createPdf(dd).download();
}

Does anybody know what needs to be modified?


Solution

  • Error was anywhere else, the code above works perfectly fine