1

I am trying to generate a pdf of a page that I have rendered in the browser. I have a button which you can click but it generates an error. Can anyone help me out please

Lots of jsPDF questions (some with answers) but not this one, any ideas. Not doing anything clever. Just a straight copy/paste from the examples.

My html

<button id="cmd">generate PDF</button> <div class="row" id="dashb"> <div class="col-md-6"> <h1>Hello</h1> <p>World</p> </div> </div> <link rel="stylesheet" href="/Content/vis/css/vis.css" /> <link href="~/Content/css/leaflet120.css" rel="stylesheet" /> <link href="~/Content/css/leaflet07.css" rel="stylesheet" /> <script src="https://code.jquery.com/jquery-1.12.0.min.js"></script> <script src="~/Content/js/leaflet120.js"></script> <script src="~/Content/js/d3.v4.min.js"></script> <script src="~/Content/js/leaflet07.js"></script> <script src="/Content/vis/js/wrap.js"></script> <script src="/Content/vis/js/d3tip.js"></script> <script src="/Content/vis/js/d3pie.js"></script> <script src="/Content/vis/js/vis.columnChart.js"></script> <script src="/Content/vis/js/vis.lineChart.js"></script> <script src="/Content/vis/js/vis.pieChart.js"></script> <script src="/Content/vis/js/vis.circleMap.js"></script> <script src="/Content/vis/js/vis.js"></script> <script src="~/Scripts/jspdf.debug.js"></script> <script src="~/Scripts/html2canvas.min.js"></script> <script> $(function () { var specialElementHandlers = { '#editor': function (element, renderer) { return true; } }; $('#cmd').click(function () { var doc = new jsPDF(); doc.fromHTML( $('#dashb').html(), 15, 15, { 'width': 170, 'elementHandlers': specialElementHandlers }, function () { doc.save('sample-file.pdf'); } ); }); }); </script> 

This is the error

Uncaught TypeError: element.className.split is not a function at elementHandledElsewhere (jspdf.debug.js:7215) at DrillForContent (jspdf.debug.js:7426) at DrillForContent (jspdf.debug.js:7427) at DrillForContent (jspdf.debug.js:7427) at DrillForContent (jspdf.debug.js:7427) at jspdf.debug.js:7595 at done (jspdf.debug.js:7470) at Image.img.onerror.img.onload (jspdf.debug.js:7492) 

This is my code

<script> $(function () { var specialElementHandlers = { '#editor': function (element, renderer) { return true; } }; $('#cmd').click(function () { var doc = new jsPDF(); doc.fromHTML( $('#dashb').html(), 15, 15, { 'width': 170, 'elementHandlers': specialElementHandlers }, function () { doc.save('sample-file.pdf'); } ); }); }); </script> 

1 Answer 1

2

You need to move your click() event handler up to the beginning of the function. I included this fiddle showing it working.

$('#cmd').click(function () { var specialElementHandlers = { '#editor': function (element, renderer) { return true; } }; var doc = new jsPDF(); doc.fromHTML( $('#dashb').html(), 15, 15, { 'width': 170, 'elementHandlers': specialElementHandlers }, function () { doc.save('sample-file.pdf'); } ); }); 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.