Search code examples
htmlangularjshtml-to-pdf

How to export angular Modal to the pdf using angularjs?


I am having an angular Modal and I want a button on the Modal named export to pdf which will export my modal content to the pdf file.

I have searched through but did not get any solution.

I have 5 modals and I want only one generic function to be used. Please help me with this. Thanks in advance


Solution

  • Here is what I came accross,

    HTML Modal:

    <div  class="modal-body" id="pdfPrakrutiId"  style="height:480px;overflow: auto;">
                         <div ng-include="'../prakrutiVP.html'"></div>
                    </div>
                    <div class="modal-footer" style="margin-bottom:5px;margin-right:10px;">
                        <button type="button" class="btn btn-default" ng-click="closeSecond()">{{'prakruti.common.close' | translate}}</button>
                      <button type="button" id="btn" class="btn btn-default" ng-click="exportToPdf()">Export to pdf</button>
                    </div>
    

    JS Code

    $scope.exportToPdf = function (value) {
    /*Code to generate pdf*/
    var pdf = new jsPDF('p', 'pt', 'letter');
    var specialElementHandlers = {
        '#bypassme': function(element, renderer) {
          return true;
        }
      };
      var margins = {
        top: 50,
        bottom: 60,
        left: 60,
        right:20,
        width: 522,
      };
      pdf.fromHTML(
        $("#pdfPrakrutiId").html(),
          margins.left, 
          margins.top, 
          {
            'width': margins.width,
            'elementHandlers': specialElementHandlers
          },
        margins
      );
      pdf.save('my_prakruti.pdf');
    /*Code to generate pdf ends here*/  
    

    };