In my quest to dynamically generate PDFs using pdfmake, I've encountered an issue with creating dynamic rows based on data.
To illustrate, here is a simplified version of the code:
getDocumentDefinition(img: string, data: DataResponse, user: UserResponse): TDocumentDefinitions {
return {
header: this.header(img),
content: this.document(data, user),
styles: this.applyStyles(),
};
}
private document(data: DataResponse, user: UserResponse) {
let rowY = 252;
return [
// works
this.createRow(55, rowY, 1, { m: 10, s: 30 }, [163, 205], rowY + 6)
// not working in loop
data.map((item) => this.createRow(55, rowY, item.version, { m: item.minutes, s: item.seconds }, [163, 205], rowY + 6)).map(() => rowY += 34),
];
}
private createRow(rowX: number, rowY: number, version: number, time: { m: number; s: number }, x: number[], y: number): Content {
return [
this.createTable(10, version, time, {x: rowX, y: rowY}),
this.circle(),
this.circle(),
];
}
private createTable(heights: number, version: number, time: { m: number, s: number }, position: { x: number; y: number }): Content {
return {
table: {
widths: ['10%', '30%', '30%', '30%'],
heights: heights,
body: [
[
{
text: version,
fontSize: 10,
borderColor: ['white', 'white', 'white', '#B3CCE6'],
},
{
text: time.m + 'm ' + time.s + 's',
fontSize: 10,
borderColor: ['white', 'white', 'white', '#B3CCE6'],
},
{
text: '',
fontSize: 10,
borderColor: ['white', 'white', 'white', '#B3CCE6'],
},
{
text: '',
fontSize: 10,
borderColor: ['white', 'white', 'white', '#B3CCE6'],
},
],
],
},
absolutePosition: position,
};
}
The method createRow functions as expected when used singularly, but when attempted to be generated dynamically (as shown in the example), the result is empty. I have tried various loops and methods but have been unable to resolve the issue. Is there a way to add content based on an array with an unknown size?