Francisco Fernandez
Francisco Fernandez

Reputation: 841

doc.fromHTML is not a function jsPDF

Hello Im trying to save my html page to PDF and I have this code:

 <div id="content">
<h3>Hello, this is a H3 tag</h3>

<p>a pararaph</p>
</div>
<div id="editor"></div>
<button id="cmd">Generate PDF</button>

<script>
var doc = new jsPDF();
var specialElementHandlers = {
    '#editor': function (element, renderer) {
        return true;
    }
};

$('#cmd').click(function () {   
    doc.fromHTML($('#content').html(), 15, 15, {
        'width': 170,
            'elementHandlers': specialElementHandlers
    });
    doc.save('sample-file.pdf');
});

// This code is collected but useful, click below to jsfiddle link.
</script>

I uploaded to jsFiddle and works... but when I try it in my browser doesnt works and I get the error doc.fromHTML is not a function. I have the jQuery and jsPDF references exactly like jsFiddle.

Example Code

Upvotes: 13

Views: 54016

Answers (3)

Hakan
Hakan

Reputation: 597

The fromHTML function was replaced by html and you have to add html2canvas manually.

Just add this line:

<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/html2canvas.min.js"></script>

Working Example:

window.jsPDF = window.jspdf.jsPDF;
var doc = new jsPDF();
var pdf_el=document.getElementById('pdf');
doc.html(  pdf_el , {x:20, y:75,maxWidth:200 , callback: function(doc_e){
    doc_e.save("bbb.pdf");
}});

Upvotes: 1

Alexa
Alexa

Reputation: 390

The fromHTML function was replaced by html in 2.0.0. Unfortunately, the typings currently still contain the fromHTML method but a fix is under way.

Upvotes: 26

Naga Sai A
Naga Sai A

Reputation: 10975

Add External libraries - Jquery and Jspdf js files with script tags

<script src="https://code.jquery.com/jquery-1.12.3.min.js"></script>


<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/0.9.0rc1/jspdf.min.js"></script>

Codepen - http://codepen.io/nagasai/pen/JKKNMK

Upvotes: 4

Related Questions