5

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

Anoop Kc
  • 147
  • 1
  • 9
  • Probably not. Have you considered moving your PDF generation to the server instead? – GreyBeardedGeek Oct 30 '17 at 01:16
  • No, I thought of optimizing the client library, if that is possible it will be better for me. I am considering moving the PDF generation to server as a last resort. – Anoop Kc Oct 30 '17 at 02:50
  • 1
    See this link , it should help https://github.com/bpampuch/pdfmake/issues/1374 – dorriz Apr 16 '19 at 11:31

2 Answers2

1

Try using this

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

Import from min

Got this from github issue pdfmake/issues/1181

pramodpxi
  • 977
  • 8
  • 13
0

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)
Lawl
  • 21
  • 2