How to import pdfkit in ng2/ionic 2 typescript based app
See original GitHub issueHi, I want to use pdfKit in my ionic 2 typescript based app. Is this possible?
I gave it a try and followed these steps:
-
Via the ionic CLI I created a new ionic2 v2 app
-
I installed pdfkit and blob-stream via npm install --save pdfkit npm install --save blob-stream
-
I installed the typings: npm install --save @types/pdfstream npm install --save @types/blob-stream
-
i made a service.ts file and create the following service:
` import { Injectable } from ‘@angular/core’; import * as pdfkit from ‘pdfkit’; import * as blobStream from ‘blob-stream’;
@Injectable()
export class PdfService {
doc;
stream: blobStream.IBlobStream;
constructor() {
this.doc = new pdfkit();
this.stream = this.doc.pipe(blobStream());
}
createPdf() {
this.doc.fontSize(25).text('Test PDF!', 100, 100);
this.doc.circle(280, 200, 50).fill("#6600FF");
this.doc.end();
this.stream.on('finish', function () {
window.open(this.stream.toBlobURL('application/pdf'));
});
}
} `
- in other file I call the createPdf() function of this service, but when i do a ionic serve I get the following error:
Uncaught TypeError: fs.readFileSync is not a function at Object.<anonymous> (index.js:10)
I Guess this is because pdfkit isn’t available in “browser mode” at this moment, but in “node mode”???
Can anybody help me with this?
Issue Analytics
- State:
- Created 6 years ago
- Reactions:2
- Comments:8 (1 by maintainers)
Does pdfkit work for angular 6+? if Yes, how ?
Got following error :
@hesampour thanks for the explanation but should we let angular and webpack compile the source code. Is there a plan to do something like that ?