Wednesday, 12 October 2016

Download a div in a HTML page as pdf using javascript

You can do it using jsPDF

OR

<script type="text/javascript" src="https://cdn.jsdelivr.net/jspdf/1.3.2/jspdf.min.js"></script>

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



OUTPUT EXAMPLE

Hemant Vishwakarma - Programming Blog, Tutorials, jQuery, Ajax, PHP, MySQL Code

Software & Website Developer Expert in Core PHP, Yii, Wordpress, Codeignter,
Laravel, Bootstrap, javaScript, CSS,Ajax etc. Web design / Development






Download a div in a HTML page as pdf using javascript

No comments:

Post a Comment