mahadev sutar mahadev sutar - 1 year ago 118
AngularJS Question

How to create Html to pdf on particular div onclick button in angularjs?


I am new to angularjs i want to create pdf of particular div onclick
button. currently im having one code that is works on page load. If i
refresh page it will automatically download that particular div. But i
want that if i click on button then only div will download. this is
my code for download div . please help me with the same. i have added necessary scripts of html2canvas etc.


html2canvas(document.getElementById('exportthis'), {
onrendered: function (canvas) {
var data = canvas.toDataURL();
var docDefinition = {
content: [{
image: data,
width: 500,
}]
};
pdfMake.createPdf(docDefinition).download("Score_Details.pdf");
}
});

Answer Source

You should create a function in your controller, and add a click event to a button.

Your view:

<input type="submit" ng-click="createPdf()" value="Create PDF"/>

Your controller:

$scope.createPdf = function () {

    html2canvas(document.getElementById('exportthis'), {
        onrendered: function (canvas) {
            var data = canvas.toDataURL();
            var docDefinition = {
                content: [{
                    image: data,
                    width: 500,
                }]
            };
            pdfMake.createPdf(docDefinition).download("Score_Details.pdf");
        }
    }); 
};
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download