Skip to content
Advertisement

Angular: Typescript: Uncaught TypeError: Cannot set property ‘autoTable’ of undefined

I am trying to import a simple table using angular with jspdf-autotable. But I cant, Here is error

JavaScript

For package.json :

JavaScript

For .ts :

here is import :

JavaScript

here is download function :

JavaScript

Tell me what went wrong?

Advertisement

Answer

Solution : Do not use jspdf, use pdfmake [https://www.npmjs.com/package/pdfmake]

(Why should you use a buggy tool when there is a far better tool out there with better features and has a easier way to code

Of course, I get it – no library is 100% perfect. But some library is more buggier than others when we talk about a special feature or purpose. Such as : My case was html table to PDF in typescript. I tried jspdf-autotable also but no luck)

My work is now more simple, I don’t need another screenshot tool like html2canvas to get all my tables screenshot, I no longer need to worry about my screenshot anymore, image resizing. Furthermore, I have an actual table that is in PDF, which means I can copy data from my PDF, its not only an image anymore.

So lets begin.

Install pdfmake:

(my case was install for Angular)

JavaScript

typescript code:

Import it:

JavaScript

Write function:

Now just write a function for download PDF that you want to trigger by you PDF download button

JavaScript

3 heads up:

  1. I have a table that has 3 columns slno, name, age. Design your table according to your need.
  2. If you have a non-string item, convert it to string (I had to convert my age to string, if you don’t you might have an error – I had to face it)
  3. You see I had to give an unnecessary row then removed it. you might have to do it also (I had to because I faced error, if you have found a better solution please post it)

Courtesy:

I got help from the two links below:

  1. How to convert html table to pdf using pdfmake
  2. this github link
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement