如何在Ionic 2中使用pdfmake?

5

我正在尝试使用pdfmake在ionic2中创建PDF。

我已经将该库添加到我的应用程序中:

$ npm install pdfmake --save

将其导入到类中

import { Component } from '@angular/core';
import { NavController, NavParams, LoadingController, ToastController, AlertController } from 'ionic-angular';

import * as pdfmake from 'pdfmake'

但是,当我尝试实例化并使用该方法时,在设备上显示错误:
var dd = {
    content: [
        'First paragraph',
        'Another paragraph, this time a little bit longer to make sure, this line will be divided into at least two lines'
    ],
    pageSize: 'A4',
    pageMargins: [25, 25, 25, 25],
};

// download the PDF
var pdf = new pdfmake();
pdf.createPdf( dd ).download();

运行时错误:

fs.readFileSync is not a function

我如何在Ionic 2中使用pdfmake?是否可能?

在此输入图片描述


你尝试过从'pdfmake'导入pdfmake吗?CommonJS模块和所有。 - misha130
是的,我尝试过不用*,但对我来说没有效果。 - calraiden
@misha130 关于“CommonJS模块和所有内容”,抱歉,我没有理解。 - calraiden
1个回答

1

所以...再次...经过多天的努力,我终于在pdfmake社区的帮助下让pdfmake在我的项目中工作。

我将编译后的版本克隆到了www文件夹中。

$ cd  project/www/
$ git clone https://github.com/bpampuch/pdfmake.git

然后我将脚本添加到索引中。
<body>

  <!-- Ionic's root component and where the app will load -->
  <ion-app></ion-app>

  <!-- The polyfills js is generated during the build process -->
  <script src="build/polyfills.js"></script>

  <!-- The bundle js is generated during the build process -->
  <script src="build/main.js"></script>
  <script src='pdfmake/build/pdfmake.min.js'></script>
  <script src='pdfmake/build/vfs_fonts.js'></script>
</body>
</html>

把“import”替换为...
import * as pdfmake from 'pdfmake/build/pdfmake';

Pdfmake社区反应

带有项目测试的Github


1
当我在Ionic 2中像这样添加项目时 “clone https://github.com/bpampuch/pdfmake.git”它会给我一个错误 “clone”不被识别为内部或外部命令, 可执行程序或批处理文件。 - Shehram Tahir

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接