pdfkit-table icon indicating copy to clipboard operation
pdfkit-table copied to clipboard

Problem with layout and colors when using prepareRow

Open MeMineToMe opened this issue 4 years ago • 2 comments

Several problems occur when using custom style in rows :

  1. When setting a font-size, the first line height is not properly calculated
  2. When setting background-color, the first column text has an opacity set
  3. When adding twice the same table, columns are superimposed

Here is the code to reproduce and the PDF generated is attached :

    const fs = require("fs");
    const PDFDocument = require("pdfkit-table");

    let doc = new PDFDocument({ margin: 30, size: 'A4' });
    doc.pipe(fs.createWriteStream("./document.pdf"));

    const table1 = {
      headers: [ "head1" , "head2" ],
      rows: [
        ['Prop1', 'Value1'],
        ['Prop2', 'Value2'],
        ['Prop3', 'Value3'],
        ['Prop4', 'Value4']
      ]
    };

    const table2 = {
      headers: [
        { label: "head1", property: 'prop' },
        { label: "head2", property: 'value' }
      ],
      datas: [
        {prop: 'Prop1', value: 'Value1'},
        {prop: 'Prop2', value: 'Value2'},
        {prop: 'Prop3', value: 'Value3'},
        {prop: 'Prop4', value: 'Value4'}
      ],
    };

    const tableOptions1 = {
      prepareRow: (row, indexColumn, indexRow, rectRow) => {
        indexRow === 0 && doc.font("Helvetica").fontSize(22);
      }
    };

    const tableOptions2 = {
      prepareRow: (row, indexColumn, indexRow, rectRow) => {
        indexRow === 0 && doc.font("Helvetica").fontSize(22);
        if(indexColumn === 0) {
          doc.fillColor('white');
          doc.addBackground(rectRow, (indexRow % 2 ? 'blue' : 'green'), 0.5); 
        }
      }
    };

    doc.table(table1, tableOptions1);
    doc.table(table2, tableOptions2);
    doc.table(table2, tableOptions2);
    doc.end();

document.pdf

MeMineToMe avatar Jan 24 '22 15:01 MeMineToMe

Same issue here.

dev-fredericfox avatar Jun 15 '22 12:06 dev-fredericfox

Let me see...

natancabral avatar Jun 17 '22 13:06 natancabral