Angular-jsPDF warnings, uncexpected tocken and jsPDF.fromHTM



我想用 Angular 打印 html 代码,但我不能。 我需要帮助,看。

ts.config.app.json

{
"extends": "./tsconfig.base.json",
"compilerOptions": {
"outDir": "./out-tsc/app",
"types": [],
"allowSyntheticDefaultImports": true
},
"files": [
"src/main.ts",
"src/polyfills.ts"
],
"include": [
"src/**/*.d.ts"
]
}

npm jspdf 模块

在此处输入图像描述

Angular.json 脚本

"scripts": [
"node_modules/jquery/dist/jquery.js",
"node_modules/popper.js/dist/umd/popper.js",
"node_modules/bootstrap/dist/js/bootstrap.js",
"node_modules/jspdf/dist/jspdf.es.min.js"
]

如果在类中我像这样导入import jsPDF from 'jspdf';或像这样import { jsPDF } from "jspdf";我会收到此警告和此错误。

警告:

WARNING in ./node_modules/jspdf/dist/jspdf.umd.min.js 195:141-151
Critical dependency: the request of a dependency is an expression
WARNING in ./node_modules/jspdf/dist/jspdf.umd.min.js 195:240-254
Critical dependency: the request of a dependency is an expression

错误:

scripts.js:18390 Uncaught SyntaxError: Unexpected token 'export'

如果我像这样导入JSPDF,import * as jsPDF from "jspdf";我会收到此错误:

错误:

Type 'typeof import("jspdf")' has no construct signatures.
const doc = new jsPDF('p', 'pt', 'letter');

这是我的班级:

//PDF
// import { jsPDF } from "jspdf";
import jsPDF from 'jspdf';
// import * as jsPDF from "jspdf";
@Component({
selector: 'app-cash-register',
templateUrl: './cash-register.component.html'
})
export class CashRegisterComponent implements OnInit {
@ViewChild("ticket") ticket: ElementRef;
public printPDF(): void {
var pdf = this.ticket.nativeElement.innerHTML;
const doc = new jsPDF('p', 'pt', 'letter');
const margins = {
top: 80,
bottom: 60,
left: 40,
width: 522
};
doc.html(pdf, {
callback: function(doc) {
doc.save("Test1.pdf");
}
})
// or

doc.fromHTML(pdf, margins.left, margins.top, {}, function () {
doc.save("Test2.pdf");
}, margins);
}
}

如果我没有在 Angular.json 中导入 JSPDF,当我尝试下载时会出现此错误:

core.js:4197 ERROR TypeError: doc.fromHTML is not a function

我没有一个好的解决方案,但这就是我让它工作的方式。看起来jspdf 2.0.0在角度10中抛出"doc.fromHTML不是一个函数"(我不确定它是否适用于以前的Angle版本。

我使用

npm uninstall jspdf

然后我安装了以前的版本,即:

npm install jspdf@1.5.3

工作示例:

import * as jsPDF from 'jspdf';

var doc = new jsPDF('p', 'pt', 'letter');
var imgData =
'data:image/png;base64,addAnImageData';
var specialElementHandlers = {
'#idDiv': function (element, renderer) {
return true;
}
};
doc.addImage(imgData, 'png', 0, 250, 615, 200);
let source = document.getElementById("idDiv");
doc.fromHTML(source, 0, 0, {
'elementHandlers': specialElementHandlers
});
doc.save('card.pdf');

doc.fromHTML()问题也发生在Angular 7.x上。如果降级到 jsPDF 1.5.3,它可以正常工作。

最新更新