问题
I wanna export a template html to pdf and I'm trying use jsPDF lib with angular, but o got this error 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);
}
}
I tried too to use import * as jsPDF from 'jspdf'; or declare var jsPDF: any;
when I use declare var jsPDF: any
, I have at the console all jsPDF properties, but nothing happen when I click on button. When I use import * as jsPDF from 'jspdf'
i have an object without any properties like this image:
In angular.json i put in scripts the imports
"scripts": [
"./node_modules/jspdf/dist/jspdf.min.js",
"./node_modules/jspdf/dist/jspdf.debug.js"
]
and the error goes on.
I found other dudes with the same error but without solution. I don't know if it is a lib bug or if with angular it not work very well.
I uploaded the code to GitHub.
回答1:
Remove what I commented from your code. To export as pdf you need to use 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 **
}
}
angular.json
"scripts": [
"./node_modules/jspdf/dist/jspdf.min.js",
// "./node_modules/jspdf/dist/jspdf.debug.js" ** REMOVE **
]
index.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>
来源:https://stackoverflow.com/questions/53972144/doc-fromhtml-is-not-a-function-with-angular-6