【问题标题】:How to use external library on angular-cli? (pdfmake)如何在 angular-cli 上使用外部库? (pdfmake)
【发布时间】:2017-07-10 14:18:41
【问题描述】:

好的,所以,我在我的测试项目中使用了 angular-cli,并且由于我是 web 开发世界的新手,我正在尝试新的想法。这次我没能成功。

好吧,我尝试了什么?

好吧,我遵循 pdfmake 自述文件并在我的 index.html 中将这两行放在 body 标记中:

<script src='build/pdfmake.min.js'></script>
<script src='build/vfs_fonts.js'></script>

好吧,我得到了这个错误:

获取http://localhost:4200/build/pdfmake.min.js GET http://localhost:4200/build/vfs_fonts.js 404(未找到)

所以,我把它拿走了,在我的组件中我只导入了 pdfmake,像这样:

import * as pdfmake from 'pdfmake/build/pdfmake.js';

它的作用是什么?好吧,没什么,我仍然没有在我的代码中的任何地方调用它,现在做:

pdfmake.createPdf(this.docDefinition).download();

docDefinition 代表我的 pdf 内容。

好的,现在呢?出现这个错误:

错误错误:在虚拟文件中找不到文件“Roboto-Regular.ttf” 系统

好的,我可能需要导入 vfs_font,在哪里?好吧,我不知道,我尝试在脚本标签处导入 angular-cli.json,不,不工作,我什至尝试在我的组件中导入,不。

我尝试使用 ng-pdf-make 库找到 ant npm 站点,但它根本不起作用。

我可能犯了一个新手错误,如果它简单而愚蠢,对不起:P。

@编辑

我尝试了他做的同样的事情,实际上它适用于我的项目中的 jquery,但它不适用于 pdfmake。

这是我的 angular-cli.json 脚本标签:

"scripts": [
        "../node_modules/pdfmake/build/pdfmake.min.js",
        "../node_modules/pdfmake/build/vfs_fonts.js",
        "../node_modules/jquery/dist/jquery.js",
        "../node_modules/tether/dist/js/tether.js",
        "../node_modules/bootstrap/dist/js/bootstrap.js"
      ],

事实上,即使导入 vfs_fonts,我也无法使用 pdfmake.createPdf("content"):

import * as pdfmake from 'pdfmake/build/pdfmake.min.js';
import * as pdfFonts from 'pdfmake/build/vfs_fonts.js';

【问题讨论】:

  • 你试过this吗?
  • 您的pdfmake.min.js 和其他文件在您的项目文件结构中在哪里?如果是典型的cli项目,应该放在dist文件夹中
  • @GangadharJannu 是的
  • @ShanilFernando 使用 npm install 就像我将库放在 node_modules/pdfmake... 和 pdfmake.min.js 位于 node_modules/pdfmake/build/pdfmake.min.js

标签: angular typescript angular-cli pdfmake


【解决方案1】:

首先你需要通过 npm 安装你的 pdfmake

npm install pdfmake --save

它会将您的包裹保存在node_modules

之后你可以像这样访问pdfmake

 import * as pdfMake from 'pdfmake/build/pdfmake';
 import * as pdfFonts from 'pdfmake/build/vfs_fonts';

在使用 pdfMake 之前像这样初始化

 pdfMake.vfs = pdfFonts.pdfMake.vfs;

示例:-

import { Component } from '@angular/core';
 import * as pdfMake from 'pdfmake/build/pdfmake';
 import * as pdfFonts from 'pdfmake/build/vfs_fonts';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'app works!';
constructor(){
     //called first time before the ngOnInit()
     pdfMake.vfs = pdfFonts.pdfMake.vfs;
      var dd = { content: 'your pdf data' };
    pdfMake.createPdf(dd).download();
  }

}

它应该可以工作。

您不需要在 index.html 或脚本中添加任何内容。

【讨论】:

  • pdfmake.createPdf("你的数据");
  • 仍然出现同样的错误._.: 错误:在虚拟文件系统中找不到文件“Roboto-Regular.ttf”
  • 如果您的问题得到解决,我们很乐意提供帮助,而不是选择我的答案。
  • 如何将表格作为内容传递给 var dd = { content: 'your pdf data' };
【解决方案2】:

pdfmake.min.js在node_modules中,那么浏览器如何访问node_modules中的pdfmake.min.js呢?只有dist 文件夹内容可供外界使用,因此也可供浏览器使用。

按照@Jonnysai 说的安装npm 模块并配置.angular-cli.json 如下

{
...
"scripts": ["node_modules/pdfmake/build/pdfmake.min.js"],
...
}

所有其他静态资产,如 Roboto-Regular.ttf 复制到 src/assets/ 文件夹中。整个assets 文件夹将在转译时自动复制到dist。然后在你的 index.html 中,像assets/oboto-Regular.ttf 一样包含它们

【讨论】:

  • 我像@Jonnysai 说的那样安装了,我在 .angualr-cli.json 中的脚本和你的一样。关于包括 assets/Roboto-Regular.ttf 我需要使用样式标签或其他,我还是 html 新手:P
  • 然后尝试手动复制到 assets 文件夹并相应地包含在 HTML 中。但我不推荐这种方式。不过你可以试一试。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-16
  • 2017-12-21
  • 2017-07-22
  • 2017-02-27
  • 1970-01-01
相关资源
最近更新 更多