Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error: jsPDF is not a constructor #2719

Closed
centurianii opened this issue Apr 8, 2020 · 4 comments
Closed

Error: jsPDF is not a constructor #2719

centurianii opened this issue Apr 8, 2020 · 4 comments
Assignees

Comments

@centurianii
Copy link

OK, I think you messed with automatic bundlers like everybody these days...

Replace first lines with these:

(function (factory) {
  if(typeof define === 'function' && define.amd)
      define(factory) 
  else
   window.jsPDF = factory();
}(function () { 'use strict';
...

Now we can make:

var x = new jsPDF();

without errors.

File I'm referred to: jspdf.debug.js at the dist folder of your project.

Now let's include minified version from CDN:

<!doctype html>
<html>
   <head>
      <meta charset="utf-8">
      <title>Test jsPDF</title>
   </head>
   <body style="">
   ...
   <script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.5.3/jspdf.min.js"></script>
   </body>
</html>

browser prints at console:

Script error.
at: https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.5.3/jspdf.min.js
line: 0

The above happens when we dynamically append the <script> tag at the bottom of <body> (using custom code here).
Amazingly, no error prints out if we hard-code the <script> tag!

From stackoverflow one proposes:

const jsPDF = require('jspdf')
var pdf = new jsPDF('p', 'pt', 'letter')

but you have to check what is the result in terms of javascript code when we issue a require in the Node environment - I mean what's the code that the browser runs? (browser javascript is not Node javascript).

Now can you change the CDN version of the library?

@github-actions
Copy link

github-actions bot commented Jul 8, 2020

This issue is stale because it has been open 90 days with no activity. It will be closed soon. Please comment/reopen if this issue is still relevant.

@HackbrettXXX
Copy link
Collaborator

Currently working on that...

@github-actions
Copy link

github-actions bot commented Oct 7, 2020

This issue is stale because it has been open 90 days with no activity. It will be closed soon. Please comment/reopen if this issue is still relevant.

@Vishwa17
Copy link

Vishwa17 commented Jan 6, 2021

Always use
const { jsPDF } = require("jspdf"); ---> define like this in nodejs if your file is .js extension

`const doc = new jsPDF('p');`

Then like this, it will work smoothly

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants