The type initializer for 'Gdip' threw an exception while PDF to tiff conversion on Ubuntu container .NET 5.0

[HttpGet("document/{sourceId}")]
    public async Task<IActionResult> GetDocument([FromRoute] string sourceId, [FromQuery] string fileType)
    {
        var file = await _filesFinder.GetFile(sourceId);
        Log.Info().Message("Converting file {0}.", file.FileName).Write();

        await using var sourceFileStream = new MemoryStream(file.Content);
        await using var resultFileStream = new MemoryStream();

        using var converter = new Converter(() => sourceFileStream);
        var options = GetConvertOptions(fileType);
        converter.Convert(() => resultFileStream, options);//here
        var result = resultFileStream.ToArray();

        return new FileContentResult(result, "application/octet-stream");
    }

    private static ConvertOptions GetConvertOptions(string fileType)
    {
        return fileType switch
        {
            "pdf" => new PdfConvertOptions(),
            "tiff" => new ImageConvertOptions
            {
                Format = ImageFileType.Tiff
            },
            _ => throw new NotSupportedException($"Specified file type is not supported: '{fileType}'")
        };
    }

This code causes “The type initializer for ‘Gdip’ threw an exception” on ubuntu server while converting pdf to tiff. Furthermore conversion from same pdf to pdf causes “Object reference not set to an instance of an object.” exception. On local windows iss everything works fine.

@piotr.paszko

Please share following details and we’ll look into this scenario:

  • API version that you are using (e.g. 20.10, 22.1)
  • Container Image and a screencast/video explaining the steps to reproduce the issue
  • Sample/problematic file
  • OS details (e.g. version)

We’ve prepeared sample project converting docx to pdf. We’re facing same issue with null reference exception. At local kestrel everything works fine. We’re facing issue in docker instance built via dockerfile in project.

In this case we’re facing “Cannot convert. The file is corrupt or damaged.” exception while trying to convert that docx to tiff.

TestApi.7z (30.1 KB)

@piotr.paszko

Could you please also share the docker image as well and the environment details (where you can reproduce the issue)?

Dockerfile is in the archive I’ve sent. To reproduce this issue You have to build docker image with visual studio in project I’ve sent. We’re working with docker for windows.

@piotr.paszko

We are investigating this issue. Your investigation ticket ID is CONVERSIONNET-5230. You’ll be notified in case of any update.

@Atir_Tahir

Hello
Is there any progress in invastigating our issue?

@piotr.paszko

This issue is still under investigation. We’ll notify you in case of any progress update.

@piotr.paszko

Please add following to your docker file:

RUN apt-get update && apt-get install -y ffmpeg libgdiplus

RUN sed -i'.bak' 's/$/ contrib/' /etc/apt/sources.list
RUN apt-get update; apt-get install -y ttf-mscorefonts-installer fontconfig
RUN fc-cache -f -v 

So, your docker file must look like:

FROM mcr.microsoft.com/dotnet/aspnet:5.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

RUN apt-get update && apt-get install -y ffmpeg libgdiplus

RUN sed -i'.bak' 's/$/ contrib/' /etc/apt/sources.list
RUN apt-get update; apt-get install -y ttf-mscorefonts-installer fontconfig
RUN fc-cache -f -v

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

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

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

Let us know if issue persists.