how to use jsPDF and jspdf-autotable in angular 5

后端 未结 10 854
北海茫月
北海茫月 2021-01-06 01:46

I am trying to use jsPDF and jspdf-autotable in my Angular 5.2.0 project. My package.json is below (related parts):

\"dependencies\": {
    ...
    \"jspdf\"         


        
10条回答
  •  清酒与你
    2021-01-06 02:42

    Is working for me Angular 5.

    To work with jspdf-autotable in angular 5, one must install jspdf and jspdf-autotable via npm

    npm install jspdf --save
    npm install @types/jspdf --save-dev
    npm install jspdf-autotable --save
    

    also add the jspdf and jspdf-autotable files to the scripts array in angular-cli.json

    "scripts": [
    "../node_modules/jspdf/dist/jspdf.min.js",
    "../node_modules/jspdf-autotable/dist/jspdf.plugin.autotable.js"
    ],
    

    and in component never import jspdf or jspdf-autotable just.

    Forget about the following import.

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

    Just use Before @Component:

    declare var jsPDF: any;
    

    Your component (related parts ):

    declare var jsPDF: any;
    
    @Component({
        selector: "energy-login",
        templateUrl: "./login.component.html",
        styleUrls: ["./login.component.scss"]
    })
    
    export class MyComponent implements OnInit {
    
        constructor() {}
    
        ngOnInit() {}
    
        downloadPDF() {
    
            let columns = ["ID", "Name", "Country"];
            let rows = [
                [1, "Shaw", "Tanzania"],
                [2, "Nelson", "Kazakhstan"],
                [3, "Garcia", "Madagascar"],
            ];
    
            let doc = new jsPDF('l', 'pt');
            doc.autoTable(columns, rows); // typescript compile time error
            doc.save('table.pdf');
            }
        }
    

提交回复
热议问题