Enqueue jsPDF in wordpress

36 Views Asked by At

I enqueue my jsPDF and the file which is dependent on it as

wp_enqueue_script('jsPDF','https://cdnjs.cloudflare.com/ajax/libs/jspdf/2.5.1/jspdf.umd.min.js', array(), '2.5.1', false);
wp_enqueue_script('my-plugin-pro', plugins_url('assets/js/my-plugin-pro.js', dirname(__FILE__)), array('jsPDF'), MY_PLUGIN_PRO_VERSION, false);

And use that in the my-plugin-pro.js file as

(function($) {
    $(document).on('click', '.my_plugin_export_pdf_btn', function(e) {
        e.preventDefault();
        if (typeof jsPDF !== 'undefined') {
            // jsPDF is available, let's create a PDF
            const doc = new jsPDF();
            doc.text('Hello, this is a test PDF created using jsPDF!', 10, 10);
            doc.save('test.pdf');
        } else {
            // jsPDF is not available
            console.error('jsPDF is not available!');
        }
    }); 
})(jQuery);

But I get the following error:

my-plugin-pro.js?ver=5.2.1:394 jsPDF is not available!
(anonymous) @   wmy-plugin-pro.js?ver=5.2.1:394
dispatch    @   jquery.js?ver=3.7.1:5145
elemData.handle @   jquery.js?ver=3.7.1:4949
0

There are 0 best solutions below