简体   繁体   中英

Saving pdf with jsPDF gives error: Cannot read property 'elementHandlers' of undefined

I want to automatically save certain webpages as pdf. For that I came across jsPDF . The code on target page was not working. So I created a dummy code:

$.getScript('https://cdnjs.cloudflare.com/ajax/libs/jspdf/0.9.0rc1/jspdf.min.js');

var doc = new jsPDF();

var pdfStr = "<div><div>Mahesh</div><img src=\"https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png\" /></div>"

//creating dom from string
var div =  document.createElement('div');
div.innerHTML = pdfStr;
var pdfHtml = div.childNodes;
doc.fromHTML(pdfHtml);
doc.save('pdfjsdemo.pdf');

I am copy pasting following code in Chromes console and hitting Enter. It is giving me:

Uncaught TypeError: Cannot read property 'elementHandlers' of undefined
    at Object.f.fromHTML (jspdf.min.js:75)
    at <anonymous>:11:5

I used dom string in format <div><div></div><img></img></div> because thats how my target page looks like.

Besides, it works, if I use

doc.text("<div>mahesh</div>");

saving desired pdf.

So whats going on here?

There seems to be many similar threads on github. Is it not possible. Or is their better alternative to jspdf?

div.childNodes returns an array - you need to get it's first element.
Replace var pdfHtml = div.childNodes; with var pdfHtml = div.childNodes[0];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM