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.