Angular 9 with jsPDF module : Type 'typeof jsPDF' has no construct signatures
Asked Answered
P

4

5

Angular 9 Module which has issue jsPDF (installed types + packages itself)

When doing ng serve it works When doing ng build --prod , it has errors

ERROR in src/app/xxx/xxxx.componentomponent.ts:52:27 - error TS2351: This expression is not constructable.
  Type 'typeof jsPDF' has no construct signatures.

52       let pdf = new jsPDF('p', 'mm', 'a4');
                             ~~~~~

  src/app/xxx/xxxx.component.ts:7:1
    7 import * as jsPDF from 'jspdf';
      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Type originates at this import. A namespace-style import cannot be called or constructed, and will cause a failure at runtime. Consider using a default import or import require here instead.

tsconfig file has "esModuleInterop": true,

{
  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "./",
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "downlevelIteration": true,
    "experimentalDecorators": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "esModuleInterop": true,
    "importHelpers": true,
    "target": "es2015",
    "allowSyntheticDefaultImports":true,
    "typeRoots": [
      "node_modules/@types"
    ],
    "lib": [
      "es2018",
      "dom"
    ]
  },
  "angularCompilerOptions": {
    "fullTemplateTypeCheck": true,
    "strictInjectionParameters": true
  }
}

I import the module like this :

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

Use it like this inside my class :

 generatePDF() {
    var data = document.getElementById('contentToConvert');
    html2canvas(data).then(canvas => {
      var imgWidth = 208;
      var imgHeight = canvas.height * imgWidth / canvas.width;
      const contentDataURL = canvas.toDataURL('image/png')
      let pdf = **new jsPDF**('p', 'mm', 'a4');
      var position = 0;
      pdf.addImage(contentDataURL, 'PNG', 0, position, imgWidth, imgHeight)
      pdf.save('skill-set.pdf');
    });
  }

I also tried to add the module js files in scripts section of angular.json

  "scripts": [
              "node_modules/jquery/dist/jquery.min.js",
              "node_modules/bootstrap/dist/js/bootstrap.min.js",
              **"node_modules/jspdf/dist/jspdf.min.js"**
            ]
Piston answered 27/6, 2020 at 11:28 Comment(4)
Try installing jsPDF as a node module instead: npm install -S jspdf and remove yhe script import from angular.jsonDaffy
I've initially done it with npm install jspdf --save Then tried like a modulePiston
Dis you also install @types/jspdf?Daffy
@Daffy yes I did. I found out what was the issue, I sent couple parameters as I found in one article on web . **new jsPDF**('p', 'mm', 'a4'); Removing parameters , solved the issuePiston
S
23

(tested in angular 10, so I guess this fix is for newer angular versions)

instead of this :

import * as jsPDF from 'jspdf';

write this :

import jspdf from 'jspdf';

[Watch out ! both jspdf in 2nd line are in small letter]

then your

let pdf = new jsPDF('p', 'mm', 'a4'); 

would work too, just change all to small letter (you must import in small letter like my 2nd import line before doing this)

let pdf = new jspdf('p', 'mm', 'a4');
Steiger answered 29/8, 2020 at 13:51 Comment(0)
K
8

I setup a fresh angular 10 project and was able to use jspdf.

  1. Create new Angular app
> npx @angular/cli new pdf-viewer --strict --defaults true
  1. Install jspdf package from npm registry
> cd pdf-viewer
> npm install jspdf --save
> npm install @types/jspdf --save-dev

enter image description here

  1. Update tsconfig.base.json. Add allowSyntheticDefaultImports inside compilerOptions
"allowSyntheticDefaultImports": true
  1. Add some jspdf code to test in src/app/app.component.ts
import { Component, ChangeDetectionStrategy } from '@angular/core';
import jsPDF from 'jspdf';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css'],
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class AppComponent {
  title = 'pdf-viewer';

  constructor() {
    this.generatePDF();
  }

  private generatePDF(): void {
    const doc = new jsPDF();

    doc.text('Hello world!', 10, 10);
    doc.save('a4.pdf');
  }
}
  1. Launch angular application and navigate to http://localhost:4200. You will notice that a4.pdf gets downloaded as soon as we open the webpage. Open the PDF file to verify the integrity of the file.
> npm start
Kamin answered 27/6, 2020 at 22:44 Comment(1)
I figured out was was wrong. In my code , I pass some parameters "new jsPDF**('p', 'mm', 'a4') " as I found in some of the examples . This was actually the issue. Once I changed to **new jsPDF**() declaration , error was gonePiston
F
2

Just import like this:

import { jsPDF } from 'jspdf';

// and Initialize JSPDF
var doc = new jsPDF("p", "mm", "a4");

This works for me.

Francinefrancis answered 21/3, 2021 at 16:43 Comment(0)
F
-1

instead of this :

import * as jsPDF from 'jspdf';

write this :

import JSPDF from 'jspdf';

then your

let pdf = new JSPDF.jsPDF(); 
Finstad answered 6/11, 2020 at 13:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.