Using an external library for converting to pdf - undefined error

I have been using the Plumsail export to pdf (JavaScript built in feature) for some time as advised in a previous post, plus using some help from this link:
https://plumsail.com/docs/forms-web/how-to/export-to-pdf-setup.html

However, the pdf output doesn't look good as our forms are big and complicated.
I am looking into other options like using external JS libraries to convert to pdf like: html2pdf and jsPDF

I have referenced external JS libraries before using the require method, and it was working fine. However, with these 2 libraries, I am getting an error like: "html2pdf is not defined" or "jsPDF is not defined".

A simple code which is not working in a Plumsail form JS:

require(["https://cdnjs.cloudflare.com/ajax/libs/html2pdf.js/0.10.1/html2pdf.bundle.min.js"], function(){
    $('#exportPDF').click(function(){
        var element = document.getElementsByClassName('page-template')[0];
        html2pdf(element);
    });
});

Please note I tried this code in an external html/JS file and it worked fine, only in Plumsail I get this error.

Please advise.

Hi @Mai,

Please try using requirejs.config() and require() like so:

// Load the html2pdf.js library
requirejs.config({
    paths: {
        html2pdf: "https://cdnjs.cloudflare.com/ajax/libs/html2pdf.js/0.10.1/html2pdf.bundle.min"
    }
});

// Execute when the form is rendered
fd.spRendered(() => {
     require(['html2pdf'], function(html2pdf) {
         const element = document.querySelector('.exportToPDF');
        if (element) {
            html2pdf(element);
        } else {
            console.error('Element with class "exportToPDF" not found.');
        }
     });
});

If you want to use a different library, note not to include the .js file extension in the requirejs.config() statement.

Let me know if this helps!