Typescript - 如何在 Angular2 中正确使用 jsPDF?

Typescript - how to correctly use jsPDF in Angular2?

我创建了一个生成 JSON 数据的 Angular2 应用程序。我想将此 JSON 输出保存到文件中,最好是 PDF 文件。此应用程序是使用 Typescript 编写的。

我使用 jsPDF 将 JSON 数据写入 PDF 文件。我已经使用 npm install jspdf --save 通过 npm 安装了 jsPDF 包。我还在 index.html 页面中添加了必要的链接。我在 运行 时对我的应用程序进行了这些更改。我能够将 JSON 数据保存到文件中。

当我停止并重新启动应用程序时,它没有按预期启动。我收到以下错误:

json-excel@1.0.0 start C:\Users*****\Documents\Projects\Angular\json-to-pdf

tsc && concurrently "npm run tsc:w" "npm run lite"

app/components/app.component.ts(35,19): error TS2304: Cannot find name 'jsPDF'.

我正在添加我使用过的代码。

package.json:

"dependencies": {
     "@angular/common": "2.0.0-rc.1",
     "@angular/compiler": "2.0.0-rc.1",
     "@angular/core": "2.0.0-rc.1",
     "@angular/http": "2.0.0-rc.1",
     "@angular/platform-browser": "2.0.0-rc.1",
     "@angular/platform-browser-dynamic": "2.0.0-rc.1",
     "@angular/router": "2.0.0-rc.1",
     "@angular/router-deprecated": "2.0.0-rc.1",
     "@angular/upgrade": "2.0.0-rc.1",
     "angular2-in-memory-web-api": "0.0.7",
     "bootstrap": "^3.3.6",
     "es6-shim": "^0.35.0",
     "jquery": "^2.2.4",
     "jspdf": "^1.2.61",
     "reflect-metadata": "^0.1.3",
     "rxjs": "5.0.0-beta.6",
     "systemjs": "0.19.27",
     "zone.js": "^0.6.12"
}

index.html:

<html>
  <head>
    <title>JSON to PDF</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    ....

    <script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.0.272/jspdf.debug.js"></script>

    ....

  </head>

  <!-- 3. Display the application -->
  <body class="bg">
    <div>
      <app>Loading...</app>
    </div>
  </body>
</html>

app.component.ts:

 import {Component} from '@angular/core';
 @Component({
   selector: 'app',
   template: `<button (click)="createFile()">Export JSON to PDF</button>`
 })
 export class AppComponent {
     public item = {
        "Name" : "XYZ",
         "Age" : "22",
         "Gender" : "Male"
     }
     constructor() {}
     createFile(){
        var doc = new jsPDF();
        var i=0;
        for(var key in this.item){
           doc.text(20, 10 + i, key + ": " + this.item[key]);
           i+=10;
        }
        doc.save('Test.pdf');
    }
 }

我认为 app.component.ts 文件中缺少一些导入语句。如果缺少什么,有人可以指出正确的导入语句吗?如果那是我犯的错误,我能知道如何在 Angular2 中正确使用 jsPDF 吗?

谢谢。

在最新的 angular cli 版本中使用 jspdf 非常简单。只需从 npm 安装 jspdf 并像这样使用它:

app.component.ts

// Note that you cannot use import jsPDF from 'jspdf' if you 
// don't install jspdf types from DefinitelyTyped
let jsPDF = require('jspdf');
import { Component } from '@angular/core';

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

  constructor() {
    let doc = new jsPDF();
    doc.text("Hello", 20, 20);
    doc.save('table.pdf');
  }
}

老版本的angular cli基于systemjs而不是webpack

Here is a link 描述了一种使用 angular-cliumd 或普通 javascript 中的库的方法。它基本上涉及使用 declare jsPDF: any 以及在 systemjs 供应商文件中导入库。

使用 Angular-cli 和 Angular 4 的方法是 首先将 jspdf 添加到 .angular-cli.json

中的 Scripts 数组
"scripts": [
        "../node_modules/jspdf/dist/jspdf.min.js"
      ]

然后在组件中添加以下内容

import * as JSPdf from 'jspdf'; 

然后在你的 class

  generatePdf() {
    let doc = new JSPdf();
    doc.text("Hello", 20, 20);
    doc.save('table.pdf');
}