首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Angular2:如何使用pdfmake库

Angular2:如何使用pdfmake库
EN

Stack Overflow用户
提问于 2017-07-17 04:44:48
回答 4查看 20.8K关注 0票数 13

试图使用客户端pdf库pdfmake在我的角2 (version=4.2.x)项目.

在.角-cli.json文件中,我声明js如下所示:

代码语言:javascript
复制
"scripts": [
    "../node_modules/pdfmake/build/pdfmake.js",
    "../node_modules/pdfmake/build/vfs_fonts.js"
  ]

然后在app.component.ts中,我像这样使用它:

代码语言:javascript
复制
import * as pdfMake from 'pdfmake';

@Component({
selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})
export class AppComponent implements OnInit {
   pdf: any;
   downloadPdf() {
    let item = {firstName: 'Peter', lastName: 'Parker'};
    this.pdf = pdfMake;
    this.pdf.createPdf(buildPdf(item)).open();
  }
}

function buildPdf(value) {
   var pdfContent = value;
   var docDefinition = {
     content: [{
    text: 'My name is: ' + pdfContent.firstName  + ' ' + pdfContent.lastName + '.'
     }]
   }
   console.log(pdfContent);
   return docDefinition;
}

在加载应用程序时,我在浏览器控制台中碰到了下面的错误:

代码语言:javascript
复制
Uncaught TypeError: fs.readFileSync is not a function
at Object.<anonymous> (linebreaker.js:15)
at Object.<anonymous> (linebreaker.js:161)
at Object.../../../../linebreak/src/linebreaker.js (linebreaker.js:161)
at __webpack_require__ (bootstrap b937441…:54)
at Object.../../../../pdfmake/src/textTools.js (textTools.js:4)
at __webpack_require__ (bootstrap b937441…:54)
at Object.../../../../pdfmake/src/docMeasure.js (docMeasure.js:4)
at __webpack_require__ (bootstrap b937441…:54)
at Object.../../../../pdfmake/src/layoutBuilder.js (layoutBuilder.js:7)
at __webpack_require__ (bootstrap b937441…:54)

我解决这个问题的办法是:

将pdfmake.js和vfs_fonts.js复制到资产文件夹,然后将其添加到index.html中:

代码语言:javascript
复制
<script src='assets/pdfmake.min.js'></script>
<script src='assets/vfs_fonts.js'></script>

将其从app.component.ts中删除

代码语言:javascript
复制
import * as pdfMake from 'pdfmake';

并将其添加到app.component.ts中:

代码语言:javascript
复制
declare var pdfMake: any;

最后,将其从.角-cli.js中删除:

代码语言:javascript
复制
"../node_modules/pdfmake/build/pdfmake.js",
"../node_modules/pdfmake/build/vfs_fonts.js"

这是可行的,但它仍然是一个解决办法。

有人知道如何以角/脚本的方式使用这个库吗?

非常感谢!

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2017-07-18 00:28:19

按照@benny_boe上面的指示,我已经让它工作了!让我将其概述如下:

第一,

代码语言:javascript
复制
npm install pdfmake --save

其次,将以下内容添加到typings.d.ts中:

代码语言:javascript
复制
declare module 'pdfmake/build/pdfmake.js';
declare module 'pdfmake/build/vfs_fonts.js';

第三,在使用pdfMake的文件中,组件或服务添加以下行:

代码语言:javascript
复制
import * as pdfMake from 'pdfmake/build/pdfmake.js';
import * as pdfFonts from 'pdfmake/build/vfs_fonts.js';
pdfMake.vfs = pdfFonts.pdfMake.vfs;

最后,像往常一样使用pdfMake.xxx()。

票数 27
EN

Stack Overflow用户

发布于 2017-07-17 08:11:48

所以首先要做的是。您可以做而不是,需要将您的第三方脚本添加到.angular-cli.json 中,并且在您的TS文件中添加一个导入。从全局脚本的角度看一看它的故事。

一旦您通过脚本数组导入一个库,您应该而不是通过TypeScript代码中的导入语句导入它.

(没有pdfmake的类型,所以您需要在取消配置文件时声明它们。)

所以如果你想把它添加到你的TS文件中..。替换您的import * as pdfMake from 'pdfmake'; (这是服务器端版本!)客户端版本('pdfmake/build/pdfmake')。您还需要添加字体('pdfmake/build/vfs_fonts'),否则也会出现错误。

当您的进口看起来像这样时,它应该可以工作:

代码语言:javascript
复制
import pdfMake from 'pdfmake/build/pdfmake';
import pdfFonts from 'pdfmake/build/vfs_fonts';
pdfMake.vfs = pdfFonts.pdfMake.vfs;
票数 13
EN

Stack Overflow用户

发布于 2018-04-29 14:56:08

另一个简单的方法,当使用全局脚本时,根据角-cli 故事全球脚本,如果您仔细地遵循指示。如果库没有任何类型。

On ange-cli.json文件

代码语言:javascript
复制
"scripts": [
  "../node_modules/pdfmake/build/pdfmake.min.js",
  "../node_modules/pdfmake/build/vfs_fonts.js"
],

ON src/file ings.d.ts文件

在下面添加declare var pdfMake: any;行。

现在,应用程序中的任何地方都可以使用pdfMake全局变量。

尝试在构造器或任何init方法中记录pdfMake。

代码语言:javascript
复制
ngOnInit() { console.log(pdfMake); }

输出

代码语言:javascript
复制
{
    createdPdf: f(t),
    vfs: {
        Roboto-Italic.ttf: "some long encoded string...",
        Roboto-Medium.ttf: "some long encoded string...",
        Roboto-MediumItalic.ttf: "some long encoded string...",
        Roboto-Regular.ttf: "some long encoded string...",
    }
}

也就是说它已经准备好使用了。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45136111

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档