user1916077
user1916077

Reputation: 451

How to use jspdf-autotable in Angular 4?

I was able to import jsPDF, however I can't use autoTable method from jspdf-autotable, not sure how to import it as a dependency of jsPDF.

import { Injectable, Component } from '@angular/core';

import * as jsPDF from 'jspdf';
import { autoTable } from 'jspdf-autotable';

@Injectable()
export class PdfService {

    constructor() {

    }

    convertJsonToPdf(columns: any, jsonData: any) {
       var doc = new jsPDF('p', 'pt');  // OK, created
       doc.autoTable(columns, jsonData); // Fails because autoTable is not in doc
    }
}

Upvotes: 3

Views: 7488

Answers (2)

Daniel Steven neds
Daniel Steven neds

Reputation: 41

Instead of declaring like:

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

it is better to do this

import 'jspdf';
import 'jspdf-autotable';
declare let jsPDF;

This is how I solved the problem.

Upvotes: 2

Simon Bengtsson
Simon Bengtsson

Reputation: 8161

Extracted from discussion above, replace this line:

import { autoTable } from 'jspdf-autotable'; 

with

import 'jspdf-autotable';

Upvotes: 7

Related Questions