我正在尝试使用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可以用于我的项目。
我将编译后的版本克隆到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 * as pdfmake from 'pdfmake/build/pdfmake';
Pdfmake community response
Github with project test