Anoop Kc
Anoop Kc

Reputation: 167

Any way to reduce the bundle size of pdfmake

I am using pdfmake in my angular 4 app and it really helps me in making nice pdf docs on the client side. But I am a little concerned when I look into the bundle-report after an aot build. It adds a considerable amount of weight to my chunk. Is there any way to reduce this, like importing only the needed parts/services/modules or so? Right now I am importing it as below

import { createPdf } from 'pdfmake/build/pdfmake';

enter image description here

Upvotes: 7

Views: 3225

Answers (2)

Lawl
Lawl

Reputation: 51

Any way to reduce the bundle size of pdfmake?

Not directly, but you may defer the bundle loading from app initialization to the time you actually use pdfMake. This way you make a trade off - your 'main.js' file will be substantially smaller and therefore initial page load will be faster, but the first PDF generation operation will become slower due to the need of loading the module beforehand.

Here's how I did it with Promises (procedure sequence when you need to generate a PDF):

  1. Load their VFS Fonts script file (CDN link to download it from: https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.32/vfs_fonts.js). And here is how you do that: https://codinglatte.com/posts/angular/lazy-loading-scripts-and-styles-angular/
  2. Load the pdfMake bundle: import('pdfmake/build/pdfmake').then(pdfMake => //generation code)

Upvotes: 1

pramodpxi
pramodpxi

Reputation: 1057

Try using this

import { createPdf } from 'pdfmake/build/pdfmake.min';

Import from min

Got this from github issue pdfmake/issues/1181

Upvotes: 3

Related Questions