NullReferenceException in C#.NET Aspose.PDF.Save()

Hi everyone,

Aspose.PDF throws a NullReferenceException when saving a document as a stream on my integration environement (into a docker container), even though it works well locally… I’ve tried to change the Aspose version (18.12, 20.6, 20.7) but the problem persists.

Here is the code:

var file = new Aspose.Pdf.Document(Path.ChangeExtension(doc.File.Path, "pdf"));

// Convert document to base 64 string
string base64FileToSign;
using (var outStream = new MemoryStream())
{
    Console.WriteLine("Is outStream null?");
    Console.WriteLine(outStream == null); // False
    
    Console.WriteLine("Is file null?");
    Console.WriteLine(file == null); // False
    
    file.Save(outStream, SaveFormat.Pdf); // NullReferenceException
    base64FileToSign = Convert.ToBase64String(outStream.ToArray());
}

Here is the exception stack trace:

10/08/2020 12:14:38System.NullReferenceException: Object reference not set to an instance of an object.
10/08/2020 12:14:38   at #=znHA95grzTGE4zq2AFeMfRCq0zqR5.#=z7AaEsao=(#=zp_V_l1Mvkg23muomG$JkgY69t8EL #=zWS2rR7liNJQN)
10/08/2020 12:14:38   at #=z2MMIu0$I1wO8R$unHBrdweI=.#=zOMbIeJM=(#=zp_V_l1Mvkg23muomG$JkgY69t8EL #=zWS2rR7liNJQN)
10/08/2020 12:14:38   at #=qup3TNXHhuiQKW2jaXaI6m_PL7xxT2H8rB4o6I3GarCY=.#=zBjBIJohMYi39KZobqD48PL57rokXxn8dcUTh2saBjm6a(Object #=z$Mn61Qs=)
10/08/2020 12:14:38   at #=qup3TNXHhuiQKW2jaXaI6m_PL7xxT2H8rB4o6I3GarCY=.#=z9z6sSRGn946xniAuUfZnAqWnnsYb2EY77IQs_dLNjupa(MethodBase #=z$Mn61Qs=, Boolean #=zJKSNvTo=)
10/08/2020 12:14:38   at #=qup3TNXHhuiQKW2jaXaI6m_PL7xxT2H8rB4o6I3GarCY=.#=zIXUtMI8o3kj5iO4W5zeRxF3Fbx$t(#=qup3TNXHhuiQKW2jaXaI6m_PL7xxT2H8rB4o6I3GarCY= #=z$Mn61Qs=, #=qVNVRJVzHhGDD2q4TVWbHKniZLImufmg8LjJ$5FRCnZY= #=zJKSNvTo=)
10/08/2020 12:14:38   at #=qup3TNXHhuiQKW2jaXaI6m_PL7xxT2H8rB4o6I3GarCY=.#=zqpUKT$yINYYW2boIGIUQs76bfk03ThIqYDL1ECY=()
10/08/2020 12:14:38   at #=qup3TNXHhuiQKW2jaXaI6m_PL7xxT2H8rB4o6I3GarCY=.#=zwAkouQ_qu9bo4qEkhuQ6_5aQy8zHMT2kHRpl$gI=(Boolean #=z$Mn61Qs=)
10/08/2020 12:14:38   at #=qup3TNXHhuiQKW2jaXaI6m_PL7xxT2H8rB4o6I3GarCY=.#=zBjBIJohMYi39KZobqD48PL57rokXxn8dcUTh2saBjm6a(Object #=z$Mn61Qs=)
10/08/2020 12:14:38   at #=qup3TNXHhuiQKW2jaXaI6m_PL7xxT2H8rB4o6I3GarCY=.#=z3NvYiMARoMDmOVcczX$mj1bJgfatgt0C7g==()
10/08/2020 12:14:38   at #=qup3TNXHhuiQKW2jaXaI6m_PL7xxT2H8rB4o6I3GarCY=.#=zAweICui3kTJVcjrF19v7nLoJdtEEiuEdllfDcAtkrpOI(#=qup3TNXHhuiQKW2jaXaI6m_PL7xxT2H8rB4o6I3GarCY= #=z$Mn61Qs=, #=qVNVRJVzHhGDD2q4TVWbHKniZLImufmg8LjJ$5FRCnZY= #=zJKSNvTo=)
10/08/2020 12:14:38   at #=qup3TNXHhuiQKW2jaXaI6m_PL7xxT2H8rB4o6I3GarCY=.#=zqpUKT$yINYYW2boIGIUQs76bfk03ThIqYDL1ECY=()
10/08/2020 12:14:38   at #=qup3TNXHhuiQKW2jaXaI6m_PL7xxT2H8rB4o6I3GarCY=.#=zwAkouQ_qu9bo4qEkhuQ6_5aQy8zHMT2kHRpl$gI=(Boolean #=z$Mn61Qs=)
10/08/2020 12:14:38   at #=qup3TNXHhuiQKW2jaXaI6m_PL7xxT2H8rB4o6I3GarCY=.#=zBjBIJohMYi39KZobqD48PL57rokXxn8dcUTh2saBjm6a(Object #=z$Mn61Qs=)
10/08/2020 12:14:38   at #=qup3TNXHhuiQKW2jaXaI6m_PL7xxT2H8rB4o6I3GarCY=.#=z3NvYiMARoMDmOVcczX$mj1bJgfatgt0C7g==()
10/08/2020 12:14:38   at #=qup3TNXHhuiQKW2jaXaI6m_PL7xxT2H8rB4o6I3GarCY=.#=zkk673iowztOmudof1092hBTQ5i0gREXTVvdO0yU=(Object #=z$Mn61Qs=, UInt32 #=zJKSNvTo=)
10/08/2020 12:14:38   at #=qup3TNXHhuiQKW2jaXaI6m_PL7xxT2H8rB4o6I3GarCY=.#=zwAkouQ_qu9bo4qEkhuQ6_5aQy8zHMT2kHRpl$gI=(Boolean #=z$Mn61Qs=)
10/08/2020 12:14:38   at #=qup3TNXHhuiQKW2jaXaI6m_PL7xxT2H8rB4o6I3GarCY=.#=zwAkouQ_qu9bo4qEkhuQ6_5aQy8zHMT2kHRpl$gI=(Boolean #=z$Mn61Qs=)
10/08/2020 12:14:38   at #=qup3TNXHhuiQKW2jaXaI6m_PL7xxT2H8rB4o6I3GarCY=.#=zWRAdnz8251txzBN$LFq7XQPUrE4Fh3mPiMG_Qes=()
10/08/2020 12:14:38   at #=qup3TNXHhuiQKW2jaXaI6m_PL7xxT2H8rB4o6I3GarCY=.#=zG4fiMqbltwqWs_OfflMoxO0=(Object[] #=z$Mn61Qs=, Type[] #=zJKSNvTo=, Type[] #=zq2vrlDc=, Object[] #=ziBeFMlc=)
10/08/2020 12:14:38   at #=qup3TNXHhuiQKW2jaXaI6m_PL7xxT2H8rB4o6I3GarCY=.#=zchUqj7_c6tPX9MQYb9Jp0bu7qdvOzeIdhC3i8oI=(Stream #=z$Mn61Qs=, String #=zJKSNvTo=, Object[] #=zq2vrlDc=, Type[] #=ziBeFMlc=, Type[] #=zhSSQU60=, Object[] #=zeB_H6no=)
10/08/2020 12:14:38   at #=qup3TNXHhuiQKW2jaXaI6m_PL7xxT2H8rB4o6I3GarCY=.#=zJNWbikYCiYNO1zFmNfjoV2Kr$M_NFVMSMHMgexU=(Stream #=z$Mn61Qs=, String #=zJKSNvTo=, Object[] #=zq2vrlDc=)
10/08/2020 12:14:38   at #=qup3TNXHhuiQKW2jaXaI6m_PL7xxT2H8rB4o6I3GarCY=.#=zvFMKSW3OhAQ9CoKsILgNSOijQPhItCXZXg==(Stream #=z$Mn61Qs=, String #=zJKSNvTo=, Object[] #=zq2vrlDc=)
10/08/2020 12:14:38   at Aspose.Pdf.Document.#=zLLKL3S0IMlcC(Stream #=z4LBTcEU=, SaveOptions #=zm6V_S53fBIO_)
10/08/2020 12:14:38   at Aspose.Pdf.Document.#=zf69xgfS16u1o(Stream #=zg8bde0B4t1Vn, SaveOptions #=zRFkXoHU=)
10/08/2020 12:14:38   at Aspose.Pdf.Document.Save(Stream outputStream, SaveFormat format)
10/08/2020 12:14:38   at DDocs_reforged.Service.ElectronicSignature.ElectronicSignatureService.SendDocumentToDS(Int64 userId, String userEmail, DgdDocument doc) in /src/DDocs_reforged.Service/ElectronicSignature/ElectronicSignatureService.cs:line 112
10/08/2020 12:14:38   at DDocs_reforged.Api.Controllers.ElectronicSignatureController.SendDocumentToDS(String documentId) in /src/DDocs_reforged.Api/Controllers/ElectronicSignatureController.cs:line 105
10/08/2020 12:14:38   at lambda_method(Closure , Object )
10/08/2020 12:14:38   at Microsoft.Extensions.Internal.ObjectMethodExecutorAwaitable.Awaiter.GetResult()
10/08/2020 12:14:38   at Microsoft.AspNetCore.Mvc.Internal.ActionMethodExecutor.AwaitableObjectResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
10/08/2020 12:14:38   at System.Threading.Tasks.ValueTask`1.get_Result()
10/08/2020 12:14:38   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeActionMethodAsync()
10/08/2020 12:14:38   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeNextActionFilterAsync()
10/08/2020 12:14:38   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Rethrow(ActionExecutedContext context)
10/08/2020 12:14:38   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
10/08/2020 12:14:38   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeInnerFilterAsync()
10/08/2020 12:14:38   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextExceptionFilterAsync()

I’ve seen this kind of issue (deep Aspose exceptions during PDF saving) elsewhere on the forum, but no solution/explanation is given, while this seem to be an Aspose related issue…

-> Does anyone has a solution, an explanation, or even a track? :stuck_out_tongue:

Thanks in advance.

@LeoRapaic

Could you kindly share if you are facing this issue with particular type of PDF files or all PDF documents you are processing in docker. Please try copying/installing all windows fonts in your docker container and test again. In case issue still persists, please share your docker file along with sample PDF document. We will test the scenario in our environment and address it accordingly.

Hi Asad,

Thanks for your answer :slight_smile:

I’ve created a sample app that reprodues my issue: https://github.com/LeoRpc/AsposeSaveBug.

To answer your questions

  • Concerning the PDF file: I am facing this issue with all the PDF I’ve tested. Note that this PDF is
    created from a .doc file using Aspose.Word (you will see in my sample app).
  • Concerning the fonts, the documentations available on aspose forum refers to Aspose Word, but I face issues with Aspose.PDF… Beside, since the PDF is created from a word using Aspose Word, it might not be a font issue?

Best.

@LeoRapaic

Thanks for sharing requested details.

We have logged an investigation ticket as PDFNET-48639 in our issue tracking system for this case. We will investigate it in details and inform you as soon as the ticket is resolved.

Aspose.PDF requires all fonts to be installed/present in the system while processing a PDF document. Would you kindly try to copy/install all your existing Windows Fonts (where the scenario is working successfully) in your docker environment and try again? Please also make sure to use a valid license. We will let you know as soon as we have some updates regarding ticket resolution. Please spare us some time.

PS: Initially the Stack Trace shows that API is unable to find fonts.

Hi,

Coming back from holidays, I tried to install the fonts in my docker container using the following ways (I also updated my test application on Github) but it does not work.

@asad.ali could you please double check that the issue comes from missing fonts? In fact if so called windows fonts are needed on linux, they would be added as dependency?
If font is the issue, could you please provide me a Dockerfile (or other) to allow my test application to work? Otherwise, could you provide me some specific documentation about fonts management with Aspose.PDF? I assume I use standards docker images (since you use the same in your documentation), and followed the availiable ressources, so this should work, but it does not…

  • We are planning to upgrade our Aspose licence from 18 to 20, but this is a major issue for us.

Thanks in advance.


Solution I’ve tried

See also my test application on GitHub: https://github.com/LeoRpc/AsposeSaveBug

  1. SOLUTION 1 from Aspose documentation (Install TrueType Fonts on Linux in C#|Aspose.Words for .NET)

I copied all fonts (but left only Arial into GitHub) from C:/Windows/Fonts to a Fonts folder in my projet and added this to my docker file:

WORKDIR /fonts
COPY Fonts .

then specify the fonts path in my Program.cs:

// 0. Specify fonts path (SOLUTION 0)
FontSettings.DefaultInstance.SetFontsFolder("/fonts", true); // From Aspose.Word...

but unsurprisingly throw same exception as before. In fact, the FontSettings object comes from Aspose.Word, and I found no way to do the same think on aspose.PDF except this (Using custom font folder - #3 by tilal.ahmad) which does not work.

  1. SOLUTION 2 from Aspose documentation (Install TrueType Fonts on Linux in C#|Aspose.Words for .NET)

Added to my dockerfile:

RUN echo "deb http://us-west-2.ec2.archive.ubuntu.com/ubuntu/ trusty multiverse\ndeb http://us-west-2.ec2.archive.ubuntu.com/ubuntu/ trusty-updates multiverse\ndeb http://us-west-2.ec2.archive.ubuntu.com/ubuntu/ trusty-backports main restricted universe multiverse" | tee /etc/apt/sources.list.d/multiverse.list
RUN apt-get update && apt-get install ttf-mscorefonts-installer

but output:

The following packages have unmet dependencies:
 ttf-mscorefonts-installer : Depends: update-notifier-common (>= 0.119ubuntu2) but it is not installable
                             Recommends: fonts-liberation but it is not going to be installed
  1. SOLUTION 3 from the apt-get output

Added to my dockerfile

RUN apt-get update && apt-get install -y apt-utils
RUN apt-get install -y fonts-liberation

but throw same exception as before.

  1. SOLUTION 4 from Debian documentation about TrueType fonts (TrueType - Debian Wiki)

Added to my dockerfile

RUN apt-get update && apt-get install -y apt-utils
RUN apt-get install -y ttf-liberation

but throw same exception as before.

  1. SOLUTION 5 from Aspose documentation (How to Run Aspose.Words in Docker|Aspose.Words for .NET) - unable to find same article for Asose PDF

Added to my dockerfile:

RUN apt-get update && apt-get install -y libfontconfig1

but throw same exception as before.

@LeoRapaic

We need to investigate this case in further details in order to provide you proper feedback and a solution to the issue that you are facing. We have updated the ticket information logged previously and investigate it as per provided details. We will inform you as soon as it is resolved. Please be patient and spare us some time.

We are sorry for the inconvenience.

We are also facing the same issue. Do you have any updates on this?

@sarathisathish90

We are afraid that the earlier logged ticket is not yet resolved. However, your concerns have been recorded and we will surely inform you once we have some updates in this regard.

We apologize for the inconvenience.