Created
January 13, 2023 08:10
-
-
Save agronom81/0ba057ce50085ead19b01c65cc4234b2 to your computer and use it in GitHub Desktop.
Dividers in pdfkit-table
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// create document | |
const doc = new PDFDocument({ margin: 30, }); | |
// to save on server | |
doc.pipe(fs.createWriteStream("./document-5.pdf")); | |
const table01 = { | |
"headers" : ["A", "B", "C"], | |
"rows": [ | |
[ "Version 0.1.74", "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus id est ipsum. Fusce scelerisque maximus justo, lacinia ornare felis iaculis nec.", "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus id est ipsum. " ], | |
[ "Update:", "Age 2", "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus id est ipsum. Fusce." ], | |
[ "$ npm pdfkit-table@latest", "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus id est ipsum.", "Year 3" ], | |
[ "Thanks", "Age 4", "Year 4" ], | |
], | |
}; | |
doc.table(table01, { | |
columnSpacing: 10, | |
padding: 10, | |
columnsSize: [200, 220, 135], | |
align: "center", | |
prepareHeader: () => doc.fontSize(11), // {Function} | |
// ----------------------------------------------------------------- | |
// HERE THE MAGIC: | |
// ----------------------------------------------------------------- | |
prepareRow: (row, indexColumn, indexRow, rectRow, rectCell) => { | |
const {x, y, width, height} = rectCell; | |
// first line | |
if(indexColumn === 0){ | |
doc | |
.lineWidth(.5) | |
.moveTo(x, y) | |
.lineTo(x, y + height) | |
.stroke(); | |
} | |
doc | |
.lineWidth(.5) | |
.moveTo(x + width, y) | |
.lineTo(x + width, y + height) | |
.stroke(); | |
doc.fontSize(10).fillColor('#292929'); | |
}, // {Function} | |
}); | |
// done | |
doc.end(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment