简体   繁体   English

如何消除在angular2中显示pdf视图的延迟

[英]How to remove the delay in displaying pdf view in angular2

i am using jspdf and jspdf-autotable to display the json data in pdf view mode,but the pdf view mode is displaying in 2nd click due to delay. 我正在使用jspdf和jspdf-autotable在pdf查看模式下显示json数据,但由于延迟,pdf查看模式在第二次单击中显示。 Can anyone help me to solve this. 谁能帮我解决这个问题。

Here is working DEMO 这是工作演示

HTML: HTML:

<iframe id="convertToPdf" type="application/pdf"  width="100%" height="100%" *ngIf="displayTable"></iframe>

TS: TS:

captureScreen() {
    this.displayTable = true;
    var doc = new jsPDF();
    var col = ["year", "budget", 'exclude', 'expenses'];
    var rows = [];
    for (var i = 0; i < this.items.length; i++) {
      var temp = []
      for (var key in this.items[i]) {
        temp.push(this.items[i][key])
      }
      rows.push(temp);
    }
    doc.autoTable(col, rows);
    document.getElementById("convertToPdf").setAttribute('src', doc.output('datauri'))
  }

If you see the console in your demo, you will see an error. 如果您在演示中看到控制台,则会看到错误。 The iframe need to be placed before it is used. 使用iframe之前需要先放置它。

You can do this without *ngIf 您可以不使用* ngIf

<iframe id="convertToPdf" type="application/pdf"  width="100%" height="100%" [ngStyle]="{display: (displayTable ? 'block' : 'none')}"></iframe>

Add little timeout on document operation on last line. 在最后一行的document操作上添加一点超时。 This will make sure that this line will execute in the last after above statements. 这将确保该行将在上述语句之后的最后一行中执行。

    setTimeout(() => {
      document.getElementById("convertToPdf").setAttribute('src', doc.output('datauri'))
    }, 10)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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