bpampuch / pdfmake

Client/server side PDF printing in pure JavaScript
http://pdfmake.org
Other
11.44k stars 2.02k forks source link

Big problem in vfs_fonts. Objects are not extensible (React + Vite) #2726

Open PandukaNandara opened 1 month ago

PandukaNandara commented 1 month ago

I am using React with Vite. There is a serious problem needed to be addressed related to vfs_fonts. I am unable to set the vfs_fonts according to the outdated documentation.

// Not even working in locally ❌ 
import * as pdfMake from "pdfmake/build/pdfmake";
import * as pdfFonts from 'pdfmake/build/vfs_fonts';

// Also this is an illegal assignment. Imports are readonly  ❌  ❌ 

(<any>pdfMake).vfs = pdfFonts.pdfMake.vfs;

When I apply this code, I am getting the following error.

src/reports/barcode/BarcodeReport2.ts (22:13) Illegal reassignment of import "pdfMake" in "src/reports/barcode/BarcodeReport2.ts".

Then I figured out how to do this without getting an error locally.

// eslint-disable-next-line no-import-assign
Object.defineProperty(pdfMake, 'vfs', {
  value: vfs_fonts,
  writable: false, // optional, makes it read-only
});

But in the built version I am getting the following error.

<img width="576" alt="Why the fuck can't you fix this yet?" title="Why the fuck can't you fix this yet?" src="https://github.com/bpampuch/pdfmake/assets/37260467/a2b315bc-632c-40e8-b9c8-f5a04a1bf51d">

It is so pathetic that developers of the plugin have managed to build one of the best pdf generator library but still not be able to implement a simple function to set the vfs_fonts like this.

import * as pdfMake from 'pdfmake/build/pdfmake';
import * as pdfFonts from 'pdfmake/build/vfs_fonts';

pdfMake.setVfs(pdfFonts)
beppo-ivel commented 1 week ago

Removing the * as syntax worked for me.

https://stackoverflow.com/questions/69468718/using-pdfmaker-in-typescript#comment122787297_69468718

https://github.com/bpampuch/pdfmake/issues/1877#issuecomment-2148594824