We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

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, IList1 #=zu83x$9FAzRce, String #=zFlFrncq6kdtQ, Matrix #=ztXJE9LY=)
at Aspose.Pdf.HtmlFragment.#=zMc15BMOLM4WE(Page #=zkNijddA=, Double #=zGiKOfGI=, Double #=z$w9Pe$S5hTr_, Double #=zWt1mcej7e2pb, IList1 #=zu83x$9FAzRce, Matrix #=ztXJE9LY=) at Aspose.Pdf.FormattedFragment.#=zOVYCjv4=(Double& #=zqH43fAc=, Double& #=zTl_W1yY=, Boolean #=zG5VRs8hgi9fI, Double #=zGiKOfGI=, Double #=zGLjwz1U=, List1 #=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

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.