Pdfmake: custom layout functions for a table contained in a footer are ignored

Created on 27 Jul 2018  路  5Comments  路  Source: bpampuch/pdfmake

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!

Most helpful comment

you have to put it into the options of createPdfKitDocument (second argument)

All 5 comments

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!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Christian24 picture Christian24  路  3Comments

MathLavallee picture MathLavallee  路  3Comments

jokris1 picture jokris1  路  3Comments

Masber picture Masber  路  3Comments

ValeSauer picture ValeSauer  路  3Comments