Property 'fromHTML' does not exist on type 'jsPDF

Viewed 6333

Property 'fromHTML' does not exist on type jsPDF I am trying to use jsPDF to convert an HTML code to PDF. I've seen that the best way to do this is to use the jsPDF.fromHTML() function but when I try I get the following: Property 'fromHTML' does not exist on type 'jsPDF' I am using Angular 10 and jsPDF@2.3.1. I already tried

2 Answers

Well, that's because .fromHTML() has been deprecated in favor of .html() as seen in the documentation. jsPDF documentation html()

From my point of view this method does not exist. I see two ways:

if your html code contains the style information

const doc = new jsPDF('p', 'pt', 'a4');
const div = ...your code to get the html
await doc.html(div);
doc.save('test.pdf'); // save / download
doc.output('dataurlnewwindow'); // just open it

or if the style is defined somewhere else (as usually in angular application). you html2canvas and extract an image from canvas to add to pdf.

const doc = new jsPDF('p', 'pt', 'a4');
const div = ...your code to get the html
await html2canvas(... your element ...).then(canvas => {
// Few necessary setting options
const imgWidth = 208; // your own stuff to calc the format you want
const imgHeight = canvas.height * imgWidth / canvas.width; // your own stuff to calc the format you want
const contentDataURL = canvas.toDataURL('image/png');
doc.addImage(contentDataURL, 'PNG', 0, 0, imgWidth, imgHeight);
doc.save('test.pdf'); // save / download
doc.output('dataurlnewwindow'); // just open it
Related