Search code examples
javascriptjspdf

How do I use addHTML-plugin from jsPDF? (importing via script-tag is not an option)


I have a problem with jsPDF that relates to the plugin called addHTML.

I have installed jsPDF as a package via npm.

Installation:

npm install jspdf --save
npm install @types/jspdf

My current version of jspdf is 1.5.2.

When I run this code:

import jsPDF from "jspdf";

  function downloadPdfFunction() {
    const element = document.getElementById("My-HTML-div") as HTMLElement;
    const doc = new jsPDF("p", "mm", [element.scrollWidth, element.scrollHeight]);
    doc.addHTML(element, function callbackfunction() {
      doc.save("sample.pdf");
    });

I get this error in the console:

TypeError: doc.addHTML is not a function

I found several who asked similar questions, but it seems to me that they all solve it by using e.g.

<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.0.272/jspdf.debug.js"></script> 

which is not something I want to do.

In other words: It is not an option to import jspdf.debug.js (or any other file) in a script-tag - instead I want to have the jspdf installed from npm.

Any help is highly appreciated, as I have been trying to solve this issue for more than a week now.


Solution

  • So apparently, the solution is to downgrade from 1.5.2 to 1.4.1 if the package is used via npm.

    I found the answer in the comment to this post: https://stackoverflow.com/a/57030574/13793995.