Unable to run PDF to SVG in Docker without `ttf-mscorefonts-installer` but with fonts copied

Hello!

I’m having a hard time running .NET implementation of Aspose PDF to SVG conversion in official python-3.8:slim docker image. (Actually, I reproduced the first error in host Ubuntu 20.04 just by uninstalling ttf-mscorefonts-installer, so this is not particularly docker related).

So, I know about this link. In my case, I cannot just install ttf-mscorefonts-installer package as it’s stated in the link above because the package downloads fonts from Sourceforge and this resource is forbidden in my environment.
After investigating I’ve manually copied the fonts inside the container and that supposed to work:

RUN apt-get update -qq && apt-get install -y --no-install-recommends \
    libgdiplus \
    libc6-dev \
    libicu63  # This is to avoid ICU related error, second error to be discussed
    fontconfig \
    && rm -rf /var/lib/apt/lists/*
# Add MS fonts
COPY fonts/* /usr/share/fonts/truetype/msttcorefonts/
# Update fonts cache
RUN fc-cache -f -v

Strangely this is not working, giving the following error:

./tests/data_fixtures/input/Lorem-Ipsum.1.pdf: System.NullReferenceException: Object reference not set to an instance of an object.
at #=zkosqXJ91lGtPyLHtRuqNQgCs_DjXoeZ3m3CQON_0_qn4.#=z1yUTDBVqb7iz()
at #=zTqXlOYlaxvwI3cvKbLLdxP9WQmGaD8sGcm6Tz30FeYfwxmaxDQ==.#=zyWGBG9s=(#=zXu7BGZHPDNo6EkVq_lJdCq3H0KSkV4CHzl2IxXa$E2Oz #=zxWE$VIo=, #=z9eEytOfT0TpAoZw9oFq$iSO6TzCCGN9U2FFqBEFftZ1_ #=ziyVWFpeJfctX, #=zKcUUx2bOnR$Fl9_7Qli355oD1z7JF65jOA== #=zbDVmoBU=)
at #=zNnZKUM1IXHHjum$VoZx630vCpEdFjZw2ACUpLSxFnFb86MXAZg==.#=zyWGBG9s=(#=zXu7BGZHPDNo6EkVq_lJdCq3H0KSkV4CHzl2IxXa$E2Oz #=zxWE$VIo=, #=z9eEytOfT0TpAoZw9oFq$iSO6TzCCGN9U2FFqBEFftZ1_ #=ziyVWFpeJfctX)
at #=zxClYwO_U2PJlynuaEcNw59i4ZCXfxsI5uBkxRYG92ADB.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zb50g2w9M2HsL, #=zXu7BGZHPDNo6EkVq_lJdCq3H0KSkV4CHzl2IxXa$E2Oz& #=zxWE$VIo=, UnifiedSaveOptions #=zlt9mhDo=, Int32& #=zsU9AlIuS1Pvv, Boolean #=zXnlkq4E=)
at #=zwVSMed9n8rVHKVx9eS7oVtuPZjWw.#=zMEiIjQ6KgYH_(Document #=zb50g2w9M2HsL, String #=zjbNJdNwndyhT, Stream #=zw4bdJTw=, SvgSaveOptions #=zlt9mhDo=)
at Aspose.Pdf.Document.#=znMpv$NePL02D(String #=zjbNJdNwndyhT, SaveOptions #=zlt9mhDo=)
at Aspose.Pdf.Document.Save(String outputFileName, SaveOptions options)

The error is not giving much, so I dived into gdb and found out that a correct run (with the ttf-* package installed) has the following shared libs loaded (info shared in gdb):

<other libs>
0007fffeef32280  0x00007fffeef42e2b  Yes (*)     /lib/x86_64-linux-gnu/libz.so.1
0x00007fff57f8dad0  0x00007fff57fc9e73  Yes (*)     /lib/libgdiplus.so
0x00007fff57e6eb20  0x00007fff57ef0d02  Yes (*)     /lib/x86_64-linux-gnu/libglib-2.0.so.0
0x00007fff57d42cc0  0x00007fff57e0e487  Yes (*)     /lib/x86_64-linux-gnu/libcairo.so.2
0x00007fff57c7b790  0x00007fff57cfa205  Yes (*)     /lib/x86_64-linux-gnu/libfreetype.so.6
0x00007fff57bee540  0x00007fff57c31469  Yes (*)     /lib/x86_64-linux-gnu/libjpeg.so.8
0x00007fff57b71e60  0x00007fff57bb4ce5  Yes (*)     /lib/x86_64-linux-gnu/libtiff.so.5
0x00007fff7c039280  0x00007fff7c03d926  Yes (*)     /lib/x86_64-linux-gnu/libgif.so.7
0x00007fff57b36510  0x00007fff57b5a29d  Yes (*)     /lib/x86_64-linux-gnu/libpng16.so.16
0x00007fff57a0d0c0  0x00007fff57a965b6  Yes (*)     /lib/x86_64-linux-gnu/libX11.so.6
0x00007fff579bc420  0x00007fff579cd383  Yes (*)     /lib/x86_64-linux-gnu/libexif.so.12
--Type <RET> for more, q to quit, c to continue without paging--
0x00007fff5796cfc0  0x00007fff5799028e  Yes (*)     /lib/x86_64-linux-gnu/libfontconfig.so.1
0x00007fff578f42a0  0x00007fff5794486d  Yes (*)     /lib/x86_64-linux-gnu/libpcre.so.3
0x00007fff57855300  0x00007fff578d8384  Yes (*)     /lib/x86_64-linux-gnu/libpixman-1.so.0
0x00007fff7c0330e0  0x00007fff7c033c37  Yes (*)     /lib/x86_64-linux-gnu/libxcb-shm.so.0
0x00007fff7c013620  0x00007fff7c026699  Yes (*)     /lib/x86_64-linux-gnu/libxcb.so.1
0x00007fff57841120  0x00007fff57845f52  Yes (*)     /lib/x86_64-linux-gnu/libxcb-render.so.0
0x00007fff57633a60  0x00007fff57639ab5  Yes (*)     /lib/x86_64-linux-gnu/libXrender.so.1
0x00007fff576215e0  0x00007fff5762b84e  Yes (*)     /lib/x86_64-linux-gnu/libXext.so.6
0x00007fff573b6070  0x00007fff574071d4  Yes (*)     /lib/x86_64-linux-gnu/libwebp.so.6
0x00007fff5730f240  0x00007fff573a0d0a  Yes (*)     /lib/x86_64-linux-gnu/libzstd.so.1
0x00007fff572e53c0  0x00007fff572fc3a6  Yes (*)     /lib/x86_64-linux-gnu/liblzma.so.5
0x00007fff570d50a0  0x00007fff570dd5b5  Yes (*)     /lib/x86_64-linux-gnu/libjbig.so.0
0x00007fff570aa230  0x00007fff570c54b7  Yes (*)     /lib/x86_64-linux-gnu/libexpat.so.1
0x00007fff5709f580  0x00007fff570a2f71  Yes (*)     /lib/x86_64-linux-gnu/libuuid.so.1
0x00007fff7c003360  0x00007fff7c004052  Yes (*)     /lib/x86_64-linux-gnu/libXau.so.6
0x00007fff570971a0  0x00007fff57098a03  Yes (*)     /lib/x86_64-linux-gnu/libXdmcp.so.6
0x00007fff5707fe40  0x00007fff5708de69  Yes (*)     /lib/x86_64-linux-gnu/libbsd.so.0
                                        No          linux-vdso.so.1

While without the ttf-mscorefonts-installer pkg I’ve got only one of the libs above:

0x00007fffeef32280  0x00007fffeef42e2b  Yes (*)     /lib/x86_64-linux-gnu/libz.so.1

(I’m not sure if it’s related at all, just providing more info. I’m not a C# programmer and did not write the wrapper around the converter.)

So, the first question is, how ttf-mscorefonts-installer is affecting the docker/system or Aspose tool, so that the converter works? How can I achieve the same without it? I’m aware of this link - the suggested solution seems close but does not apply for C# as there are no FontRepository class.

The side question is about ICU
I also have got another error before the first one, maybe they are related:

Couldn\'t find a valid ICU package installed on the system. Set the configuration flag System.Globalization.Invariant to true if you want to run with no globalization support.\\n at System.Environment.FailFast(System.String)\\n at System.Globalization.GlobalizationMode.GetGlobalizationInvariantMode()\\n at System.Globalization.GlobalizationMode..cctor()\\n at System.Globalization.CultureData.CreateCultureWithInvariantData()\\n at System.Globalization.CultureData.get_Invariant()\\n at System.Globalization.CultureInfo..cctor()\\n at System.String.ToLowerInvariant()\\n at System.Text.EncodingHelper.GetCharset()\\n at System.Text.EncodingHelper.GetEncodingFromCharset()\\n at System.ConsolePal.GetConsoleEncoding()\\n at System.Console+<>c.<get_OutputEncoding>b__19_0()\\n at System.Threading.LazyInitializer.EnsureInitializedCore[[System.__Canon, System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]](System.__Canon ByRef, System.Object ByRef, System.Func`1<System.__Canon>)\\n at System.Threading.LazyInitializer.EnsureInitialized[[System.__Canon, System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]](System.__Canon ByRef, System.Object ByRef, System.Func`1<System.__Canon>)\\n at System.Console.get_OutputEncoding()\\n at System.Console.CreateOutputWriter(System.IO.Stream)\\n at System.Console+<>c.<get_Out>b__26_0()\\n at System.Threading.LazyInitializer.EnsureInitializedCore[[System.__Canon, System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]](System.__Canon ByRef, System.Object ByRef, System.Func`1<System.__Canon>)\\n at System.Threading.LazyInitializer.EnsureInitialized[[System.__Canon, System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]](System.__Canon ByRef, System.Object ByRef, System.Func`1<System.__Canon>)\\n at System.Console.get_Out()\\n at Microsoft.Extensions.CommandLineUtils.CommandLineApplication..ctor(Boolean)\\n"'

This can be fixed by installing libicu63 or by setting DOTNET_SYSTEM_GLOBALIZATION_INVARIANT=1 env var. Both ways work, although, it is unclear to me why this happens in the first place.

@Larleyt

It is quite possible that API is not able to access the font folder in your environment and raising an exception. Furthermore, the .NET API does have FontRepository Class and you can try using FontRepository.Sources.Add(new FolderFontSource("")) method in order to tell the API where to look the fonts. Please try changing the font location and use the above suggested method. In case you still face any issues, please feel free to let us know.

It is unclear, what is the default folder where Aspose looks for the fonts on Linux system? Why the error isn’t telling something like FontNotFound (or whatever exception there is for such cases)?

In case I can’t change source code with new FolderFontSource, the only option for me is to try placing fonts in different locations? Which locations should I try? No other packages or env variables are needed to be set for Aspose to work?

@Larleyt

The default folder is “/usr/share/fonts/truetype/msttcorefonts” where Aspose.PDF looks for the installed fonts on Linux like operating systems. In case operating system has other default folder/directory for fonts, you should use following line of code before performing any operation using Aspose.PDF.

Aspose.Pdf.Text.FontRepository.Sources.Add(new FolderFontSource("<user's path to ms fonts>"));

As far as the exception you are facing is concerned, the stack trace of the exception is encrypted due to obfuscated components of the API. However, in non-Windows environments, such exceptions are caused due to absence of fonts or specific font that you are trying to use.

Please let us know if you still face this issue after trying the suggested solutions by sharing your docker file with us in .zip file. We will log an investigation ticket in our issue management and share the ID with you.