Downloading a pdf with jsPdf library, what's wrong with my code?

<html>
  <head>
    <script src="https://unpkg.com/jspdf@latest/dist/jspdf.min.js"></script>
    <script type='text/javascript'>
        import { jsPDF } from "jspdf";

        // Default export is a4 paper, portrait, using millimeters for units
        function test(){
          const doc = new jsPDF();

          doc.text("Hello world!", 10, 10);
          doc.save("a4.pdf");
        }
        
    </script>
  </head>
  <style>
  </style>
  
  <body>
    <a href='javascript:test()'>link</a>
      <script>


      </script>
  </body>
</html>

From the docs:

The import statement cannot be used in embedded scripts unless such script has a type="module".

I think that might be your issue - sorry, I don’t do this kind of thing much anymore. And I would use something like webpack so I could break apart these things into separate files and use full ES6.

1 Like