Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jsPDF via script tag always not defined

I have searched for a long time but nothing helps. I have two files header.php and other.php.

My header.php it looks like this:

<head>
.....
</head>
<header>
.....
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/2.0.0/jspdf.umd.min.js"></script>
</header>

I require the header.php in other.php and want to use jsPDF in this file:

require "header.php";

<body>
....
</body>

<script>
    function createInvoice(final){
      const doc = new jsPDF();

      doc.text("Hello world!", 10, 10);
      doc.save("a4.pdf");
    }
</script>

I tried with the script to include jsPDF in the body tag, in the head tag and over the script tag with the function, but every time I am getting the following error :

Uncaught ReferenceError: jsPDF is not defined

Can someone help me? Thank you in advance

like image 458
vDrews Avatar asked Aug 20 '20 11:08

vDrews


1 Answers

In the new release, they changed the name of the global variable -> const { jsPDF } = window.jspdf.

<script>
    window.jsPDF = window.jspdf.jsPDF; // add this line of code

    function createInvoice(final){
        const doc = new jsPDF();

        doc.text("Hello world!", 10, 10);
        doc.save("a4.pdf");
    }
</script>
like image 83
Weihui Guo Avatar answered Nov 20 '22 07:11

Weihui Guo