Trial version doesn't work in linux containers (NullReferenceException after document.Save())

Hello,

I am trying to edit pdf file via trial version of Aspose.Pdf to explore functionality for project needs. But it doesn’t want to run correctly and NullReferenceException occurs.
I’ve tried to use dockerfile from aspose official site (spoiler it doesn’t work for me):

FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
RUN apt-get update && apt-get install -y libgdiplus
RUN sed -i'.bak' 's/$/ contrib/' /etc/apt/sources.list
RUN apt-get update; apt-get install -y ttf-mscorefonts-installer fontconfig

WORKDIR "/app"
EXPOSE 80 82 84
ENV ASPNETCORE_URLS=http://+:80;http://+:82;http://+:84
COPY "./artifacts/app" "."
ENTRYPOINT ["dotnet", "SampleProject.dll"]

Now I am trying to experiment with additional libs (spoiler: sadly it doesn’t work too):

FROM mcr.microsoft.com/dotnet/aspnet:6.0
RUN apt-get update \
    && apt-get install -y --allow-unauthenticated \
        locales locales-all \
        fontconfig \
        libc6-dev \
        libgdiplus \
    	ttf-mscorefonts-installer \
        libx11-dev \
    && rm -rf /var/lib/apt/lists/* \
    && fc-cache -f -v

WORKDIR "/app"
EXPOSE 80 82 84
ENV ASPNETCORE_URLS=http://+:80;http://+:82;http://+:84
COPY "./artifacts/app" "."
ENTRYPOINT ["dotnet", "SampleProject.dll"]

When I try to create new pdf:

var d = new Document();
var p = d.Pages.Add();
p.Paragraphs.Add(new TextFragment("Hello World!"));
var ms = new MemoryStream();
d.Save(ms);

return ms.ToArray();

I got exception:

    System.ArgumentNullException: Value cannot be null. (Parameter 'key')
    	at System.Collections.Generic.Dictionary`2.FindValue(TKey key)
    	at System.Collections.Generic.Dictionary`2.TryGetValue(TKey key, TValue& value)
    	at #=zqn$fLDCyLGRr8pu2uUNDw91Cghzzuh4SOw==.#=z8B2_dYQ=(#=zVw4Kmpo= #=ze$nSJ1c=)
    	at #=zvP80FvsbIYWEjk4y0$u1K7fSruiME656xQ==.#=zSB_8WahestFL.#=zceHUMjm1p8Ka(#=zVw4Kmpo= #=ze$nSJ1c=, #=z_RXnnCY=& #=zj4PCPBc=)
    	at #=zSSCy0lmpQTJJyCEjUNDbboIErkV427mkgHFP1js=.#=zdUTbZ7WbCDyC(#=zfLB4U9tmn6cS3dbXTUNyCg6q51Xy #=zMiqcpTKMe$oL)
    	at #=zrB3Wryav4TRWIsccmCRA$5K0T8wj$8tvsFJk3X8f62G4.#=zUZNGbxn5pIEY()\n   at #=zrB3Wryav4TRWIsccmCRA$5K0T8wj$8tvsFJk3X8f62G4.#=zyxshG1k=()
    	at #=zW5Xa4oeC9xkdrg53c7m_9ET9PbQ2U4fMlLwzl5UQCuB6.#=zEyZT$u4=(#=zoNYLGirF2GZ_26BfjxGA0yHDcIlX #=zaVZxQu0=, #=zJ7mqFOeX8tyl7GEYBXHhthL_dFsmFOSHOA== #=zugiTLEs=, #=z5wmixPgpid2CI$g7rMTfGvRodRnNvyTQCLJbq93Lh3sL #=zUdQiiDs=)
    	at Aspose.Pdf.Page.CalculateContentBBox()\n   at Aspose.Pdf.Page.#=zdDezmno=(Page #=zugiTLEs=)
    	at Aspose.Pdf.Document.ProcessParagraphs()\n   at Aspose.Pdf.Document.#=zfHSRt87og8RA(Stream #=zdOSrDSY=, SaveOptions #=zgp3uxjz$DQt5)
    	at Aspose.Pdf.Document.Save(Stream output)

If I try to open pdf and edit it:

await using var inputPdf = new MemoryStream(request.Pdf);
using var pdfDocument = new Document(inputPdf);
                
var page = pdfDocument.Pages[1];
page.PageInfo.Margin = new MarginInfo(10, 10, 10, 10);

await using var outputPdf = new MemoryStream();
pdfDocument.Save(outputPdf);
                
return outputPdf.ToArray();

I got this exception:

    System.NullReferenceException: Object reference not set to an instance of an object.
    	at #=z1NC5bGfOtG5Spahf93YivZn6$0GD.#=zVUCl61U=(#=zfLB4U9tmn6cS3dbXTUNyCg6q51Xy #=zMiqcpTKMe$oL)
    	at #=z5cKxuexnqDQ9RNwZ23esmHg=.#=zXXYY2sg=(#=zfLB4U9tmn6cS3dbXTUNyCg6q51Xy #=zMiqcpTKMe$oL)
    	at #=qbuqhvGzuec_0VaILCCKCpK9HKNen9454QCJx5O09gR0=.#=z0PzUBkyvU_7nJn$0Ll2UExL0hUNP6BuZWpUh$po=(Object #=zN8L8sZ8=)
    	at #=qbuqhvGzuec_0VaILCCKCpK9HKNen9454QCJx5O09gR0=.#=zR7S2UpSDvbHBXj8jqC6vNcRoLEag1quTlj7EYbg=(MethodBase #=zN8L8sZ8=, Boolean #=zvhFewtQ=)
    	at #=qbuqhvGzuec_0VaILCCKCpK9HKNen9454QCJx5O09gR0=.#=z3v2khBATe2hlZ$0yWVYKHik=(#=qbuqhvGzuec_0VaILCCKCpK9HKNen9454QCJx5O09gR0= #=zN8L8sZ8=, #=q41WHVdZY0DOqLHlgkNSlGKXYqAqBI7PyEcC9rXx9BxM= #=zvhFewtQ=)
    	at #=qbuqhvGzuec_0VaILCCKCpK9HKNen9454QCJx5O09gR0=.#=zrgv_EvFdDLuTSPmrurQq5D7Z4kRYLm0qLw==()
    	at #=qbuqhvGzuec_0VaILCCKCpK9HKNen9454QCJx5O09gR0=.#=zSN9fxqikJMkRcO0m1d3eLN_G1KITEhhjgQ==(Boolean #=zN8L8sZ8=)
    	at #=qbuqhvGzuec_0VaILCCKCpK9HKNen9454QCJx5O09gR0=.#=z0PzUBkyvU_7nJn$0Ll2UExL0hUNP6BuZWpUh$po=(Object #=zN8L8sZ8=)
    	at #=qbuqhvGzuec_0VaILCCKCpK9HKNen9454QCJx5O09gR0=.#=z9T7L214txsC0PfsJdQwvKAV_OsMeuktmDOQjHM$gLHFi()
    	at #=qbuqhvGzuec_0VaILCCKCpK9HKNen9454QCJx5O09gR0=.#=z4grFK9RgbVH87fBrzyiA8RGF5sKBw9z$4A==(#=qbuqhvGzuec_0VaILCCKCpK9HKNen9454QCJx5O09gR0= #=zN8L8sZ8=, #=q41WHVdZY0DOqLHlgkNSlGKXYqAqBI7PyEcC9rXx9BxM= #=zvhFewtQ=)
    	at #=qbuqhvGzuec_0VaILCCKCpK9HKNen9454QCJx5O09gR0=.#=zrgv_EvFdDLuTSPmrurQq5D7Z4kRYLm0qLw==()
    	at #=qbuqhvGzuec_0VaILCCKCpK9HKNen9454QCJx5O09gR0=.#=zSN9fxqikJMkRcO0m1d3eLN_G1KITEhhjgQ==(Boolean #=zN8L8sZ8=)
    	at #=qbuqhvGzuec_0VaILCCKCpK9HKNen9454QCJx5O09gR0=.#=z0PzUBkyvU_7nJn$0Ll2UExL0hUNP6BuZWpUh$po=(Object #=zN8L8sZ8=)
    	at #=qbuqhvGzuec_0VaILCCKCpK9HKNen9454QCJx5O09gR0=.#=z9T7L214txsC0PfsJdQwvKAV_OsMeuktmDOQjHM$gLHFi()
    	at #=qbuqhvGzuec_0VaILCCKCpK9HKNen9454QCJx5O09gR0=.#=zwOSkuMu6iA_xoe9Wva_YfPY1uT07(Object #=zN8L8sZ8=, UInt32 #=zvhFewtQ=)
    	at #=qbuqhvGzuec_0VaILCCKCpK9HKNen9454QCJx5O09gR0=.#=zSN9fxqikJMkRcO0m1d3eLN_G1KITEhhjgQ==(Boolean #=zN8L8sZ8=)
    	at #=qbuqhvGzuec_0VaILCCKCpK9HKNen9454QCJx5O09gR0=.#=zSN9fxqikJMkRcO0m1d3eLN_G1KITEhhjgQ==(Boolean #=zN8L8sZ8=)
    	at #=qbuqhvGzuec_0VaILCCKCpK9HKNen9454QCJx5O09gR0=.#=zy_lRdo2RWswSyYeeoCZXhLXi6YstPKBOm2KdKl4=(Object[] #=zN8L8sZ8=, Type[] #=zvhFewtQ=, Type[] #=z2kCS$PI=, Object[] #=zAPuDuog=)
    	at #=qbuqhvGzuec_0VaILCCKCpK9HKNen9454QCJx5O09gR0=.#=zZ7FoPd7sa6aOKR36srVv_JafqThBqPsXLw==(Stream #=zN8L8sZ8=, String #=zvhFewtQ=, Object[] #=z2kCS$PI=)\n   at #=qbuqhvGzuec_0VaILCCKCpK9HKNen9454QCJx5O09gR0=.#=zDVjo1QWQXxwBunhL9pM8RD2gAGmWd2YDojWzbNs=(Stream #=zN8L8sZ8=, String #=zvhFewtQ=, Object[] #=z2kCS$PI=)
    	at Aspose.Pdf.Document.#=zfHSRt87og8RA(Stream #=zdOSrDSY=, SaveOptions #=zgp3uxjz$DQt5)
    	at Aspose.Pdf.Document.Save(Stream output)

I don’t know is problem depends on fonts or it’s trial version behavior or something else.
Please let me know how I can fix it. I would like to use licensed version but I have to test trial version before and should know that it will be work fine in linux docker containers.

P.S. It looks like this is the same problem as this thread

@prandy

You are using very simple code example and it works at Linux and Windows. We tested your both cases and have not faced any issue.

To ensure a timely and accurate response, please create Visual Studio docker application that help us to reproduce the same issue at our end. We will then investigate this issue and provide you more information on it. Thanks for your cooperation.

@tahir.manzoor
I figured out what is probably the problem. It will not work with .net 6 because System.Drawing.Common is not supported for non-windows applications (Breaking change: System.Drawing.Common only supported on Windows - .NET | Microsoft Learn).
It looks like only .net 5 works for linux containers. Also I’ve seen some pdf libraries like PdfSharp uses SixLabors.ImageSharp as 2d graphic library for non windows environment. So I guess Aspose.Pdf should use something the same to run it on non windows applications and .net 6+.
Please test this on .net 6 and might be you can suggest a solution. As a fast solution it can be downgrade to .net 5 but this problem will still be actual for the next .net vesions.

From this link: Breaking change: System.Drawing.Common only supported on Windows - .NET | Microsoft Learn

There is the way to run it on .net 6 in linux. You need to add runtimeconfig.template.json файл to your project and content for this file will be

{
  "configProperties": {
    "System.Drawing.EnableUnixSupport": true
   }
}

so after project build you will get in bin folder file with name [application_name].runtimeconfig.json
In my example the file content will be:

{
      "runtimeOptions": {
        "tfm": "net6.0",
        "frameworks": [
          {
            "name": "Microsoft.NETCore.App",
            "version": "6.0.0"
          },
          {
            "name": "Microsoft.AspNetCore.App",
            "version": "6.0.0"
          }
        ],
        "configProperties": {
          "System.Drawing.EnableUnixSupport": true,
          "System.GC.Server": true,
          "System.Runtime.Serialization.EnableUnsafeBinaryFormatterSerialization": false
        }
      }
    }

make sure that row “System.Drawing.EnableUnixSupport”: true appear now at this file in correct place.
So now the code will run correctly in .net 6 linux container. But microsoft add this option (System.Drawing.EnableUnixSupport) just for give a time to library devs to migrate from System.Drawing.Common to something cross-platform. And it will not work in .net 7

@prandy

It is nice to hear from you that your problem has been solved. We have logged an investigation ticket as PDFNET-51906 in our issue tracking system. You will be notified via this forum thread once there is an update available on it.

We apologize for your inconvenience.