I am implementing jsPdf example and have sample html project in which i included jquery and jspdf cdn link to generate pdf. Here is my code
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Page Title</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<script> src = ".3.4/jspdf.min.js"</script>
<script src=".2.4.min.js"></script>
</head>
<body>
<div id="content">
<p>Testing testing testing</p>
</div>
<button type="button" id="export">Export To Pdf</button>
<script>
var doc = new jsPDF();
$('#export').click(function () {
doc.fromHTML($('#content').html(), 15, 15, {
'width': 170
});
doc.save('sample-file.pdf');
});
</script>
</body>
</html>
I am implementing jsPdf example and have sample html project in which i included jquery and jspdf cdn link to generate pdf. Here is my code
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Page Title</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<script> src = "https://cdnjs.cloudflare./ajax/libs/jspdf/1.3.4/jspdf.min.js"</script>
<script src="https://code.jquery./jquery-2.2.4.min.js"></script>
</head>
<body>
<div id="content">
<p>Testing testing testing</p>
</div>
<button type="button" id="export">Export To Pdf</button>
<script>
var doc = new jsPDF();
$('#export').click(function () {
doc.fromHTML($('#content').html(), 15, 15, {
'width': 170
});
doc.save('sample-file.pdf');
});
</script>
</body>
</html>
But it is giving this error
Uncaught ReferenceError: jsPDF is not defined
at index.html:19
What's wrong in my code?
Share Improve this question asked Jan 3, 2019 at 9:55 Fahad SubzwariFahad Subzwari 2,3653 gold badges28 silver badges63 bronze badges 1- When importing a script, it will export the variable/reference you may use to work with (i.e. jsPDF). Make sure it's imported correctly and keep note of script loading via head and body. I would remend usage of webpack or requirejs to avoid cluttering of the global namespace. – Koshux Commented Jan 3, 2019 at 13:34
2 Answers
Reset to default 4The error is in this line-
<script> src = "https://cdnjs.cloudflare./ajax/libs/jspdf/1.3.4/jspdf.min.js"</script>
src should be inside the script tag
<script src = "https://cdnjs.cloudflare./ajax/libs/jspdf/1.3.4/jspdf.min.js"></script>
For exporting and rendering
$('#export').click(() => {
var pdf = new jsPDF('p','pt','a4');
pdf.addHTML(document.body,function() {
pdf.save('web.pdf');
});
})
fix your script tag of https://cdnjs.cloudflare./ajax/libs/jspdf/1.3.4/jspdf.min.js to
<script src="https://cdnjs.cloudflare./ajax/libs/jspdf/1.3.4/jspdf.min.js"></script>