【问题标题】:jsPDF AutoTable - autoTable is not a functionjsPDF AutoTable - autoTable 不是函数
【发布时间】:2017-11-27 07:40:26
【问题描述】:

我在 Angular 应用程序上使用 JSPdf,我正在尝试使用 JS 自动表格插件,但我遇到了 JS 错误

异常:未捕获(承诺):TypeError:doc.autoTable 不是函数

TypeError: doc.autoTable 不是函数

我通过 npm 安装了 jspdf 和 jspdf-autotable,我确认它们在节点模块中。

我已经通过这种方式导入了两个插件:

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

这是我的代码:

private renderPdf():void{
    let testcolumns = ["TestCol1", "TestCol2"];
    let testrows = [["test item 1", "test item 2"]];
    let doc = new jsPDF();
    doc.autoTable(testcolumns, testrows);
    doc.save('sample.pdf');
}

这里有什么我可能遗漏的或者我可以提供更多代码来帮助确定问题吗?

谢谢!

【问题讨论】:

    标签: javascript angularjs pdf-generation jspdf jspdf-autotable


    【解决方案1】:

    只需删除导入的 2 第一行并添加以下行:

    var jsPDF = require('jspdf');
    require('jspdf-autotable');
    

    你可以看一个例子here

    【讨论】:

    • 这已经拯救了我的一天!
    【解决方案2】:

    我遇到了同样的问题,这个问题对我有用。 我已经把它写在 import as

    import * as jsPDF from 'jspdf';
    import 'jspdf-autotable';
    

    在函数中我将其声明为

    const doc = new jsPDF();
    

    【讨论】:

    • import jsPDF from 'jspdf'; import 'jspdf-autotable';
    【解决方案3】:

    我遇到了同样的问题,我这样解决了:

    import jsPDF from '../../node_modules/jspdf/dist/jspdf.umd.min.js'
    import { applyPlugin } from 'jspdf-autotable'
    applyPlugin(jsPDF)
    

    我使用“jspdf”:“^2.3.1”,“jspdf-autotable”:“^3.5.20” 希望对你有帮助!

    【讨论】:

      【解决方案4】:

      我今天在使用https://github.com/SimulatedGREG/electron-vue 时遇到了同样的问题。我通过将“jspdf”和“jspdf-autotable”添加到 path-to-project/.vscode 中的白名单数组来解决它

      let whiteListedModules = [
        'vue',
        'vue-sweetalert2',
        'element-ui',
        'vue-avatar-component',
        'vue-router', 
        'vue-json-excel',
        'vuex',
        'vue-chart-js',
        'pluralize',   
        'Print',
        'jspdf',
        "jspdf-autotable"
      ]
      

      【讨论】:

        【解决方案5】:

        您可以按照正常导入的方式导入 jsPDF:

        import jsPDF from 'jspdf';
        

        然后是自动表:

        require('jspdf-autotable');
        

        在函数中添加这个 ^

        【讨论】:

          【解决方案6】:

          这对我有用:

          import jsPDF from 'jspdf';
          
          require('jspdf-autotable');
          
          const tableColumns = ["column1", "Column2", "Column3"];
          
          const tableRows = [[1,2,3],[a,b,c],[X,Y,Z]];
          
          const doc = new jsPDF();
          
          doc.autoTable(tableColumns, tableRows, { startY: 20 });
          
          doc.text("Closed tickets within the last one month.", 14, 15);
          
          doc.save('dataModel.pdf');
          

          【讨论】:

            猜你喜欢
            • 2019-01-23
            • 1970-01-01
            • 1970-01-01
            • 2017-08-15
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多