gpt4 book ai didi

angular - 如何在 angular-cli 上使用外部库? (pdf制作)

转载 作者:搜寻专家 更新时间:2023-10-30 21:39:35 31 4
gpt4 key购买 nike

好的,所以,我在我的测试项目中使用 angular-cli,因为我是 Web 开发领域的新手,所以我在尝试新的想法。这次我没能成功。

那么,我尝试了什么?

好吧,我按照 pdfmake 自述文件进入了我的 index.html,我将这两行放在 body 标签中:

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

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

GET http://localhost:4200/build/pdfmake.min.js GET http://localhost:4200/build/vfs_fonts.js 404 (Not Found)

所以,我拿走了而不是我的组件,我只导入了 pdfmake,如下所示:

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

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

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

docDefinition 代表我的 pdf 内容。

好的,现在呢?出现此错误:

ERROR Error: File 'Roboto-Regular.ttf' not found in virtual file system

好的,我可能需要导入 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"
],

事实上我不能使用 pdfmake.createPdf("content") 即使是 vfs_fonts 导入:

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

最佳答案

首先你需要通过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 或脚本中添加任何东西。

关于angular - 如何在 angular-cli 上使用外部库? (pdf制作),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45014497/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com