javascript - var not affected outside of function? -



javascript - var not affected outside of function? -

i trying convert html pdf using jspdf. however, variable , things happen within function seem invisible outside of function. here code:

$(document).ready(function() { $("#runpdf").click(function(event) { var doc = new jspdf(); var imagedata; html2canvas($("#page1"), { logging:true, profile:true, allowtaint:true, letterrendering: true, onrendered:function(canvas) { imagedata= canvas.todataurl("image/jpeg"); doc.addimage(imagedata, 'jpeg', 0, 0, 200, 200); } }); doc.save('test.pdf'); }); });

upon running this, blank page rendered, notably within function html2canvas did not impact var doc. however, upon putting doc.save('test.pdf'); bit within function (after doc.addimage()), executes fine page beingness rendered. however, cannot because going utilize for-each loop execute html2canvas function multiple times on multiple pages , @ end, save document. won't work because seems doc.save() needs in same function rest. how can avoid problem?thanksedit: problem fixed using counter , simple if statement.

var doc = new jspdf("p", "pt", "letter"); $(document).ready(function () { $("#runpdf").click(function (event) { $(document.body).width(1903); var count=0; $("section").each(function() { $(this).children('footer').children('article').append($(document.createelement('span')).text((count+1)+".").css("float","right").css("font-weight", "900").css("font-size","150%")); count++; }); var pages = $(".page5"); var remaining = pages.length; pages.each(function () { html2canvas($(this), { logging: true, profile: true, allowtaint: true, letterrendering: true, onrendered: function (canvas) { var imagedata = canvas.todataurl("image/jpeg"); doc.addimage(imagedata, 'jpeg', -425, 0, 1450, 800); remaining--; if (remaining === 0) { doc.save('test.pdf'); } doc.addpage(); } }); }); }); });

help, everybody

you need set save after you've added image:

html2canvas($("#page1"), { logging:true, profile:true, allowtaint:true, letterrendering: true, onrendered:function(canvas) { imagedata= canvas.todataurl("image/jpeg"); doc.addimage(imagedata, 'jpeg', 0, 0, 200, 200); doc.save('test.pdf'); } });

the onrendered function callback , executed 1 time image rendered. in code save beingness called prior image beingness added.

javascript jquery html variables jspdf

Comments

Popular posts from this blog

model view controller - MVC Rails Planning -

ruby on rails - Devise Logout Error in RoR -

html - Submenu setup with jquery and effect 'fold' -