0

I am using pdfMake in Angular 8 app. the class has an invoice object populated with correct data. I want to make a table in header of pdf which will be included on all pages. I am unable to use object variables and get error. If I call to a function to return a table it also does not work.

TypeError: Cannot read property 'invoice' of undefined

Below is snippet of header of pdfMake.

header: function (currentPage, pageCount, pageSize) {
        return [
          { text: 'simple text' + this.invoice.branch.name , alignment: (currentPage % 2) ? 'left' : 'right' },
          { canvas: [{ type: 'rect', x: 170, y: 32, w: pageSize.width - 170, h: 40 }] }
        ]
      }

If use a call function.

header: function (currentPage, pageCount, pageSize) {
        this.getInvoiceHeaderObject(currentPage, pageCount, pageSize, this.invoice);
      }

Function

getInvoiceHeaderObject(currentPage, pageCount, pageSize, invoice: Invoice) {
    return {
      table: {
        widths: [25, '*', '*', 45, 55],
        body: [
          [
            {
              text: 'Invoice No.',
              style: 'tableHeader',
              alignment: 'left'
            },
            {
              text: 'VAT Number',
              style: 'tableHeader',
              alignment: 'left'
            },
            {
              text: 'CASH SALE INVOICE',
              style: 'tableHeader',
              alignment: 'left'
            },
            {
              text: 'Branch',
              style: 'tableHeader',
              alignment: 'center'
            },
            {
              text: 'Date',
              style: 'tableHeader',
              alignment: 'right'
            },
          ],
          [
            {
              text: invoice.invoiceNumber,
              alignment: 'left'
            },
            {
              text: invoice.customer.vat,
              alignment: 'left'
            },
            {
              text: currentPage + '/' + pageCount,
              alignment: 'left'
            },
            {
              text: invoice.branch.name,
              alignment: 'center'
            },
            {
              text: 'data',
              alignment: 'right'
            }
          ]
        ]
      }
    };
  }
usmanwalana
  • 1,002
  • 2
  • 14
  • 31

0 Answers0