0

I am using PDF make to create PDF client side. PDFMake uses below format to generate the PDF

 var docDefinition = {
            content: [
              {
                  table: {
                      // headers are automatically repeated if the table spans over multiple pages
                      // you can declare how many rows should be treated as headers
                      headerRows: 1,
                      widths: ['*', 'auto', 100, '*'],

                      body: [
                        ['first', 'second', 'third', 'the last one'],
                        ['value 1', 'value 2', 'value 3', 'value 4'],
                        [{ text: 'bold value', bold: true }, 'val 2', 'val 3', 'val 4']                          ]
                  }
              }
            ]
        };


        pdfMake.createPdf(docDefinition).download('optionalName.pdf');

With above format I am able to create PDF successfully but when I am trying to provide values dynamically as code shown below, I am getting error as JavaScript runtime error: Malformed table row, a cell is undefined.

               var body = [];

                var dataRow = [];
                var aaa = $('#dataGrid').jqGrid('getRowData').map(function (o) {
                    for (k in o) {
                        dataRow.push(o[k]);
                    }
                    body.push(dataRow);
                });

and below the object passing format

        var docDefinition = {
            content: [
              {
                  table: {
                      // headers are automatically repeated if the table spans over multiple pages
                      // you can declare how many rows should be treated as headers
                      headerRows: 1,
                      widths: ['*', 'auto', 100, '*'],

                      body: [
  ['ACTIVE', 'DESCRIPTION', 'STUDENT_ID', 'STATUS', 'TYPE'],
                             body
                      ]
                  }
              }
            ]
        };

What's wrong in the required format and mine? Please sugges.Thanks

RAM
  • 75
  • 2
  • 10

1 Answers1

0

You need to include the header row in the body variable. So instead of var body = [] you need to initialize it using this:

var body = [ ['ACTIVE', 'DESCRIPTION', 'STUDENT_ID', 'STATUS', 'TYPE'] ];

Then when using it, just use body:

// ...
table: {
    headerRows: 1,
    widths: ['*', 'auto', 100, '*'],
    body: body
}
// ...
Peter B
  • 22,460
  • 5
  • 32
  • 69