简体   繁体   English

Angular:Typescript:未捕获类型错误:无法设置未定义的属性“autoTable”

[英]Angular: Typescript: Uncaught TypeError: Cannot set property 'autoTable' of undefined

I am trying to import a simple table using angular with jspdf-autotable.我正在尝试使用 angular 和 jspdf-autotable 导入一个简单的表。 But I cant, Here is error但我不能,这是错误

jspdf.plugin.autotable.js:1023 Uncaught TypeError: Cannot set property 'autoTable' of undefined
    at Object.<anonymous> (jspdf.plugin.autotable.js:1023)
    at __webpack_require__ (jspdf.plugin.autotable.js:39)
    at jspdf.plugin.autotable.js:103
    at jspdf.plugin.autotable.js:106
    at webpackUniversalModuleDefinition (jspdf.plugin.autotable.js:12)
    at Object../node_modules/jspdf-autotable/dist/jspdf.plugin.autotable.js (jspdf.plugin.autotable.js:19)
    at __webpack_require__ (bootstrap:84)
    at Module../src/app/components/zone-list/zone-list.component.ts (zone-list.component.ts:12)
    at __webpack_require__ (bootstrap:84)
    at Module../src/app/app-routing.module.ts (app-routing.module.ts:3)

For package.json :对于package.json

"jspdf": "^2.1.0",
"jspdf-autotable": "^2.3.5",

For .ts :对于.ts

here is import :这是进口

import jsPDF from 'jspdf';
var autoTable = require('jspdf-autotable');

here is download function:这里是下载 function:

var head = [['ID', 'Country', 'Rank', 'Capital']]
var data = [
  [1, 'Denmark', 7.526, 'Copenhagen'],
  [2, 'Switzerland', 7.509, 'Bern'],
  [3, 'Iceland', 7.501, 'Reykjavík'],
  [4, 'Norway', 7.498, 'Oslo'],
  [5, 'Finland', 7.413, 'Helsinki'],
]
const doc = new jsPDF()
autoTable(doc, {
  head: head,
  body: data,
  didDrawCell: (data) => {
    console.log(data.column.index)
  },
})

doc.save('table.pdf')

Tell me what went wrong?告诉我哪里出了问题?

what happens if you replace如果你更换会发生什么

var autoTable = require('jspdf-autotable') with this: import autoTable from 'jspdf-autotable' ? var autoTable = require('jspdf-autotable')与此: import autoTable from 'jspdf-autotable'

Solution: Do not use jspdf, use pdfmake [https://www.npmjs.com/package/pdfmake]解决方案:不要使用jspdf,使用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.当然,我明白了——没有图书馆是 100% 完美的。 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)如:我的情况是html表到typescript中的PDF。我也试过jspdf-autotable但没有运气)

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.我的工作现在更简单了,我不需要像 html2canvas 这样的其他截图工具来获取我所有的表格截图,我不再需要担心我的截图,图像大小调整。 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.此外,我在 PDF 中有一个实际的表,这意味着我可以从我的 PDF 复制数据,它不再只是一个图像。

So lets begin.让我们开始吧。

Install pdfmake:安装 pdfmake:

(my case was install for Angular) (我的案例是为 Angular 安装的)

npm i pdfmake --save

typescript code: typescript 代码:

Import it:导入它:

import pdfMake from "../../../../node_modules/pdfmake/build/pdfmake";
import pdfFonts from "../../../../node_modules/pdfmake/build/vfs_fonts";
pdfMake.vfs = pdfFonts.pdfMake.vfs;

Write function:写入 function:

Now just write a function for download PDF that you want to trigger by you PDF download button现在只要写一个function下载PDF你想触发你PDF下载按钮

  peopleExportToPdf() {
    let docDefinition = {
      content: [
        {
          table: {
            body: [
              [{ text: 'SL#', bold: true }, { text: 'Name', bold: true }, { text: 'Age', bold: true }],
              [{ text: 'Bold value', bold: true }, 'Val 2', 'Val 3'],
            ]
          }
        }]
    }

    docDefinition.content[0].table.body.pop(); //remove the unnecessary 2nd row
    let slno: number = 1;
    for (let p of this.people) {
      docDefinition.content[0].table.body.push([{ text: slno.toString(), bold: true }, p.name, p.age.toString()]);
      slno = slno +1;
    }
    pdfMake.createPdf(docDefinition).download('Report.pdf');
  }

3 heads up: 3个头:

  1. I have a table that has 3 columns slno, name, age.我有一个包含 3 列 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 如何使用pdfmake将html表转换为pdf
  2. this github link这个 github 链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM