Hello everyone, I’m looking for someone who could help me load fonts from a webgl build.
I’m working on a pdf download during a webgl build. After some research, I found the iText API which allows me to create pdf in C#. Everything was fine until I did some tests in webgl. There is a big problem with the font loading (I guess it’s because the webgl executable doesn’t have access to all the files on the computer). So I tested several methods that allowed me to create my own font by loading a ttf file (Resources.Load and UnityWebRequest) and then use it with iText. To create a font you have to go through a path or an array of bytes. So I tested these two methods but once the information is passed, iText returns the same error and I don’t know how to get out of it.
a) you should post the code how you load that font and pass it into iText
b) test your code in editor or desktop build to confirm it’s generally working, and only fails in webgl builds (in case you haven’t done so)
c) assuming that only few forum members know iText, you should reach out to the iText devs for support as well
PS: code running in browsers has generally no access to the file system other than what’s bundled.
Thanks for you reply.
There are two screens attached : one is the resources loading and the other is the https loading.
The error send me to the line 88 from the second screen.
As you said, I’ve already tried each method in the editor and the window builds and they worked well.
I should highly consider your proposition about writting to the iText devs.
Okay, good. That wasn’t clear from the original post, sounded like you made that change only for web.
Btw, you should post text stuff (code, error logs, etc) right in the post (with code formatting), so it can be searched and quoted.
I can see from the code that you aren’t checking if Resources.Load returns null - not sure if it can but it’s one quick check you should add. If it returns null or an empty array, you’ll know its the resource loading part that fails and not something the iText devs should be concerned with.
I wonder if Resources folder is handled differently in web. Maybe try putting it under StreamingAssets instead, and use regular File.IO like so:
var bytes = File.ReadAllBytes(Path.Combine(Application.streamingAssetsPath, "Font/Arial.ttf"));
Assuming the path is: Assets/StreamingAssets/Font/Arial.ttf
Next code stuff will be posted with code formatting if it can help then!
I am aware of the “not checking null value” from the Resources.Load. I’ve just checked it using the VS debugger for now and the loading is fine. But as you said, it’s just a quick check and it will be added as soon as possible.
From what I’ve already tested, it is handled the same way in webgl as editor and windows. I’ll check tomorrow morning (once I have access to the project again) using the regular File.IO way. I’ll keep you informed. Thank you for your time so far.
I tested this method but there is always an error, here concerning the conversion to bytes (I took care that the path is correct).
I then tested with a conversion function directly with iText but still the same error :
byte[] data = PdfEncodings.ConvertToBytes(text.text, PdfEncodings.UTF8);
--> IOException: Type of font is not recognized. At iText.IO.Font.FontProgramFactory.CreateFont (System.String name, System.Byte[] fontProgram, System.Boolean cached) [0x00000] in <00000000000000000000000000000000>:0