Library not working with Ember.js because of extended array prototype
See original GitHub issueConfiguration:
- Web browser and its version: chrome Version 78.0.3904.108
- Operating system and its version: MacOs
- PDF.js version: 2.0.943
- Is a browser extension:
This is not an issue with the lib itself, but hopefully I can ask for feedback and help.
I am using this library in an ember.js project. Usually, everything works well, but some pdf do not render the text correctly, and I get Error during font loading: this._map[t].charCodeAt is not a function
as an error. Searching around, I found #7064 and #7318 that explains that the fact that Ember extend the Array prototype and make them enumerable seems to be causing a problem.
A fix that works is to disable globally Ember array extension, but our app is quite large and manually going into the code to make sure every array is an Ember Array would be extremely cumbersome.
So, my questions:
- Is there any possible reason why some file would display correctly why other won’t? Could it be a font or encodage issue? I tried loading a file that does not display correctly with the simple viewer demo provided and it works, so I am thinking this is possibly not the issue, but it’s still weird that some file work correctly while some don’t.
The way it works is that an external user add text to the pdf with an editing pdf program, and then upload the pdf to our web site that they then use a PDF.js based viewer to look at, and this is where sometime the text does not get displayed with the error.
- Is there any modification I could make to the library code itself that would make this work, without having to disable globally my ember array extension?
I found this commit and I tried to make the same modification to my node_modules/pdfjs-dist/lib/core/fonts.js
file but it had no effect. That kind of solution would be perfect for me though, even if I lose some performance.
Thank you very much!
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:6 (2 by maintainers)
Top GitHub Comments
I’m sure the code below has some potentially dire implications, but simply setting enumerable to false on the _super array prototype fixes the issue for me, without breaking the rest of my app [Ember v3.25.1 (Octane)]
PDF.js requires that array are implemented in a standard way, so either a native implementation or one that implements everything from the specification. If not, you’ll need to polyfill the implementation yourself. There are existing polyfills for arrays, for example in
core-js
. However, this is outside of the scope of PDF.js and we would recommend to disable any Ember-specific overrides of native JavaScript primitives because you’re bound to run into issues sooner or later.Depending on the PDF file itself (based on encoding, font types, et cetera), other code paths will be taken, so not all may require
charCodeAt
to be present. It’s not possible to say in general without code inspection.The extensions are in fact breaking normal JavaScript behavior, so it’s very much not recommended, but if you really need to you can use existing polyfills at outlined above. They might conflict with Ember’s modifications though, but for that you’ll need to ask Ember developers for support.