简体   繁体   中英

Angular JS App and JSPDF not working in IE at all

I have an application in Angular JS and it is using JSPDF.

The problem is that generating the PDF works in Chrome, Firefox and Safari but not in IE at all. There, I get the following error:

SCRIPT438: Object doesn't support property or method 'trimLeft' 
jspdf.plugin.from_html.js, line 71 character 4

Code in js file for generating the PDF:

 function mainControl($scope) {

    $scope.showtooltip = false;
    $scope.value = 'Generate PDF';

    $scope.hideTooltip = function() {
      $scope.showtooltip = false;
    }

    $scope.toggleTooltip = function(e) {
      e.stopPropagation();
      $scope.value = 'HERE';
      $scope.showtooltip = !$scope.showtooltip;
    }
  }

  function printPDF() {

    var doc = new jsPDF();
    var elementHandler = {
      '#oi' : function(element, renderer) {
        return true;
      }
    };

    var source = window.document.getElementsByTagName("body")[0];

    doc.fromHTML($('#content').get(0), 10, 15, {
      'width' : 190
    });


    doc.setFont("helvetica");
    doc.setFontType("normal");
    doc.setTextColor(198, 0, 24);
    doc.setFontSize(10);
    doc.text(10, 10,  'xxxx');

    doc.output("dataurlnewwindow")  
  }

  var Show = {
    getFromPDF : function() {
      $('#btn-pdf').click(function() {
        printPDF();
      });
    }
  }

  $(function() {
    Show.getFromPDF();
  });

IE does not support the non-standard function trimLeft of String.prototype, for more info check this

Here is a workaround :

<!--[IF IE]>
<script type="text/javascript">
    if (typeof String.prototype.trimLeft !== 'function') {
        String.prototype.trimLeft = function() {
            return this.replace(/^\s*/,'');
        }
    }
</script>

Here is a working demo:

 if (typeof String.prototype.trimLeft !== 'function') { String.prototype.trimLeft = function() { return this.replace(/^\\s*/, ''); } } var myString = " YEBO zsdsad sadsad "; console.log("test"); console.log(myString.trimLeft()); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM