5

I'm trying to create pdf in ionic2 with pdfmake.

I added the library to my app:

$ npm install pdfmake --save

Import it into class

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

import * as pdfmake from 'pdfmake'

But, When I try to instantiate and use the method, the displayed error in the device:

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();

Runtime Error:

fs.readFileSync is not a function

How can I use pdfmake in ionic 2? Is it possible

enter image description here

calraiden
  • 1,686
  • 1
  • 27
  • 37

1 Answers1

1

So... again... After many days, I finally get the pdfmake to work on my project with pdfmake community's help .

I cloned the compiled version into the www folder

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

Then I added the scripts to the index.

<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>

and replace import to ...

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

Pdfmake community response

Github with project test

calraiden
  • 1,686
  • 1
  • 27
  • 37
  • 1
    When I am adding project like this in Ionic 2 "clone https://github.com/bpampuch/pdfmake.git" it gives me error 'clone' is not recognized as an internal or external command, operable program or batch file – Shehram Tahir Jul 03 '17 at 10:06