natancabral / pdfkit-table

Helps to draw informations in simple tables using pdfkit. #server-side. Generate pdf tables with javascript (PDFKIT plugin)
MIT License
93 stars 59 forks source link

how to add row or column border and change its property like width, color #76

Open krButani opened 1 year ago

krButani commented 1 year ago

how to add row or column border and change its property like width, color

mahmutBerat commented 8 months ago

hey, I found a way for row and cell stylings which is using pdfkit capabilities in the pdkit-table. You can check this link to draw a rectangle border https://pdfkit.org/docs/vector.html. Simply, use the doc.rect() function in the prepareRow hook of table options.

For example, this code sample draws border and fills the cell with a background color:

await doc.table(table, { 
/* {options starts} */
..
prepareRow: (row, indexColumn, indexRow, rectRow, rectCell) => {
           doc.font('Helvetica').fontSize(9);
                   doc.rect(rectCell?.x ?? 0,
                rectCell?.y ?? 0,
                rectCell?.width ?? 0,
                rectCell?.height ?? 0)
                   .fillOpacity(0.5)
              .fillAndStroke('red', 'black')
          .fillColor('black', 1);
}
/* {options ends} */
});

hope this helps