svg2pdf.js: PDF export does not work when using ES6 modules import

Importing jspdf-yworks and svg2pdf.js as ES6 modules does not work. The jsPDF instance is missing functionality and calling svg2pdf on it will result in an excpetion:

TypeError: o.advancedAPI is not a function

However, when loading the very same svg2pdf and jspdf files via script-tags (directly from /node_modules/), the export works as expected. Thus I assume there is something broken with the ES6 import.

I’ve attached a sample project with webpack and ES6 modules import. To reproduce run

npm install
npm run build

and open /dist/index.html. Then check the DevTools for the exception: es6-modules-test.zip

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 21 (19 by maintainers)

Commits related to this issue

Most upvoted comments

The fix for es6 imports will come with 2.0.1

You installed the wrong jsPDF:

npm install jspdf-yworks

Please support ES6 modules import, thx.

For the es6 imports I use Webpack like with @fskpf’s sample project. This works now when getting rid of

  • named defines
  • CommonJS exports

However, with the new build script from MrRio, the AMD require doesn’t work properly anymore:

require([
    'svg2pdf.js/dist/svg2pdf.min.js',
    'jsPDF/dist/jspdf.min.js'
  ], function (svg2pdf, jsPDF) {
  // here jsPDF is an object with "default", "rewire" and "restore" properties
  // "default" is jsPDF
}

I suppose the reason is the generated UMD header, that uses the AMD -> CommonJS wrapper:

define(["exports"], function(exports) {
  // ...
  exports.default = jsPDF;
  var _default2 = exports.default;
  function rewire($stub) {
    exports.default = $stub;
  }
  function restore() {
    exports.default = _default2;
  }

  exports.rewire = rewire;
  exports.restore = restore;

  Object.defineProperty(exports, '__esModule', { value: true });
})