NullReferenceException using Document.Save() in Docker Container

I am trying to save a document locally when run within a Docker Container running Linux. This piece of code works as expected when run outside of a Docker Container on Windows. We are currently using Aspose.Pdf 19.4. Perhaps I am missing a dependency in the Dockerfile?

Below is my code for Aspose PDF in C#. The exception appears when the Document.Save() method is invoked.

>              // Initialize original document
>             var document = new Document();
> 
>             // Add page
>             var page = document.Pages.Add();
> 
>             // Set paper attributes
>             page.SetPageSize(1024, 768);
>             page.PageInfo.Margin.Left = 24;
>             page.PageInfo.Margin.Right = 24;
>             page.PageInfo.Margin.Top = 80;
>             page.PageInfo.Margin.Bottom = 32;
> 
>             // Add content to page
>             var htmlFragment = new HtmlFragment(html);
>             page.Paragraphs.Add(htmlFragment);
> 
>             // Save original document
>             var exportFolder = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
>             var outputPath = Path.Combine(exportFolder+"/_Reports", "Original.pdf");
>             document.Save(outputPath);
> 
>             // Load original document, add headers and footers
>             var pdfDocument = new Document(outputPath);
> 
>             // Add page numbers
>             var pageNumberStamp = new PageNumberStamp();
>             pageNumberStamp.Background = false;
>             pageNumberStamp.Format = "Page # of " + pdfDocument.Pages.Count;
>             pageNumberStamp.BottomMargin = 16;
>             pageNumberStamp.HorizontalAlignment = HorizontalAlignment.Center;
>             pageNumberStamp.StartingNumber = 1;
>             pageNumberStamp.TextState.Font = FontRepository.FindFont("Arial");
>             pageNumberStamp.TextState.FontStyle = FontStyles.Bold;
>             pageNumberStamp.TextState.FontSize = 9;
>             var color = Color.FromRgb(System.Drawing.Color.FromArgb(132, 136, 141));
>             pageNumberStamp.TextState.ForegroundColor = color;
> 
> 
>             // Add to every page
>             foreach (Page originalPage in pdfDocument.Pages)
>             {
>                
>                     // Header
>                     var headerFirst = new HeaderFooter();
>                     var headerHtmlFragment = new HtmlFragment(htmlHeader);
>                     headerFirst.Margin.Left = 0;
>                     headerFirst.Margin.Right = 0;
>                     headerFirst.Paragraphs.Add(headerHtmlFragment);
>                     originalPage.Header = headerFirst;
> 
>                     // Footer
>                     var footerFirst = new HeaderFooter();
>                     var footerHtmlFragment = new HtmlFragment(htmlFooter);
>                     footerFirst.Margin.Left = 24;
>                     footerFirst.Margin.Right = 24;
>                     originalPage.AddStamp(pageNumberStamp);
>                     footerFirst.Paragraphs.Add(footerHtmlFragment);
>                     originalPage.Footer = footerFirst;
>                 
>             }
>             // Save updated document
>             var finalOutputPath = Path.Combine(exportFolder+"/_Reports", "Updated.pdf");
>             pdfDocument.Save(finalOutputPath);

The Dockerfile used is:

> FROM mcr.microsoft.com/dotnet/core/aspnet:2.2-stretch-slim AS base
> WORKDIR /app
> 
> # install System.Drawing native dependencies
> RUN apt-get update \
>     && apt-get install -y --allow-unauthenticated \
>         libc6-dev \
>         libgdiplus \
>         libx11-dev \
>      && rm -rf /var/lib/apt/lists/*
> 
> ENV ASPNETCORE_URLS http://+:4201
> EXPOSE 4201
> 
> FROM mcr.microsoft.com/dotnet/core/sdk:2.2-stretch AS build
> WORKDIR /src
> 
> 
> RUN dotnet restore "src/Zeus.WebApi/Zeus.WebApi.csproj"
> COPY . .
> WORKDIR "/src/src/Zeus.WebApi"
> RUN dotnet build "Zeus.WebApi.csproj" -c Release -o /app
> 
> FROM build AS publish
> RUN dotnet publish "Zeus.WebApi.csproj" -c Release -o /app
> 
> FROM base AS final
> WORKDIR /app
> COPY --from=publish /app .
> 
> ENTRYPOINT ["dotnet", "Zeus.WebApi.dll"]

If it should help, the exception message and stack trace is below.

Exception

> System.NullReferenceException: Object reference not set to an instance of an object.

Stack Trace
>    at #=zljpl9PxfTsh1Q8XAN$zauXw_j753dQ9$IqiY2$pZTCIj.#=zllAhCY8=(#=zs$A1ISb_FV_87$vwsJO03ZQ= #=zNQS1Sds=, Boolean #=zndp$Xw$RUnNK, Boolean #=zfzPX3sg=, Boolean #=zIU3GyIU=)
>    at #=zljpl9PxfTsh1Q8XAN$zauXw_j753dQ9$IqiY2$pZTCIj.#=zk1Zp6teTwcL_cAgVQw==(#=zVaDoWzSvQa$udhgm3bB3H$3M5lXX6DqwwVZKDSY= #=zEI9E3Rc=, #=zs$A1ISb_FV_87$vwsJO03ZQ= #=zNQS1Sds=, Boolean #=zIU3GyIU=, String& #=zdxm6Kv4=)
>    at #=z3tDFAvFbCZ4PoKhyWn1BZS$ewOiOiJLEmGIYUjVl4MLUBz0YFkqC753NEPCk4GBuIb$ZRpKwxSxe.#=z0eoUJ7E=(String #=zgwl95As=, #=zVaDoWzSvQa$udhgm3bB3H$3M5lXX6DqwwVZKDSY= #=zEI9E3Rc=, #=zOrqnFYPnIgXaCzlc9qItrZtDhzXiglEy15lC0RvrDmamGzEDk2HnPl8= #=z6ZPzk786UqbK, Boolean #=zfzPX3sg=, Boolean #=zIU3GyIU=, #=zrCEnSX3LfIzzTLzzU_TnAVWN5jJ1Ext6Lw==& #=z4xaGkpI=, #=zK0Zae2nT9grrdestLRwNBGjgc2JOaJKHF2P2ptt7YpEX& #=z9GdeUrppuErQ, String& #=zHuQE5x7Ld8hL)
>    at #=zyTB3XIyaBhej7MWu47CesmMV0wi7wHubh5Yfk0M4pjxsjJADD96lcIg=.#=z_qJ0k9c=(#=zyJecUYaB6pdDfKOoDlWBYgNgdz1K4P7LMlaI$aFd8WuXPBm8R5L6IblYoNqKBWQ6bMD8jJewd383wdOYTg==[] #=zU1zQYN5bwgZ_zDpCe3Rs0blrSB47, String #=zgwl95As=, #=zVaDoWzSvQa$udhgm3bB3H$3M5lXX6DqwwVZKDSY= #=zEI9E3Rc=, #=zOrqnFYPnIgXaCzlc9qItrZtDhzXiglEy15lC0RvrDmamGzEDk2HnPl8= #=z6ZPzk786UqbK, Boolean #=zfzPX3sg=, Boolean #=zIU3GyIU=, #=zrCEnSX3LfIzzTLzzU_TnAVWN5jJ1Ext6Lw==& #=zNafL$mo=, #=zK0Zae2nT9grrdestLRwNBGjgc2JOaJKHF2P2ptt7YpEX& #=z9GdeUrppuErQ, String& #=zHuQE5x7Ld8hL)
>    at #=zYa_kN$0aM62IUOhkbAze3jmzSXE9E$I5L9kgAQaiNCN3VSFrow==.#=zV$AHuxrUze87(#=zLxoTvu0MdOLCZJFxaf8KDghof1KHdNOGhA== #=zHu05vQc=, TextEditOptions #=z$0FxaJo=)
>    at Aspose.Pdf.Text.TextSegment.set_Text(String value)
>    at Aspose.Pdf.Text.TextSegment.#=zphWGfwA=(#=zyTB3XIyaBhej7MWu47CesseLHLFue9MSsVNz98vwHuxPx1KVTTNJGUQ= #=zCLS9qsxuYjE3)
>    at Aspose.Pdf.Text.TextBuilder.#=zyig9wy0=(TextFragment #=z6pi7dbfFEG$b, Int32 #=zLf13QYQ=, Boolean #=zp_$ZEfNxkO83)
>    at Aspose.Pdf.Text.TextBuilder.AppendText(TextFragment textFragment)
>    at #=zOl3hptiWFPoEBZQ3Y8pj3sR$acprTx$Fnw==`1.#=zElsGI_o=(String #=z6ZfKERQ=, PointF #=zCVXTPzk=)
>    at #=z3tQtWPX6_SXJHGdDyra8vkW2JFcNT6FNWsTIAi0=.#=zMox_mEc=(#=z5kGnU8eq_Bl7J9ZO8wj_Zded_TNizz5iAA== #=zYjAMFYa5OUot)
>    at #=z5kGnU8eq_Bl7J9ZO8wj_Zded_TNizz5iAA==.#=zPcIWOYU=(#=zoegrIHgawc1JmpO9gJMH9ygYsj3MGZw0dQ== #=z8ll1KSg=)
>    at #=zjLtT92P5zlHSu1aNdueLegNdT6oFO8sqDg==.#=zPcIWOYU=(#=zoegrIHgawc1JmpO9gJMH9ygYsj3MGZw0dQ== #=z8ll1KSg=)
>    at #=zzssONwRFXCzSiDbOU0UaXQCVQm0aLB8VgQ==.#=zPcIWOYU=(#=zoegrIHgawc1JmpO9gJMH9ygYsj3MGZw0dQ== #=z8ll1KSg=)
>    at #=zjyOb6FtMG1wa_hNer_Gdx0j5emv0YXiBUw==.#=zqTVyVvtVthVa(#=zQ38K89iDe7uagPHKa035WWI= #=zss_Vyqg0weMg$6_yFQ==, #=zsy6OTAiHiw34MCus4viqF7356yz_[] #=zCRN_vOjUa8GB, #=zLDeOHrVL3KqMBYC4rpWAeE5LxuSp #=zxurZdh8=, TimeSpan #=zGzD1eoM=)
>    at #=zuTg0aTa66WPrpf8dO7HFgAQ2VQMY.Render(#=zLDeOHrVL3KqMBYC4rpWAeE5LxuSp #=zxurZdh8=, TimeSpan #=zGzD1eoM=, #=zfpWNKASJfOKoZMy$grTDfHo=[] #=zX74B9qo=)
>    at #=zQkhdTufNNUx55sXjcfbojcpDE5TK.Render(#=zLDeOHrVL3KqMBYC4rpWAeE5LxuSp #=zxurZdh8=, #=zysH2GpM= #=zzftdz3o=, TimeSpan #=zGzD1eoM=)
>    at #=zQkhdTufNNUx55sXjcfbojcpDE5TK.Render(#=zLDeOHrVL3KqMBYC4rpWAeE5LxuSp #=zxurZdh8=, #=zysH2GpM= #=zzftdz3o=)
>    at #=zanPY6zEyoqQwj8W3$QrngBszWkRE.#=zzi80H2DDS4U9(Stream #=zzzbAU$pfHJF6, Document #=z_fzCkjo4_0p3, HtmlLoadOptions #=zT0kTbdVO5xil, IList`1 #=zu83x$9FAzRce, String #=zFlFrncq6kdtQ, Matrix #=ztXJE9LY=)
>    at Aspose.Pdf.HtmlFragment.#=zMc15BMOLM4WE(Page #=zkNijddA=, Double #=zGiKOfGI=, Double #=z$w9Pe$S5hTr_, Double #=zWt1mcej7e2pb, IList`1 #=zu83x$9FAzRce, Matrix #=ztXJE9LY=)
>    at Aspose.Pdf.FormattedFragment.#=zOVYCjv4=(Double& #=zqH43fAc=, Double& #=zTl_W1yY=, Boolean #=zG5VRs8hgi9fI, Double #=zGiKOfGI=, Double #=zGLjwz1U=, List`1 #=zA1LvAyY=, Page #=zkNijddA=, MarginInfo #=z1y1eVuo=, Table #=z4ju$vTw=, Double #=zfur9TwgcaiA3, Boolean #=zUSp2yLZkasCB)
>    at #=z6Gu3oEMf_fwueis7IsEom1s=.#=zOVYCjv4=()
>    at Aspose.Pdf.Page.#=z42oynO4=(Page #=zkNijddA=)
>    at Aspose.Pdf.Page.#=zrrvWX49tTiYZoGeuVw==()
>    at Aspose.Pdf.Document.ProcessParagraphs()
>    at Aspose.Pdf.Document.#=zc987sPODI9gV(Stream #=zUBYR2bw=, SaveOptions #=zalZVzje3NyLF)
>    at Aspose.Pdf.Document.#=z_Si59eYP6riT(String #=zRhmsRbJ2_iGd)
>    at Aspose.Pdf.Document.Save(String outputFileName)
>    at FANC.Zeus.Outputs.Jobs.ZeusReportJob.CreateZeusReportAsync(UserExport userExport) in C:\Users\raymond.dau\Documents\ZeusNew\Zeus\src\FANC.Zeus.Outputs\Jobs\ZeusReportJob.cs:line 139

@RaymondDau

Thank you for contacting support.

We have logged a ticket with ID PDFNET-46508 in our issue management system for further investigations. The ticket ID has been linked with this thread so that you will receive notification as soon as the ticket is resolved.

We are sorry for the inconvenience.

I’m facing a similar issue to this same error message and stacktrace running on docker for linux. What is the status of this ticket?

@GMarcucciBruce

Regretfully, the earlier logged ticket is not yet resolved. We have logged your concerns and will surely update you within this forum thread as soon as the issue is resolved. Please have patience and give us some time.

We are sorry for the inconvenience.

For anyone experiencing this issue, it can be solved by mounting a volume containing the fonts you use in your html and then referencing them in Aspose. See more here document.Save(string) Object reference not set to an instance of an object - #9 by shernandez068

You dont have to include every single Windows font, just the ones you use. The path in Docker HAS to be to /usr/share/fonts.

@cwhfa

Thanks for sharing the information.

It would definitely help others facing the similar issue.

Do you know when this issue will be resolved? It is a major pain for us and we are considering moving away from Aspose because of it.

@cwhfa

The investigation against this ticket is not yet completed and as soon as it is done, we will be able to share some ETA or update with you. We really apologize for the inconvenience being faced due to this issue. We highly appreciate your patience and comprehension in this regard. Please give us some time.

What happened with this ticket? Any update please?!

var msResult = new MemoryStream();
            using (var ms = new MemoryStream())
            {
                ms.Write(byteArray, 0, byteArray.Length);
                ms.Seek(0, SeekOrigin.Begin);

                Document doc = new Document(ms);

                var opt = new OptimizationOptions();
                opt.AllowReusePageContent = true;
                opt.RemoveUnusedStreams = true;
                doc.OptimizeResources(opt);

                doc.Optimize();

                doc.Save(msResult, Aspose.Pdf.SaveFormat.Pdf);
            }
            return msResult.ToArray();

I want to Linearization pdf using streams, but I got this error below:

at #=zBdTAsSW3PUvj5yztcCR1VjQ4Vsd4.#=zVAK5R1I=(#=zg6AKRCb$CKVyf4MdmaZt13dXK9F_ #=zAiA52cvNCqkC)
at #=zQz2URRfeLM1yC8C2dHNPcVs=.#=zEQim5io=(#=zg6AKRCb$CKVyf4MdmaZt13dXK9F_ #=zAiA52cvNCqkC)
at #=zijqXFdpthN9qVjUQZHl0Uqo=.#=zEQim5io=(#=zg6AKRCb$CKVyf4MdmaZt13dXK9F_ #=zAiA52cvNCqkC)
at #=q$wnTj5$DHer5MxDrQqS8KggaBWKlJLJq7iJmN3AXFbQ=.#=zeYAG4WXNwJGUrL8QPIsq$Vs=(Object #=zrhGyXAg=)
at #=q$wnTj5$DHer5MxDrQqS8KggaBWKlJLJq7iJmN3AXFbQ=.#=zP1rGEVj0rOMRl4j1Mg$edWVUJiIhMud2GjUXZCU=(MethodBase #=zrhGyXAg=, Boolean #=zA89q1zs=)
at #=q$wnTj5$DHer5MxDrQqS8KggaBWKlJLJq7iJmN3AXFbQ=.#=zfkEgnpcR8XtADmUv_6$J37INw2n8HKdi6qcwHy0=(#=q$wnTj5$DHer5MxDrQqS8KggaBWKlJLJq7iJmN3AXFbQ= #=zrhGyXAg=, #=qinp$Kh$jaikxgtQl7n06sSbrBPv$DNohRM8BT$PqLbo= #=zA89q1zs=)
at #=q$wnTj5$DHer5MxDrQqS8KggaBWKlJLJq7iJmN3AXFbQ=.#=zColLw6KLSe8B3aKix8uiuJPolT0up0mzCLRgaGJGEmwL()
at #=q$wnTj5$DHer5MxDrQqS8KggaBWKlJLJq7iJmN3AXFbQ=.#=zJG_Gh8oiuqsBdOBm9UAn416qdD69(Boolean #=zrhGyXAg=)
at #=q$wnTj5$DHer5MxDrQqS8KggaBWKlJLJq7iJmN3AXFbQ=.#=zeYAG4WXNwJGUrL8QPIsq$Vs=(Object #=zrhGyXAg=)
at #=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We6FhfudCwg=(Object[] #=zrhGyXAg=, Type[] #=zA89q1zs=, Type[] #=zOgEDiAY=, Object[] #=zrQR4xF4=)
at #=q$wnTj5$DHer5MxDrQqS8KggaBWKlJLJq7iJmN3AXFbQ=.#=zru_ueJhlhZc0H4Rxmp_W191ukr4hRoHv4A==(Stream #=zrhGyXAg=, String #=zA89q1zs=, Object[] #=zOgEDiAY=)
at #=q$wnTj5$DHer5MxDrQqS8KggaBWKlJLJq7iJmN3AXFbQ=.#=zUh2f8rnS_e5E80Pxj0TI50KQksLd4gUZJg==(Stream #=zrhGyXAg=, String #=zA89q1zs=, Object[] #=zOgEDiAY=)
at Aspose.Pdf.Document.#=z13ZvVTzHSawV(Stream #=zatgLNjc=, SaveOptions #=z$UsfP2klFDQm)
at Aspose.Pdf.Document.#=zp0nLFuDb7z1v(Stream #=z9TnI$8YAQ$d2, SaveOptions #=zXhVl28g=)
at Aspose.Pdf.Document.#=zp0nLFuDb7z1v(Stream #=z9TnI$8YAQ$d2, SaveFormat #=zs28Uvck=)
at Aspose.Pdf.Document.Save(Stream outputStream, SaveFormat format)
at dockerAppAPI.Helpers.AttachmentHelper.ConvertPDFLinearization(Byte[] byteArray) in D:\VIP\dockerAppAPI\dockerAppAPI\Helpers\AttachmentHelper.cs:line 33
at dockerAppAPI.Controllers.AttachmentController.Upload() in D:\VIP\dockerAppAPI\dockerAppAPI\Controllers\AttachmentController.cs:line 55

@mahmoud.nasser

Instead of Aspose.PDF, can you please try using Aspose.PDF.Drawing package and let us know if issue still persists.

hello,
My target is PDF Linearization, and the Aspose.Pdf.Drawing doesn’t support my case.

@mahmoud.nasser

Would you please share your sample PDF document along with sample docker file for our reference? We will test the scenario in our environment and address it accordingly.