How to generate pdf file using array on Contents

#1

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();
    }
  }


Capture

#2

You can see the example code which you need the same. Hope it works!

$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont(‘Arial’,‘B’,16);

$array1= array(1,2,3);
$array2= array(‘apple’, “ball”, “cat”);

$pdf->Cell(40,10,‘Numbers’);
$pdf->Cell(40,10,‘Animals’);
$pdf->Ln(10);
foreach($array1 as $key=>$row){
$pdf->Cell(40,10,$row);
$pdf->Cell(40,10,$array2[$key]);
$pdf->Ln(10);
}
$pdf->Output();