'how to get rid of unaccounted space when displaying JSON data in a for loop
doc
.rect(50, 50, 500, 20).fill('#233E6F')
.fontSize(12)
.fillColor('white')
.text('Credit Profile Analysis', 60,55)
.moveDown()
const credit = doc.y
doc
.fontSize(7)
.fillColor('gray')
.text('Account', 55, credit, {width:75})
.text('Account#', 160, credit, {width:75})
.text('Date Opened', 240, credit, {width:45})
.text('Account Status', 300, credit, {width:55})
.text('Current Balance', 380, credit, {width:55})
.text('Overdue Amount', 445, credit, {width:65})
.text('Last Paid', 510, credit, {width:50})
.moveDown()
// Horizontal Lines.
doc
.lineWidth(0.5)
.moveTo(50, doc.y)
.lineTo(550, doc.y)
.stroke('#D6DBDF')
// data
const profile = enquiry.CC_RESULTS.EnqCC_CPA_ACCOUNTS
for (let i = 0; i < profile.length; i++){
const item = profile[i]
const docY = doc.y + 2 + (i * 2)
doc
.fillColor('#616A6B')
.fontSize(7)
.text(item.SUBSCRIBER_NAME, 55, docY, {width:105, height: 30})
.text(item.ACCOUNT_NO, 160, docY, {width:75, height: 30})
.text(item.OPEN_DATE, 240, docY, {width:65, height: 30})
.text(item.STATUS_CODE_DESC, 300, docY, {width:105, height: 30})
.text(`R ${item.CURRENT_BAL}`, 380, docY, {width:50, height: 30, align:'right'})
if (parseInt(item.OVERDUE_AMOUNT) > 0){
doc.fillColor('red').text(`R ${item.OVERDUE_AMOUNT}`, 450, docY, {width:45, height: 30, align:'right'})
}
else {
doc.fillColor('#616A6B').text(`R ${item.OVERDUE_AMOUNT}`, 450, docY, {width:45, height: 30, align:'right'})
}
if (item.LAST_PAYMENT_DATE != ''){
doc.fillColor('#616A6B').text(item.LAST_PAYMENT_DATE, 490, docY, {width:50, height: 30, align:'right'})
}
else{
doc.fillColor('#616A6B').text('-', 490, docY, {width:50, height: 30, align:'center'})
}
doc.moveDown()
doc // Horizontal Lines
.lineWidth(0.5)
.moveTo(50, doc.y)
.lineTo(550, doc.y)
.stroke('#D6DBDF');
}
I'm using angular and pdfkit to generate a credit report in pdf format. The report consist of different account tables. the table data is from JSON data. I'm using a For Loop to display the JSON data in a makeshift table but the rows are uneven. There is a unaccounted space between the data being displayed. The rows are unequal.
The red mark shows the different height of the rows.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|

