jsPDF Tutorial to Download a Div With CSS in a HTML5 Page as PDF Document in Browser Using Javascript Full Project For Beginners

 

 

You can do it using jsPDF

 

HTML:

 

 

index.html

 

 

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

    <p>A paragraph</p>
</div>
<div id="editor"></div>
<button id="cmd">generate PDF</button>

 

 

JavaScript:

 

 

script.js

 

 

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');
});

Leave a Reply