angular - doc.fromHTML 不是 angular 6 的函数
问题描述
我想将模板 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。
解决方案
从您的代码中删除我评论的内容。要导出为 pdf,您需要使用save function
doc.save("export.pdf");
app.component.ts
import { Component, ViewChild, ElementRef } from '@angular/core';
// import { log } from 'util'; ** REMOVE **
// import * as jsPDF from 'jspdf'; ** REMOVE **
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(() => { ** REMOVE **
doc.fromHTML(content.innerHTML, margins.left, margins.top, {}, function () {
// doc.output('export.pdf'); ** REMOVE **
doc.save("export.pdf");
}, margins);
// }, 100); ** REMOVE **
}
}
角.json
"scripts": [
"./node_modules/jspdf/dist/jspdf.min.js",
// "./node_modules/jspdf/dist/jspdf.debug.js" ** REMOVE **
]
索引.html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>ExportsPdf</title>
<base href="/">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" type="image/x-icon" href="favicon.ico">
<!-- ** REMOVE ** <script src="https://code.jquery.com/jquery-1.12.3.min.js"></script>
<script type="text/javascript" src="https://code.jquery.com/jquery-2.1.3.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.3.2/jspdf.debug.js"></script> -->
</head>
<body>
<app-root></app-root>
</body>
</html>
推荐阅读
- android - 如何在 Firebase 中动态更改推送通知 (FCM) 的内容
- html - 图像未正确与另一张图像重叠
- ios - 为什么选择选择器选项后,我的 SwiftUI 页面标题会发生变化?
- java - 对象值不改变 Java 数组
- c++ - C++20 中的用户定义推导指南
- javascript - React Native 样式表在刷新之前不起作用
- javascript - 在 pool.query 中使用 node.js pool.query
- laravel - Laravel 的疑惑
- flutter - 如何使用 Flutter 过滤 Firestore 文档?
- android - 有没有办法让 textview 避免另一个?