我試圖創建和從jsPDF從現有的div下載PDF。創建並下載PDF格從角2 2
我嘗試了很多方法,但是我無法實現。
我最後一次嘗試與@ViewChild
和ElementRef
但不工作。
細節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");
});
}
}
有人知道與角2實現這一目標的最簡單的方法?
「無法達到」是什麼意思?任何錯誤消息?通常使用'()=> {...}'優於'function(){...}',因爲函數內this將指向當前組件,否則指向'pdf'(或者無論哪個函數被調用) –