Excel (.xlsm) to PDF conversion fails with error in dotnet 8 Linux: Aspose.Cells.CellsException: The type initializer for '' threw an exception

Hi Aspose team,

we are using Aspose.Cells.NET 24.12 to convert .xlsm files to PDF. Running in a local development environment under Windows 10 works fine, but the production deployment using dotnet 8 in a Docker container encounters an exception “Aspose.Cells.CellsException: The type initializer for ‘’ threw an exception.”
Using a default .xls without any specific content works as well also on Linux. Only specific .xlsm files as given by business requirement encounter this error in production environment (but can be converted in local Windows environment)

Packages added in .csproj:

<PackageReference Include="Aspose.Cells" Version="24.12.0" />
<PackageReference Include="SkiaSharp.NativeAssets.Linux" Version="3.116.1" />

Dockerfile extract:

FROM mcr.microsoft.com/dotnet/aspnet:8.0
RUN apt-get install --upgrade -y fontconfig
COPY OBB2PDF/fonts/ /usr/local/share/fonts
RUN fc-cache -v

Any help would be appreciated.
Best regards,
Thomas

@thomas.altschaffel

It seems that you are encountering an issue when converting specific .xlsm files to PDF using Aspose.Cells in a Docker container running on Linux. The error message “Aspose.Cells.CellsException: The type initializer for ‘’ threw an exception” suggests that there may be a problem related to the environment or dependencies required for processing .xlsm files.

Here are some steps you can take to troubleshoot and potentially resolve the issue:

  1. Check Font Configuration: Since you are using fontconfig in your Dockerfile, ensure that the fonts required by your .xlsm files are correctly installed and accessible in the Docker environment. You can verify the fonts by running fc-list inside the container to see if the necessary fonts are listed.

  2. Update Aspose.Cells: Ensure that you are using the latest version of Aspose.Cells. As of your current version (24.12), there may have been updates or bug fixes in later versions that could resolve compatibility issues with .xlsm files.

  3. Test with Minimal Example: Create a minimal .xlsm file that replicates the issue. This can help isolate whether the problem is with specific content in the original files or with the conversion process itself.

  4. Check for Missing Dependencies: Sometimes, additional libraries or dependencies may be required for certain features in Aspose.Cells. Ensure that all necessary dependencies are included in your Docker image.

  5. Run in Debug Mode: If possible, run your application in debug mode to get more detailed error messages or stack traces that could provide insight into what is causing the exception.

  6. Consult Aspose Support: If the issue persists, consider reaching out to Aspose support with detailed information about the error, the specific .xlsm files causing the issue, and your Docker setup. They may have more specific guidance or solutions based on similar cases.

By following these steps, you should be able to identify the root cause of the issue and find a suitable solution for converting .xlsm files to PDF in your Linux environment.

If you need further assistance, feel free to ask!

Sources:
No specific sources were referenced in this response.

@thomas.altschaffel,

See the document(s) for your reference.
https://docs.aspose.com/cells/net/getting-started/#how-to-use-graphics-library-on-non-windows-platforms-with-net6
https://docs.aspose.com/cells/net/how-to-run-aspose-cells-in-docker/

Moreover, see the similar thread where the user posted similar issue and he was able to resolve it for your further reference.

@amjad.sahi,

Thank you for your response. I tried adding the Dockerfile dependencies and the runtime property as mentioned, but still it doesn’t work. Anyway, my assumption was that since Aspose.Cells version 22.10.1, System.Drawing.Common is not used any more but replaced with SkiaSharp, as mentioned here: How to Run Aspose.Cells for .Net6|Documentation

I tried then downgrading to Aspose.Cells to version 22.5 (no other version was changed) which was used in the referenced thread and indeed it works with that version. With Aspose.Cells version 24.12 I could reproduce this error with a simple .xls file that contains some text and one additional shape (e.g. rectangle) added to the sheet, so it does not seem to be related to some specific content in our real .xlsm files.
Can you check if it works on a Linux system on your side?

For now we will try using version 22.5 but what about support from your side in this case?

@thomas.altschaffel,

We are sorry that you are getting the issue using the newer version. Let us evaluate your issue thoroughly and then we will get back to you with updates/details.

@thomas.altschaffel
Please check the docker file, There is an important point to note on Linux, please refer to the following information.

# add "libfontconfig1" package if using "SkiaSharp.NativeAssets.Linux" in your project
# Or you need to use "SkiaSharp.NativeAssets.Linux.NoDependencies" in your project
RUN apt-get update && apt-get install -y libfontconfig1

The complete Docker file is as follows:

# Ubuntu 20.04
FROM mcr.microsoft.com/dotnet/runtime:6.0-focal AS base
WORKDIR /app

# add "libfontconfig1" package if using "SkiaSharp.NativeAssets.Linux" in your project
# Or you need to use "SkiaSharp.NativeAssets.Linux.NoDependencies" in your project
RUN apt-get update && apt-get install -y libfontconfig1

# Copy fonts from local to docker
# For example, put a "fonts" folder in your project folder, and put the font files in it,
# then, use the following line:
COPY fonts/ /usr/share/fonts

FROM mcr.microsoft.com/dotnet/sdk:6.0-focal AS build
WORKDIR /src
COPY ["Ubuntu_Docker.csproj", "."]
RUN dotnet restore "./Ubuntu_Docker.csproj"
COPY . .
WORKDIR "/src/."
RUN dotnet build "Ubuntu_Docker.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "Ubuntu_Docker.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Ubuntu_Docker.dll"]

In addition, the SkiaSharp referenced in version 24.12 is still 2.88.6.
Please change:

<PackageReference Include="SkiaSharp.NativeAssets.Linux" Version="3.116.1" />

To:

<PackageReference Include="SkiaSharp.NativeAssets.Linux" Version="2.88.6" />

@thomas.altschaffel ,

We can reproduce the issue with “SkiaSharp.NativeAssets.Linux” version 3.116.1.

In Aspose.Cells 24.12, the referenced SkiaSharp version is “2.88.6”. The native libSkiaSharp library(“SkiaSharp.NativeAssets.Linux”) should be compatible with the version of SkiaSharp.

For Aspose.Cells 24.12, please change “SkiaSharp.NativeAssets.Linux” version to “2.88.6” or up to “2.88.9”.

<PackageReference Include="SkiaSharp.NativeAssets.Linux" Version="2.88.6" />

Please let us know your feedback.

@peyton.xu ,
Changing to Skia 2.88.9 fixed the error.
Thanks a lot for your support!

@thomas.altschaffel
Thank you for your feedback. You are welcome. I’m glad your issue has been resolved. If you have any questions, please feel free to contact us at any time.