Generate PDF from HTML using JQuery and jsPDF

In some applications, there can be a scenario to generate pdf from current page. It is possible using JQuery and jsPDF.

Lets check sample code for generating PDF

Add libraries- JQuery and jsPDF

<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>

Sample HTML to download

<div id="content">
    <h3>Sample h3 tag</h3>
    <p>Sample pararaph</p>
</div>
<button id="cmd">Generate PDF</button>

Script to generate and download PDF from HTML

var doc = new jsPDF();

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

Find working sample code from the codepen URL - https://codepen.io/nagasai/pen/JKKNMK