Hi,
I’m using pdfMake cordova plugin to generate single pdf file with multiple contents. But in the End after downloading file the pdf file contains only last record from the array.
createPdf() {
var docDefinition1 = [];
for (var i = 0; i < this.ledger_array.length; i++) {
var docDefinition = {
content: [
{ text: 'LEDGER', style: 'header' },
{ text: 'From:', style: 'subheader', alignment: 'right' },
{ text: this.from, alignment: 'right' },
{ text: 'To:', style: 'subheader', alignment: 'right' },
{ text: this.to, alignment: 'right' },
{ text: 'Date: ', style: 'subheader' },
{ text: this.ledger_array[i].date, alignment: 'left' },
// { text: this.letterObj.from },
// { text: 'To', style: 'subheader' },
// this.letterObj.to,
// { text: this.letterObj.text, style: 'story', margin: [0, 20, 0, 20] },
{
table: {
// headers are automatically repeated if the table spans over multiple pages
// you can declare how many rows should be treated as headers
headerRows: 1,
widths: ['*', '*', '*', '*', '*'],
body: [
['Sr.', 'Name', 'Amount Payable', 'Time', 'Amount'],
['Value 1', 'Value 2', 'Value 3', 'Value 4', 'value 5'],
['Value 1', 'Value 2', 'Value 3', 'Value 4', 'value 5'],
['', '', '', 'Total:', this.ledger_array[i].total],
]
}
}
],
styles: {
header: {
fontSize: 18,
bold: true,
},
subheader: {
fontSize: 14,
bold: true,
margin: [0, 15, 0, 0]
},
story: {
italic: true,
alignment: 'center',
width: '50%',
}
}
}
docDefinition1.push(docDefinition);
console.log("COntent: ", docDefinition);
console.log("Doc def:", docDefinition1);
}
for (var j = 0; j < docDefinition1.length; j++) {
this.pdfObj = pdfMake.createPdf(docDefinition1[j]);
}
console.log("pdf Object:", this.pdfObj);
}
downloadPdf() {
if (this.plt.is('cordova')) {
this.pdfObj.getBuffer((buffer) => {
var blob = new Blob([buffer], { type: 'application/pdf' });
// Save the PDF to the data Directory of our App
this.file.writeFile(this.file.dataDirectory, 'myletter.pdf', blob, { replace: true }).then(fileEntry => {
// Open the PDf with the correct OS tools
this.fileOpener.open(this.file.dataDirectory + 'myletter.pdf', 'application/pdf');
})
});
} else {
// On a browser simply use download!
this.pdfObj.download();
}
}