pdfme / pdfme

Open-source PDF generation library built with TypeScript and React. Features a WYSIWYG template designer, PDF viewer, and powerful generation capabilities. Create custom PDFs effortlessly in both browser and Node.js environments.
http://pdfme.com/
MIT License
2.7k stars 241 forks source link

Reduce bundle size by importing heavyweight features on-demand #161

Open sebastian-onlea opened 1 year ago

sebastian-onlea commented 1 year ago

Is your feature request related to a problem? Please describe.

tl;dr: static inclusion of bwip-js, @pdf-lib/fontkit, and default Helvetica font data increases bundle size significantly, even when they're not being used.

I've been using @pdfme/generator to embed PDF document/certificate creation features in e-learning module packages and it's been super—the WYSIWIG template designer is magical! The 2420kB webpack bundle is quite heavyweight, though, and profiling the bundle shows some hot potatoes: bwip-js, fontkit, and constants.ts. See "Additional context" for the bundle profile treemap.

Describe the solution you'd like

My proof-of-concept implementation is in sebastian-onlea/pdfme:dev/optimize-bundlesize. The way I shoehorned | { standardFont: '...' } into the Font definition isn't especially elegant, so I'm happy to take suggestions on that (or anything else in the patch!) before creating the PR.

Describe alternatives you've considered

I've been working on optimizing the bundle size by working on a local fork; initially I was looking at cutting the extra libraries out entirely for our use cases, but async loading appears to work well enough while retaining opt-in support for other projects using custom fonts and barcode generation.

Additional context

image.

hand-dot commented 1 year ago

Hey @sebastian-onlea You are true. Could you submit PR?

I would like to merge your modifications if you can confirm that it works correctly in Node and browser environment.

sebastian-onlea commented 1 year ago

Thanks! I'll follow the recommended testing steps in DEVELOPMENT.md and run the tests; are there any other validation cases that I should check for both Node and browser coverage?

hand-dot commented 1 year ago

@sebastian-onlea Thank you...!!!

are there any other validation cases that I should check for both Node and browser coverage?

It's enough Node and browser! I'll support you after PR, I also check your code changes and bug check.

dkastl commented 1 year ago

It would be nice to have a PR to be able to try your proposal and provide some feedback/help.

Currently I see 3 branches in your fork: https://github.com/sebastian-onlea/pdfme/branches ... with a pull request the correct branch would be clear.