jsPDF-AutoTable icon indicating copy to clipboard operation
jsPDF-AutoTable copied to clipboard

TypeError: c.autoTable is not a function

Open chun222 opened this issue 2 years ago • 11 comments

hello,你好! it can works in vue3 dev env. but when i build it ,it can not works in product env. image

"jspdf": "^2.5.0",
"jspdf-autotable": "^3.5.23",
import jsPDF from "jspdf";
 import "jspdf-autotable";
 const print = async () => {
      const PageMargin = { top: 20, right: 10, bottom: 15, left: 10 };
      const doc = new jsPDF({
        orientation: "p",
        unit: "mm",
        format: "a4",
      });
await something .......
 doc.autoTable({
          styles: { font: "hansans-normal" },
          columnStyles: { europe: { halign: "center" } }, // European countries centered
          body: state.datasource,
          columns: state.pdfheader,
          startY: startY, //第一次
          margin: PageMargin,
        });
}

TypeError: c.autoTable is not a function

chun222 avatar Jan 22 '22 06:01 chun222

help me 55555555

chun222 avatar Jan 22 '22 10:01 chun222

try to use it like this:

(doc as any).autoTable({

https://codezup.com/angular-9-10-export-table-data-to-pdf-using-jspdf/

KoblerS avatar Jan 24 '22 15:01 KoblerS

Probably a duplicate of #857?

andreasbhansen avatar Jan 25 '22 15:01 andreasbhansen

Probably a duplicate of #857?

very likely, yes

micksp avatar Jan 25 '22 17:01 micksp

I had meet the same problem today, and I use Vite which use rollup-commonjs-plugin to convert commonjs module to ESM during build.

After looking into dist code, I found somehow commonjs plugin did not convert require("jspdf"):

...
module.exports = e(function() {
      try {
        return require("jspdf");
      } catch (t4) {
      }
    }());
...

which should be something like this:

var require$$0 = /* @__PURE__ */ getAugmentedNamespace(jspdf_es_min);
...
...
module.exports = e(function() {
      try {
        return require$$0;
      } catch (t4) {
      }
    }());

since autotabe need apply itself as plugin to jspdf, this is the immediate cause of the problem.

After further investigation, I found in Vite version 2.6.3, they upgrade commonjs plugin to version 21.0.0 upgrade to @rollup/plugin-commonjs 21.x and what happened in commonjs plugin to version 21.0.0?

they set ignoreTryCatch default as true use safe default value for ignoreTryCatch

and, this repo, use try catch when require jspdf

So, in conclusion, to solve this problem, explicitly set ignoreTryCatch to false:

defineConfig({
...
  build: {
    commonjsOptions: {
      ignoreTryCatch: false
    }
  }
  ...
})

FPG-Alan avatar Feb 10 '22 03:02 FPG-Alan

Great research into this. The best solution here would likely to start shipping a separate esm version of the library which would not require the require call.

simonbengtsson avatar Feb 18 '22 17:02 simonbengtsson

For the record to anyone coming from Angular 13 and PrimeNG Table demo. Trying to use this versions:

  • "jspdf": "2.5.1";
  • "jspdf-autotable": "3.5.24";

I've solved this issue with the following code:

import * as jsPDF from 'jspdf';
import 'jspdf-autotable';
...
  exportPdf() {
    const doc = new jsPDF.default();
    (doc as any).autoTable({columns: this.exportColumns, body: this.data});
    doc.save('data.pdf');
  }

vstiebe avatar Jun 01 '22 11:06 vstiebe

try to use it like this:

(doc as any).autoTable({

https://codezup.com/angular-9-10-export-table-data-to-pdf-using-jspdf/

it worked for me thanks

navinrangar avatar Nov 22 '22 08:11 navinrangar

@vstiebe This should give you console errors, the syntax is deprecated in the latest version of autotable.

vzakharov-rxnt avatar Feb 06 '23 20:02 vzakharov-rxnt

In order to use this on Nuxt (serverside) I'm using this facade

import { jsPDF as _jsPDF } from "jspdf";
import autoTable from "jspdf-autotable";
import type { jsPDFOptions } from "jspdf";
import type { UserOptions } from "jspdf-autotable";

interface jsPDF extends _jsPDF {
    autoTable: (options: UserOptions) => void
}

function buildPDF (options?: jsPDFOptions) {
    const doc : Partial<jsPDF> = new _jsPDF(options);
    doc.autoTable = (options: UserOptions) => {
        (autoTable as any).default(doc, options); // Why is this necessary?
    };
    return doc as jsPDF;
}

export { buildPDF };

I tried all other options in this thread and none worked for me

kehwar avatar Jun 01 '23 04:06 kehwar

make sure you have install it. ( yarn add jspdf-autotable or npm install jspdf-autotable) after i install it worked

DrChhun avatar Jul 31 '23 15:07 DrChhun