jsPDF icon indicating copy to clipboard operation
jsPDF copied to clipboard

Used in AngularJS throws Cannot read property 'canvas' of undefined

Open randallto opened this issue 11 years ago • 16 comments

Uncaught TypeError: Cannot read property 'canvas' of undefined PNG.load._this @ jspdf.debug.js:15637 (anonymous function) @ jspdf.debug.js:15761

randallto avatar May 18 '15 17:05 randallto

version 1.0.272 works for me

jamescheuk91 avatar May 21 '15 08:05 jamescheuk91

The js code still works and runs for 1.0.272 but when you F12 in chrome or IE11 you see there seems to be some issue initializing this reported to console. I think it is only when using in AngularJs.

randallto avatar May 21 '15 17:05 randallto

I am getting exactly the same error, when using EmberJS (Ember-cli). The app just won't start.

v1.0.272 works.

*Edit: I just saw, that this error is also thrown in the basic.html sample in this repository.

Eusebius1920 avatar Jun 03 '15 10:06 Eusebius1920

I fixed my jspdf.debug.js by doing the following:

First I deleted lines 16868-16982.

Then I enclosed lines 15761-16205 with a try catch-handler:

try{
   // here lines 15761-16205
}
catch(err){
   console.log('That error occured again.');
}

Now at least my EmberJS-application started up again.

Eusebius1920 avatar Jun 03 '15 12:06 Eusebius1920

The problem is this line: if (!canvas)

It should be (typeof canvas !== 'object')

Flamenco avatar Jun 03 '15 14:06 Flamenco

I don't think that this is the problem, because the exception is caused by pdf being null/undefined.

html2pdf = function(html,pdf,callback) {
    var canvas = pdf.canvas; // pdf here is ===null or ===undefined. Exception is thrown here...
    if (!canvas) {
        alert('jsPDF canvas plugin not installed');
        return;
    }
    canvas.pdf = pdf;

        // other stuff

}

Eusebius1920 avatar Jun 03 '15 14:06 Eusebius1920

Yes. That's true. My local source file was off by 1 line...

Can you include a stack trace?

Flamenco avatar Jun 03 '15 14:06 Flamenco

See opening post:

PNG.load._this @ jspdf.debug.js:15637 (anonymous function) @ jspdf.debug.js:15761

This is the reason why I enclosed this anonymous function with a try catch block.

Eusebius1920 avatar Jun 03 '15 14:06 Eusebius1920

This fixes it: function html2pdf (html,pdf,callback){...}

instead of

html2pdf = function(html,pdf,callback)

And the file in lines 16869-16983 should probably not be included in jspdf.debug.js at all. This is an dependency definition file that used require.js and should be optional. I put if (typeof require === 'object') around that block.

Flamenco avatar Jun 03 '15 14:06 Flamenco

Thanks Flamenco. I will test that out.

randallto avatar Jun 03 '15 16:06 randallto

see https://github.com/MrRio/jsPDF/pull/529 for fix

Flamenco avatar Jun 18 '15 13:06 Flamenco

Hi, will this fix be embedded in the next release ? Thanks.

jondeodat avatar Jun 19 '15 15:06 jondeodat

pls release to bower as soon as possible

luxueyan avatar Jul 03 '15 16:07 luxueyan

bower please

kimlambiguit avatar Aug 05 '15 19:08 kimlambiguit

Hey, this fix causes errors in other plugins, such as summernote and switchery. TypeError: element.summernote is not a function at activate. Any ideas?

rodrigomtz avatar Aug 12 '15 16:08 rodrigomtz

This isn't in bower yet?

lisa-asl avatar Feb 25 '16 15:02 lisa-asl