aish.a
aish.a

Reputation: 186

How to get the finalY of lastAutoTable in jspdf-autoTable

I am using angular 9.I have a requirement to convert a html table with some content above and below the table into pdf. I am using jspdf-autotable.I followed the examples in the link https://github.com/simonbengtsson/jsPDF-AutoTable/blob/master/examples/examples.js and I am able to write a heading and then generate the table.The problem is when I need to add some text lines below the table.

My code is as below.

generatePdf(){
const doc = new jsPDF();
doc.setFontSize(18)
doc.text('ATTENDANCE REPORT FOR DEPOT - '+this.depotId, 14, 22);
autoTable(doc, { html: '#report-per-depot',startY: 30, });
doc.text(" ", 14, doc.lastAutoTable.finalY + 10)
doc.save('AttandancePerDepot.pdf');

}

The error I get is

Property 'lastAutoTable' does not exist on type 'jsPDF'.

I tried to import lastAutoTable as, import lastAutoTable from 'jspdf-autotable';
I doesnt show any error,but I am not sure how to get the finalY from it.

  lastAutoTable(doc,{});

The above doesnt show error but its return type is void.So this is where I am stuck. How do I get the finalY position in angular 9 or 10?

Upvotes: 5

Views: 7274

Answers (3)

Rizal Alfandi
Rizal Alfandi

Reputation: 1

In react and typescript, this is my solution

const finalY = (doc as jsPDF & { lastAutoTable: { finalY: number } }).lastAutoTable.finalY + 10;

Upvotes: 0

Jacob Aguilar
Jacob Aguilar

Reputation: 31

In angular 12 (Typescript)

import jsPDF from 'jspdf'
import autoTable from 'jspdf-autotable'

Then in the method

    crearPDF() {

    const doc =  new jsPDF();
    let finalY = 0;

    for (const key in this.entradasIndentificaciones) {
      if (Object.prototype.hasOwnProperty.call(this.entradasIndentificaciones, key)) {
        const element = this.entradasIndentificaciones[key];
        doc.text(element, 30, finalY + 10);
        autoTable(doc, { html: '#tableOn-' + key });
        finalY = (doc as any).lastAutoTable.finalY;

      }
    }
    console.debug("Guardando");
    doc.save("Report_.pdf");
  }

respects

Upvotes: 2

ImBatman
ImBatman

Reputation: 366

You're getting this error because Typescript is a strongly Typed language & lastAutoTable is not defined in the index.d.ts file (under jsPdf node module).

Below is a small hack to get around this error & get finalY value.

import jsPDF from 'jspdf';
import 'jspdf-autotable';
    
let finalY = (doc as any).lastAutoTable.finalY;

This worked for me in my Angular 10 project

Upvotes: 20

Related Questions