简体   繁体   English

在angular2项目中使用jspdf导出动态生成的div的问题

[英]Issues on Exporting dynamically generated div using jspdf in angular2 project

I'm using jspdf to print div s in my project. 我正在使用jspdf在项目中打印div I recently realized that this doesn't print when a div includes some dynamic contents. 我最近意识到,当div包含一些动态内容时,此命令不会显示。 What I mean is that if there is a simple angular if statement, jspdf doesn't print it. 我的意思是,如果有一个简单的有角if语句,jspdf不会打印它。 Here is a sample: 这是一个示例:

In my HTML: 在我的HTML中:

<div id="content" #content>
   <p>This sections is printed</p>
   <p> {{ item.id }} is also printed</p>
</div>

This code below throws error which can be found below the html code: 下面的代码引发错误,可以在html代码下面找到该错误:

<div id="content" #content>
   <p *ngIf="item.id === 'sample'">this is NOT printed</p>
</div>

ERROR TypeError: Cannot read property '1' of undefined at f.renderParagraph (jspdf.min.js:97)... 错误TypeError:无法读取f.renderParagraph(jspdf.min.js:97)上未定义的属性'1'...

here is the typescript code I use: 这是我使用的打字稿代码:

@ViewChild('content') content: ElementRef;    
public downloadPDF () {
    const doc = new jsPDF();
    const specialElementHandlers = {
      '#editor': function(element, renderer) {
        return true;
      }
    };
    const content = this.content.nativeElement;
    doc.fromHTML(content.innerHTML, 15, 15, {
      'width': 190,
      'elementHandlers': specialElementHandlers
    });

    const fileName = 'test';
    doc.save(fileName + '.pdf');
}

Again the first div prints as pdf just fine. 同样,第一个div打印为pdf就好了。 But the second div doesn't. 但是第二个div没有。 I suspect that the issue is angular if statement. 我怀疑这个问题与if陈述有关。 But I couldn't find a solution yet. 但是我还没有找到解决方案。 Any help would be appreciated. 任何帮助,将不胜感激。

Instead of using Structural directive Try This 代替使用结构指令尝试

Hidden Property Binding 隐藏属性绑定

<div id="content" #content>
   <p [hidden]="item.id !== 'sample'">this is NOT printed</p>
</div>

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

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