question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to import pdfkit in ng2/ionic 2 typescript based app

See original GitHub issue

Hi, I want to use pdfKit in my ionic 2 typescript based app. Is this possible?

I gave it a try and followed these steps:

  1. Via the ionic CLI I created a new ionic2 v2 app

  2. I installed pdfkit and blob-stream via npm install --save pdfkit npm install --save blob-stream

  3. I installed the typings: npm install --save @types/pdfstream npm install --save @types/blob-stream

  4. 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'));
    });
}

} `

  1. 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:closed
  • Created 6 years ago
  • Reactions:2
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

9reactions
mohamed-badaouicommented, Jan 7, 2019

Does pdfkit work for angular 6+? if Yes, how ?

Got following error :

vendor.js:74981 ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'prototype' of undefined
TypeError: Cannot read property 'prototype' of undefined
    at extend (:4210/views-fundscall-fundscall-module.js:30143)
    at :4210/views-fundscall-fundscall-module.js:30151
    at Object.<anonymous> (:4210/views-fundscall-fundscall-module.js:30235)
    at Object../node_modules/pdfkit/js/reference.js (:4210/views-fundscall-fundscall-module.js:30241)
    at __webpack_require__ (runtime.js:84)
    at Object.<anonymous> (:4210/views-fundscall-fundscall-module.js:29561)
    at Object../node_modules/pdfkit/js/object.js (:4210/views-fundscall-fundscall-module.js:29563)
    at __webpack_require__ (runtime.js:84)
    at Object.<anonymous> (:4210/views-fundscall-fundscall-module.js:26399)
    at Object../node_modules/pdfkit/js/document.js (:4210/views-fundscall-fundscall-module.js:26629)
    at resolvePromise (polyfills.js:3159)
    at resolvePromise (polyfills.js:3116)
    at polyfills.js:3218
    at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (polyfills.js:2766)
    at Object.onInvokeTask (vendor.js:76531)
    at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (polyfills.js:2765)
    at Zone.push../node_modules/zone.js/dist/zone.js.Zone.runTask (polyfills.js:2533)
    at drainMicroTaskQueue (polyfills.js:2940)
defaultErrorLogger @ vendor.js:74981
push../node_modules/@angular/core/fesm5/core.js.ErrorHandler.handleError @ vendor.js:75029
next @ vendor.js:77012
schedulerFn @ vendor.js:72993
push../node_modules/rxjs/_esm5/internal/Subscriber.js.SafeSubscriber.__tryOrUnsub @ vendor.js:196670
push../node_modules/rxjs/_esm5/internal/Subscriber.js.SafeSubscriber.next @ vendor.js:196608
push../node_modules/rxjs/_esm5/internal/Subscriber.js.Subscriber._next @ vendor.js:196551
push../node_modules/rxjs/_esm5/internal/Subscriber.js.Subscriber.next @ vendor.js:196528
push../node_modules/rxjs/_esm5/internal/Subject.js.Subject.next @ vendor.js:196293
push../node_modules/@angular/core/fesm5/core.js.EventEmitter.emit @ vendor.js:72977
(anonymous) @ vendor.js:76562
push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invoke @ polyfills.js:2733
push../node_modules/zone.js/dist/zone.js.Zone.run @ polyfills.js:2483
push../node_modules/@angular/core/fesm5/core.js.NgZone.runOutsideAngular @ vendor.js:76499
onHandleError @ vendor.js:76562
push../node_modules/zone.js/dist/zone.js.ZoneDelegate.handleError @ polyfills.js:2737
push../node_modules/zone.js/dist/zone.js.Zone.runGuarded @ polyfills.js:2499
_loop_1 @ polyfills.js:3022
api.microtaskDrainDone @ polyfills.js:3031
drainMicroTaskQueue @ polyfills.js:2947
1reaction
Dzivocommented, Sep 25, 2018

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to import pdfkit in ng2/ionic 2 typescript based app
How to import pdfkit in ng2/ionic 2 typescript based app.
Read more >
@types/pdfkit - npm
Start using @types/pdfkit in your project by running `npm i ... TypeScript icon, indicating that this package has built-in type declarations.
Read more >
Getting Started with PDFKit
There are two ways to use PDFKit in the browser. The first is to create an app using an module bundler like Browserify...
Read more >
pdfkit - PyPI
Python 2 and 3 wrapper for wkhtmltopdf utility to convert HTML to PDF using Webkit. ... import pdfkit pdfkit.from_url('http://google.com', 'out.pdf') ...
Read more >
Generating PDF Files in Node.js with PDFKit - Stack Abuse
In dynamic web applications, you might need to export data into a document ... To create a PDF document, we will need to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found