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

How to use jsPDF in node? Gets _jspdf2.default is not a constructor error #783

Closed
THPubs opened this issue Jun 24, 2016 · 13 comments
Closed

Comments

@THPubs
Copy link

THPubs commented Jun 24, 2016

I tried to import jspdf like this in my node app :

import jsPDF from 'jspdf';

Then tried to use the example code :

var doc = new jsPDF();
doc.text(20, 20, 'Hello world.');
doc.save('Test.pdf');

Then I get an error saying _jspdf2.default is not a constructor error. What is the right way to do this?

@oscaroceguera
Copy link

try in index.html => https://cdnjs.com/libraries/jspdf

@devvmh
Copy link

devvmh commented Aug 22, 2016

@THPubs I think you'll need to do one of these:

const jsPDF = require('jspdf')

or

import * as jsPDF  from 'jspdf'

This is because es6 import syntax is subtly different than node's import syntax. Probably you're best off using node require, unless this lib is updated to better accommodate es6.

I'm not 100% sure how it all works. there's more discussion at, for example, microsoft/TypeScript#5565

@crtormen
Copy link

I was facing the same problem, but was
using this syntax to import:

import { jsPDF } from 'jspdf'

I removed the brackets and now it is working.

I'm using SystemJS to load, so I mapped it in my config.js
map: { "jspdf": "node_modules/jspdf/dist/jspdf.min.js" }

installed typings:
typings install dt~jspdf --global --save

And in my component.ts
import jsPDF from 'jspdf';

let doc = new jsPDF(); doc.text(20, 20, 'Hello world.'); doc.save('Test.pdf');

@rclai
Copy link

rclai commented Dec 20, 2016

How did you guys get around the window is undefined error?

@antonioiksi
Copy link

antonioiksi commented Nov 24, 2017

it works for REACT!

import React from 'react';
import * as jsPDF  from 'jspdf'

class JSPDFTest extends React.Component {
    constructor(props){
        super(props);
        this.pdfToHTML=this.pdfToHTML.bind(this);
    }

    pdfToHTML(){
        var specialElementHandlers = {
            '#myId': function(element, renderer){
                return true;
            },
        };

        let doc = new jsPDF();
        doc.text(20, 20, 'Hello world.');
        doc.addPage('a4','p');
        var source = document.getElementById('myId'); //$('#HTMLtoPDF')[0];
        doc.fromHTML(
            source, 15, 15, {
                'elementHandlers': specialElementHandlers
            }
        );
        doc.addPage('a4','l');
        doc.fromHTML(
            source, 15, 15, {
                'elementHandlers': specialElementHandlers
            }
        );
        doc.save('Test.pdf');


    }

    render() {
        return (
            <div>
                <div id="myId" >
                        <h2>HTML to PDF</h2>
                        <p>Lorem ipsum dolor sit amet, consectetur adipisicing </p>
                </div>
                <button onClick={this.pdfToHTML}>Download PDF</button>
            </div>
        );
    }
}

export default JSPDFTest;

@Uzlopak
Copy link
Collaborator

Uzlopak commented Jun 13, 2018

I guess it works now.

@Uzlopak Uzlopak closed this as completed Jun 13, 2018
@hossammourad
Copy link

hossammourad commented Sep 18, 2018

I got it working with this syntax:

import jsPDF from 'jspdf';
const pdf = new jsPDF();

and since it is a good practice for any constructor to start with a capital letter and most likely linter would catch it as an error if you used a small letter, this syntax can be used:

import { default as JSPDF } from 'jspdf';
const pdf = new JSPDF();

@RSPaul
Copy link

RSPaul commented Oct 29, 2020

I am getting the same error:

var doc = new jsPDF('l', 'mm', [500, headers.length * 150]);
TypeError: jsPDF is not a constructor

@HackbrettXXX
Copy link
Collaborator

@RSPaul did you const { jsPDF } = require("jspdf") it?

@RSPaul
Copy link

RSPaul commented Oct 29, 2020

@HackbrettXXX Yes, I used it but still getting the same error.

@HackbrettXXX
Copy link
Collaborator

@RSPaul well then we need more information. Please share your complete code snippet, any build tools you use, node version, jsPDF version, etc.

@vaibhav3027
Copy link

I get this error

ReferenceError: document is not defined
at a (/my/path/to/project/node_modules/jspdf/dist/jspdf.node.min.js:197:1325)

due to doc.html() in below code

import {jsPDF}  from 'jspdf';

let doc = new jsPDF();
let handleElement = {
    '#editor': function (element, renderer) {
      return true;
    }
  };
doc.html(internationQuoteHTML, {
    'x': 15,
    'y': 15,
    'width': 200,
    'elementHandlers': handleElement
  });

@HackbrettXXX
Copy link
Collaborator

The html function doesn't work in node. See also #2805 (comment)

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