我正在尝试使用jsPDF从现有的div创建并下载PDF。
我尝试了许多方法,但是无法实现。
我的最后一次尝试是使用@ViewChild
和ElementRef
,但也不起作用。
detail-devis.component.ts:
import {Component, ElementRef, ViewChild} from "angular2/core";
declare let jsPDF;
@Component({
selector: 'my-app',
template: `<div #test>
Hello world
</div>
<button type="button" (click)="test()">pdf</button>
<button (click)="download()">download</button>`
})
export class App {
@ViewChild('test') el: ElementRef;
constructor() {
}
public download() {
var doc = new jsPDF();
doc.text(20, 20, 'Hello world!');
doc.save('Test.pdf');
}
public test() {
let pdf = new jsPDF('l', 'pt', 'a4');
let options = {
pagesplit: true
};
pdf.addHTML(this.el.nativeElement, 0, 0, options, () => {
pdf.save("test.pdf");
});
}
}
有人知道在Angular 2中实现这个最简单的方法吗?
() => { ... }
要优于function() { ... }
,因为在函数内部this
将指向当前组件,否则将指向pdf
(或者函数被调用的位置)。 - Günter Zöchbauer