如何在离子2中使用pdfmake?

Cla*_*ira 5 ionic-framework pdfmake ionic2 typescript2.0 angular

我正在尝试使用pdfmake在ionic2中创建pdf.

我将库添加到我的应用程序:

$ npm install pdfmake --save
Run Code Online (Sandbox Code Playgroud)

将其导入课堂

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

import * as pdfmake from 'pdfmake'
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试实例化并使用该方法时,设备中显示的错误:

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();
Run Code Online (Sandbox Code Playgroud)

运行时错误:

fs.readFileSync is not a function
Run Code Online (Sandbox Code Playgroud)

如何在离子2中使用pdfmake?可能吗

在此输入图像描述

Cla*_*ira 1

所以...再次...很多天后,我终于在 pdfmake 社区的帮助下让 pdfmake 可以在我的项目上工作。

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

$ cd  project/www/
$ git clone https://github.com/bpampuch/pdfmake.git
Run Code Online (Sandbox Code Playgroud)

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

<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>
Run Code Online (Sandbox Code Playgroud)

并将导入替换为...

import * as pdfmake from 'pdfmake/build/pdfmake';
Run Code Online (Sandbox Code Playgroud)

Pdfmake 社区回应

Github上有项目测试