Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

PDF export does not work when using ES6 modules import #59

Closed
fskpf opened this issue Oct 26, 2018 · 21 comments
Closed

PDF export does not work when using ES6 modules import #59

fskpf opened this issue Oct 26, 2018 · 21 comments

Comments

@fskpf
Copy link
Member

fskpf commented Oct 26, 2018

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

@yGuy
Copy link
Member

yGuy commented Oct 26, 2018

It really depends on how the functionality is loaded:

parallax/jsPDF#783 (comment)

@fskpf
Copy link
Member Author

fskpf commented Oct 26, 2018

Doesn't work with either approach in the test case.

@HackbrettXXX
Copy link
Member

Hhm, this is interesting, for some reason, jsPDF gets loaded as RGBColor...

@yGuy
Copy link
Member

yGuy commented Oct 26, 2018

Isnt't this the same problem as in yWorks/jsPDF#12

@HackbrettXXX
Copy link
Member

Kind of similar, yes ;)

I'm currently investigating it. Sadly, removing the named defines is not enough.
Currently, it works for es6, but no longer for AMD.

@yGuy
Copy link
Member

yGuy commented Oct 26, 2018

To fix this once and for all, the build should be adjusted to get rid of the multiple (named or unnamed) defines. However using "import" on the original sources might be an option. The minified sources are not ES6 compatible but are meant to be script-imported (or maybe AMD). What loader are you using that doesn't work? Is it webpack or browserify or is that the browser ES6 loader implementation?

@HackbrettXXX
Copy link
Member

HackbrettXXX commented Oct 26, 2018

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 });
})

@HackbrettXXX
Copy link
Member

Using import/export for the original sources will not really work, since we bundle some npm modules which use different ways of exporting their modules.

We could only replace those during the build step.

@yGuy
Copy link
Member

yGuy commented Oct 26, 2018

So the "new build script" broke it again? Which issue or commit is that?

@HackbrettXXX
Copy link
Member

On our side: yWorks/jsPDF@21df1eb

@HackbrettXXX
Copy link
Member

On their side:
parallax/jsPDF@b6b175a
parallax/jsPDF@48ca33c
parallax/jsPDF@ebe19ad

@yGuy
Copy link
Member

yGuy commented Oct 26, 2018

That looks like a huge mess to me. However the "node" part might be interesting. They are building a node compliant version that might work in an ES6 import case.

@HackbrettXXX
Copy link
Member

The node version doesn't even run on node :D
I had to exclude the promise polyfill first (uses window)

@HackbrettXXX
Copy link
Member

HackbrettXXX commented Oct 26, 2018

Ok, so I guess we have three options:

  • Fix the "new build script"
  • reroll to the old one (we would lose the node build versions)
  • write an entirely new build script

@yGuy
Copy link
Member

yGuy commented Oct 26, 2018

Do what suits you the most - the node versions would not work for us anyway with the SVG DOM not being available on node, as far as I can tell. If fixing is simple, go ahead. If you want to redo the thing using a more modern approach (I guess this is what they tried), feel free to write a new one. Maybe @fskpf wants to help?

HackbrettXXX added a commit to yWorks/jsPDF that referenced this issue Oct 26, 2018

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
(see svg2pdf issue yWorks/svg2pdf.js#59)
@HackbrettXXX
Copy link
Member

Fixed it now for es6: yWorks/jsPDF@2f18d0f

For AMD, it seems to be necessary to insert something like:

jsPDF = jsPDF['default']

Seems to be the drawback, when es6 support is also wished.

@micschro
Copy link
Member

Yes - as soon as both default and named exports are used, rollup will even warn about this:

(!) Mixing named and default exports
Consumers of your bundle will have to use bundle['default'] to access the default export, which may not be what you want. Use `output.exports: 'named'` to disable this warning

@wxfred
Copy link

wxfred commented Nov 6, 2018

Please support ES6 modules import, thx.

@GrayYoung
Copy link

Fixed it now for es6: yWorks/jsPDF@2f18d0f

For AMD, it seems to be necessary to insert something like:

jsPDF = jsPDF['default']

Seems to be the drawback, when es6 support is also wished.

Did not release a version.

npm ERR! notarget No matching version found for jspdf@2.0.0

@HackbrettXXX
Copy link
Member

You installed the wrong jsPDF:

npm install jspdf-yworks

@HackbrettXXX
Copy link
Member

The fix for es6 imports will come with 2.0.1

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants