Jspdf: does not load from requirejs

Created on 30 Sep 2016  路  28Comments  路  Source: MrRio/jsPDF

Just noticed this bug when the version 1.3.0 release went out. When loading jsPDF.min.js with requirejs, it just doesn't work. The minified file is not properly wrapped for loading via requirejs or other module loaders.

Needs example

Most helpful comment

Hi,

On my side I manually fix the generated build.
The fix is just to correct all the place where define() function is called without any name.

for example
define([], function() { return saveAs; });
to replace with
define("saveAs", [], function() { return saveAs; });

I've found 3 places in the code (I've pade my path on version 1.3.2):

  • line 10090: "saveAs"
  • line 10106: "adler32cs"
  • line 12508 (be carefull it's a verly long line): "html2canvas"

You can fix it on your side and try it. It works for me.
I can send you my patched file in MP if you want.

All 28 comments

Thanks for reporting! We did change the whole build so this is possible, the amd stuff looks to still be in there. Can you knock up a jsbin?

Yeah-- Working on it. I think the problem may lie in a second round of requirejs optimization.

Thanks @larkin :)

Too complicated to reproduce, only experiencing it when we build for production (uglify/etc), or when we load it in our test runner (karma + requirejs), but I created a new project with karma and jspdf and can't reproduce it there. Going to close for now.

I see the same issue - the problem is that the bundled file is totally incompatible with requirejs because of the multiple "define(" calls it is doing. You cannot use jspdf together with requirejs in the current state. There was a hack previously in the build file that removed these defines to enable requirejs usage, but the hack was disabled in this ominous changeset: https://github.com/MrRio/jsPDF/commit/2cec2716d3fa4cc3b5e293670091988cb51eba02#diff-efd59cd1bdc5c9ac6d0eaa368f1e149f
@simonbengtsson - do you remember why you removed that code? The commit says this actually enables the use of requirejs, but I would say it actually disables proper support.

What is your setup? I tried this now which works:

require(['libs/jspdf.min'], function(jsPDF) {
    var doc = new jsPDF();
    doc.text(20, 20, "Hello");
    doc.save("table.pdf");
});

Loading a single required module works (because jspdf is the last "define" in jspdf), however if you load more than a single dependency (which is where the AMD system really shines) it breaks - e.g. if you require angular, d3 and jspdf, you are likely to get the three defines elements inside jspdf (adler32c, filesave, and jspdf), but neither d3, nor angular - the worst thing about that is that requirejs doesn't warn you about that and that it's non-deterministicly (depending on the load order of the files).

As you say there were no warnings so I must have overlooked it. I'll submit a PR in a moment reverting that change.

Is it possible to generate a new build with this fix ?
I'm stuck with this defect. It fully block my one page app... All define calls done after jsPDF one are broken.

This might explain an issue I've been trying to fix for an entire day now, new build would be greatly appreciated.

I am having the same issue .. I am using requireJS and on a fairly large app. Everything was loading smoothly until I include jsPdf and jsPdf-autotable in the mix. Once I included them, the app started to load sporadically with load errors and functions and variables that seemed to access jsPdf rather then the intended defined module, resulting in "function not defined" errors. When I debugged setting a breakpoint, I saw this clearly happening.

Please fix or explain a temporary work-around

Thanks for a great lib if I can get to use it :)

Hi,

On my side I manually fix the generated build.
The fix is just to correct all the place where define() function is called without any name.

for example
define([], function() { return saveAs; });
to replace with
define("saveAs", [], function() { return saveAs; });

I've found 3 places in the code (I've pade my path on version 1.3.2):

  • line 10090: "saveAs"
  • line 10106: "adler32cs"
  • line 12508 (be carefull it's a verly long line): "html2canvas"

You can fix it on your side and try it. It works for me.
I can send you my patched file in MP if you want.

In the 1.3.3 I only had to replace the "saveAs" and it worked for me. Thanks @paztis

Hi

It seems it's still not corrected...
I always need to patch the lib to work...

I alos noptice in your generate"d file the code of html2canvas 0.5.0-beta3 is included (line 12741), but I don't know why.
Can you correct it ?

Thanks

Hi, I am using 1.3.4 with your "suggested fix".
I replaced the
define([], function() { return saveAs; });
with
define("saveAs", [], function() { return saveAs; });

but it doesn-t worked.
any other suggestions please?

I confirm paztis of 17 mar : correct all the places where define function is called without any name.

I did not find the lines that "paztis of 17 mar" suggested, however I found this at line 12482:

define([],e)

and I changed it into this:

define("e",[],e);

This works fine for me now.

Fixed by replacing

define([],t);

to this

define("e",[],t);

What is your setup? I tried this now which works:

require(['libs/jspdf.min'], function(jsPDF) {
    var doc = new jsPDF();
    doc.text(20, 20, "Hello");
    doc.save("table.pdf");
});

Got undefined when I tried this...

IS there any working example to use jsPDF along with requirejs ?

Hi, I'm also trying to use jspdf inside of a RequireJS based solution with similar code as @shivarajnaidu uses. The jsPDF variable is undefined for me also...
Any hints on how to get jsPDF working with RequireJS would be appreciated :)

Needs example

Seems like v1.5.0 is the latest release playing nice with requirejs, after that it looks like the module definition changed, making the constructor undefined when requiring jsPDF.

Hopefully that helps some of you!

Should work in the next release.

Hi,
@Uzlopak issue is still open in current v1.5.3. Do you know if there is a new release or newer build planned in the near future? As @azefiel mentioned v1.5.0 was last release working with requirejs.

Hi @azefiel,
have you or any one found a workaround?

Check the yWorks fork. It is a much improved version of this one and has this and a number of other issues fixed. There's even a pull request into this repo. But the recent maintainer seems to be on vacation, here.

@yGuy thanks! Will have a look.

Hi @azefiel,
have you or any one found a workaround?

I ended up using pdfmake and render the PDF directly.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

MelanieCroce picture MelanieCroce  路  4Comments

NoFootDancer picture NoFootDancer  路  3Comments

arulmb0136 picture arulmb0136  路  4Comments

glaier picture glaier  路  3Comments

allenksun picture allenksun  路  3Comments