【发布时间】:2019-05-27 01:59:02
【问题描述】:
我想将模板 html 导出为 pdf,我正在尝试将 jsPDF lib 与 angular 一起使用,但出现此错误 doc.fromHTML is not a function。
import { Component, ViewChild, ElementRef } from '@angular/core';
import * as jsPDF from 'jspdf';
// declare var jsPDF: any;
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'exports-pdf';
@ViewChild('exports') exports: ElementRef;
public print(): void {
const doc = new jsPDF('p', 'pt', 'letter');
const content = this.exports.nativeElement;
const margins = {
top: 80,
bottom: 60,
left: 40,
width: 522
};
console.log(doc);
setTimeout(() => {
doc.fromHTML(content.innerHTML, margins.left, margins.top, {}, function() {
doc.output('export.pdf');
}, margins);
}, 100);
}
}
我也试过用import * as jsPDF from 'jspdf'; or declare var jsPDF: any;
当我使用declare var jsPDF: any 时,我在控制台上拥有所有 jsPDF 属性,但是当我单击按钮时没有任何反应。当我使用import * as jsPDF from 'jspdf' 时,我有一个没有任何属性的对象,如下图:
在 angular.json 中,我将导入的脚本放入脚本中
"scripts": [
"./node_modules/jspdf/dist/jspdf.min.js",
"./node_modules/jspdf/dist/jspdf.debug.js"
]
错误还在继续。
我发现其他有相同错误但没有解决方案的家伙。我不知道这是否是一个 lib 错误,或者如果使用 angular 它不能很好地工作。
我将代码上传到GitHub。
【问题讨论】:
标签: angular angular6 jspdf export-to-pdf