NullReferenceException On Html To PDF

I just purchased the Aspose.Net license and trying to convert the html to pdf and we are getting error for Nullrefrence. Below is the code which I am trying to do, once memory stream started failing added code to read from file as well but I am still getting the same error,

Below is my code

 if (Document.IsLicensed)
        {
            try 
            {
                var htmlLoadOptions = new HtmlLoadOptions() { };
                htmlFile.Position = 0;
                var pdfDocument = new Document(htmlFile, htmlLoadOptions);
                var memoryStream = new MemoryStream();
                pdfDocument.Save(memoryStream);
                return memoryStream;
            }
            catch (Exception e)
            {
                // Try again with test code to see if its reading from the file correctly
                // HTML content to be written
                string _dataDir = Directory.GetCurrentDirectory() + Path.DirectorySeparatorChar;
                string htmlContent = "<!DOCTYPE html><html><body><h1>Hello World</h1></body></html>";
                string htmlFileName = _dataDir + "test.html";
                string pdfFileName = _dataDir + "html_test.PDF";

                // Write the HTML file
                await File.WriteAllTextAsync(htmlFileName, htmlContent);
                Console.WriteLine($"HTML file '{htmlFileName}' has been written.");

                // Ensure the HTML content is written and the file is closed
                await Task.Delay(1000); // This is to ensure the file write operation has completed. Adjust as necessary.

                // Convert the HTML file to a PDF
                HtmlLoadOptions options = new HtmlLoadOptions(){};
                Document pdf = new Document(htmlFileName, options); // Use the path to the HTML file
                var ms = new MemoryStream();
                pdf.Save(ms);
                return ms;
                
                Console.WriteLine($"Error converting HTML to PDF: {e.Message}");
            }
          
        }

And below is the exception I am getting.

System.NullReferenceException: Object reference not set to an instance of an object.
   at #=zn2b$atFm_T0Eqc19n0SzbF4UVz$U0L0$EkYWF_I=.#=zB8AkT4kRe4iS(#=zwKtsRxGnVDoV8lws$_y9iFx8Obx8y3LzXg== #=zka02UJI=, Char #=z_ctD2Eo=)
   at #=zn2b$atFm_T0Eqc19n0SzbF4UVz$U0L0$EkYWF_I=.#=zB8AkT4kRe4iS(#=zwKtsRxGnVDoV8lws$_y9iFx8Obx8y3LzXg== #=zka02UJI=)
   at #=zNLf95IFfifGIoaXKuRzcNLUgBD8vIjAgQQ==.#=zE7GDLDJxoJDZ(#=zNL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ae9wfAtxkq1u5wZhu$rFDfVyBKSt6g==.#=zzxrtMpg=(#=zZ9F6jh$Yuj99rOp0n8P4Pu8= #=ziur_Fx4=, #=zyharJah1NYeGbuAjJOcbwCnSUNWVdJdHNw== #=z0Gr6psE=, #=zP3YRc2y4O5VkuXt1bISDS5FDDmf4XOyvmw== #=zZjCFQ49KnigI, #=z$i_znGxgGveM7AvStnpQogU844g4nbBiXg== #=zVH$HK0$_Ypp1OcUS8w==)
   at #=z4a7mxSDNOk$$kN9qVI9rk$g3D0tqyIbjhA==.#=zJVeosKUOhAPn(#=zQsKCWdpmJ7wkp2o3l$sJrdk= #=zRZDHoyg=, #=zh8SHmjW6iw4Kfel1RbG_161YA05Q #=z360pcKgdJ_d5YODyeA==, #=zoZEMUREs4Kcx743dNm62rx0QyPle #=zP319jIg=, #=zryFLjVczZhHrYgbG4wWou7tX9G3h #=zkL9rrko=, #=zeElXDAiOTHYmNcKUlUXnWf15K$X1Oud3aBC7V3U= #=zoZZyGkJaGEwCFtpmZBpao0c=, #=zb9GOOvDKtk$uYqWBbW4oTV4= #=zT8WkWcj2d4_KhIEpXg==)
   at #=z4a7mxSDNOk$$kN9qVI9rk$g3D0tqyIbjhA==.#=zZXNnn9I=(#=zoZEMUREs4Kcx743dNm62rx0QyPle #=zP319jIg=, #=zryFLjVczZhHrYgbG4wWou7tX9G3h #=zkL9rrko=)
   at #=zryFLjVczZhHrYgbG4wWou7tX9G3h.#=zZXNnn9I=(#=zoZEMUREs4Kcx743dNm62rx0QyPle #=zP319jIg=)
   at #=zglPifHZLOYyWYZWmnPzwESmS7gYUDPsUiA==.#=zn8h8zmE=()
   at #=zglPifHZLOYyWYZWmnPzwESmS7gYUDPsUiA==.#=zKy7jjiM=()
   at #=zBC2PsSjZPJJwcaWiP37EuS6XXDP0CZH0Xw==.#=zw6$qi_QNUkxL(#=zXqNfL7C0KUgXJDe6G$GDc2E= #=zxoriNOiTMEIEySwoJg==, #=zryFLjVczZhHrYgbG4wWou7tX9G3h[] #=zNrDylOePZ90V, #=zoZEMUREs4Kcx743dNm62rx0QyPle #=zP319jIg=, CancellationToken #=zVYJ6ES0=)
   at #=zqieLAEP_uvPwWpA4TQk3JlHazprh.Render(#=zoZEMUREs4Kcx743dNm62rx0QyPle #=zP319jIg=, CancellationToken #=zVYJ6ES0=, #=zZ9F6jh$Yuj99rOp0n8P4Pu8=[] #=zDqOANy0=, #=zQsKCWdpmJ7wkp2o3l$sJrdk=[] #=zSir9JJ0=)
   at #=zqieLAEP_uvPwWpA4TQk3JlHazprh.Render(#=zoZEMUREs4Kcx743dNm62rx0QyPle #=zP319jIg=, TimeSpan #=zOpWyEz0=, #=zfSEB4g0HQ3qWGcNI4ofFsbA=[] #=znk$xM5Y=)
   at #=zQOI$ZS8rOg1eBYUPtf32fglxD41T.Render(#=zoZEMUREs4Kcx743dNm62rx0QyPle #=zP319jIg=, #=zp8ZOtxg= #=zj$kNs$U=, TimeSpan #=zOpWyEz0=)
   at #=zQOI$ZS8rOg1eBYUPtf32fglxD41T.Render(#=zoZEMUREs4Kcx743dNm62rx0QyPle #=zP319jIg=, #=zp8ZOtxg= #=zj$kNs$U=)
   at #=zd5KiagU$TAnvgbzDPxEB4x$cJD0o.#=zA0Xp0ogUbEyb(Stream #=zgv9wsGEdYyPr, Document #=z_xNLCjI1PvBI, HtmlLoadOptions #=zsWq_DOSFNcaF, String #=zvXQzR8IG13N_)
   at #=zd5KiagU$TAnvgbzDPxEB4x$cJD0o.#=zBl6Ongc=(Stream #=zgv9wsGEdYyPr, Document #=z_xNLCjI1PvBI, HtmlLoadOptions #=zsWq_DOSFNcaF, String #=zvXQzR8IG13N_)
   at #=zd5KiagU$TAnvgbzDPxEB4x$cJD0o.#=zBl6Ongc=(Stream #=zgv9wsGEdYyPr, Document #=z_xNLCjI1PvBI, HtmlLoadOptions #=zsWq_DOSFNcaF)
   at Aspose.Pdf.Document.#=znOufQ48=(Stream #=zkMw2ito=, LoadOptions #=zfoLwPss=, Boolean #=zKfLtE7wgFTwL)
   at Aspose.Pdf.Document.#=znOufQ48=(Stream #=zkMw2ito=, LoadOptions #=zfoLwPss=)
   at Aspose.Pdf.Document..ctor(Stream input, LoadOptions options)
   at TestApp.Services.PdfDownloader.ConvertHtmlToPdfAsync(Stream htmlFile) in /Users/TestUser/Work/src/TestApp/packages/serverless/shared/TestApp.Infrastructure/Services/PdfDownloader.cs:line 60

The exception is getting thrown when we try to create Document. This is currently major blocker for me can you help me figure out what might be going wrong here.

My Environment → MacOS, IDE Rider, .Net Core 6.0, Apsose Nuget Version 24.1.0

@invokesec

Can you please make sure that all Windows fonts are installed in the system? You can copy all fonts and paste them in the Fonts directory. OR you can install related package like msttcorefonts.

Furthermore, we request you please use Aspose.Pdf.Drawing NuGet Package instead of Aspose.PDF for .NET. Please uninstall existing package and install Aspose.Pdf.Drawing. If issue keep persisting after applying our suggestions, feel free to let us know.

Hey Asad thanks for reply. I have gone through the changes you mentioned. I have installed windows fonts in my directory already as I use office 365. I also removed Aspose.PDF and installed the Aspose.PDF.Drawing nuget package but I am still facing the same issue. Here is my new stack trace.

System.NullReferenceException: Object reference not set to an instance of an object.
   at #=zF0wEBEP8GMv9IQbMSJ6suFd0s54xE_Q12EdZsYQ=.#=zDVtfqPqtFPIG(#=zqbyGuvdqw6Nj1vhw7rzBlucybmGnPDkM2w== #=zS5Ddkxg=, Char #=zhgULeq4=)
   at #=zF0wEBEP8GMv9IQbMSJ6suFd0s54xE_Q12EdZsYQ=.#=zDVtfqPqtFPIG(#=zqbyGuvdqw6Nj1vhw7rzBlucybmGnPDkM2w== #=zS5Ddkxg=)
   at #=zfWlGV7dhTUFkybysb8upccrGgmfsfAsQHQ==.#=zGcQ9m12xEYvh(#=zfWlGV7dhTUFkybysb8upccrGgmfsfAsQHQ== #=z3LWvX34=, #=zkc_0Wxl_WFzMop7mdwwHhpTxehYmCe7qoYNmDRo= #=zYwfGGcE=, #=zyrX6AoWJ$Pz7z29nVhScAw6BKA8eUKa20A== #=z9WN27ow=)
   at #=zRKMoYonKIziksGFNSG85rHh1y1m_AA2z14j_XMU=..ctor(#=zfWlGV7dhTUFkybysb8upccrGgmfsfAsQHQ== #=z3LWvX34=, #=zO2WkybhK4c_xFgyTpZi4urhFDAQ5 #=zgLEux3A=, #=zkc_0Wxl_WFzMop7mdwwHhpTxehYmCe7qoYNmDRo= #=zYwfGGcE=)
   at #=zfWlGV7dhTUFkybysb8upccrGgmfsfAsQHQ==..ctor(#=zgxSewPYODIt7yTL51QuOf4E= #=zXO8DPFs=, #=zkc_0Wxl_WFzMop7mdwwHhpTxehYmCe7qoYNmDRo= #=zYwfGGcE=, #=zfWlGV7dhTUFkybysb8upccrGgmfsfAsQHQ== #=zWUqtfNWT57jF)
   at #=zfWlGV7dhTUFkybysb8upccrGgmfsfAsQHQ==..ctor(#=zkcmTRL1wdyvpC70f2JDldi8= #=z1BLLze4=, #=zfWlGV7dhTUFkybysb8upccrGgmfsfAsQHQ== #=zWUqtfNWT57jF)
   at #=zTA6ZHFDujBwK8HWVkaF$AoA81_HSLuLvlA==.#=zYxJWcP3I5C$f(#=zkcmTRL1wdyvpC70f2JDldi8= #=z1BLLze4=, #=zllZG4IaWmX8jDBPIOa_ENDtVNwtRvKhwPA== #=zbyGNDhE=, #=zuyggioqgkI16LMw0YJYd3sfCb2i$4Y1bhA== #=zENfaFoYXadtt, #=zn284P4Xx8QvW6Nkg_4FXpmHSqsX1 #=zgoT3kzk=, #=zfWlGV7dhTUFkybysb8upccrGgmfsfAsQHQ== #=zWUqtfNWT57jF, #=zsnO6NMX6h3elyG_Xo5nMIdw2xJs0fPi4xg== #=zXAzby8XvaQ10aVlGzw==)
   at #=zTA6ZHFDujBwK8HWVkaF$AoA81_HSLuLvlA==.#=zMCO3r4M=(#=zkcmTRL1wdyvpC70f2JDldi8= #=z1BLLze4=, #=zllZG4IaWmX8jDBPIOa_ENDtVNwtRvKhwPA== #=zbyGNDhE=, #=zuyggioqgkI16LMw0YJYd3sfCb2i$4Y1bhA== #=zENfaFoYXadtt, #=zsnO6NMX6h3elyG_Xo5nMIdw2xJs0fPi4xg== #=zXAzby8XvaQ10aVlGzw==)
   at #=zh9dVspePM9d$XVcqVz70YVt$0tHQp0b1qg==.#=znAd9rhDEh8Bw(#=zXvQ46jpqM914Y2Mudl4nDTo= #=zY4EImZY=, #=zja$cPAWf9RcNZMXAnLTm788PPH$r #=zl1VmSeuw3FkxXnvMqg==, #=zH12PhrVn4m36UDN0NCrQPaS_1tTN #=zTRWQ_3A=, #=zIfKMcL7UO$gk76F0JpcFR7VA$l9a #=zv8MyAb0=, #=zw6USVpOvFdJgdFd03RAmAQTIND1RJdk$kq4eg0Q= #=zEZ4QPCm6MZ$_ae0bwjMmz_8=, #=z4tPR5LqFCdOy6wUvYmJ6Zgs= #=zSMsn0YZNQxVIzMEqSA==)
   at #=zh9dVspePM9d$XVcqVz70YVt$0tHQp0b1qg==.#=zf3kguws=(#=zH12PhrVn4m36UDN0NCrQPaS_1tTN #=zTRWQ_3A=, #=zIfKMcL7UO$gk76F0JpcFR7VA$l9a #=zv8MyAb0=)
   at #=zIfKMcL7UO$gk76F0JpcFR7VA$l9a.#=zf3kguws=(#=zH12PhrVn4m36UDN0NCrQPaS_1tTN #=zTRWQ_3A=)
   at #=z1C1FQKV2i0UpowyvDImXWfJHlMvMZuXq5g==.#=zyDmcRrw=()
   at #=z1C1FQKV2i0UpowyvDImXWfJHlMvMZuXq5g==.#=zxzD5F5Q=()
   at #=zweLeqrfwES0i0Yhsm7nHuktqyJ$JCb$HqA==.#=zMu0_H4cvnYLb(#=z$anZvab749dZJWet8lE5qyo= #=zUS36zQsxuGJVWd43XQ==, #=zIfKMcL7UO$gk76F0JpcFR7VA$l9a[] #=z$YSV9SkeIlnN, #=zH12PhrVn4m36UDN0NCrQPaS_1tTN #=zTRWQ_3A=, CancellationToken #=z41D7Q0s=)
   at #=zVRed$MN3lAxbb4aBIU3hyBHOUb07.Render(#=zH12PhrVn4m36UDN0NCrQPaS_1tTN #=zTRWQ_3A=, CancellationToken #=z41D7Q0s=, #=zkcmTRL1wdyvpC70f2JDldi8=[] #=zjaORqTs=, #=zXvQ46jpqM914Y2Mudl4nDTo=[] #=zUj_hKFo=)
   at #=zVRed$MN3lAxbb4aBIU3hyBHOUb07.Render(#=zH12PhrVn4m36UDN0NCrQPaS_1tTN #=zTRWQ_3A=, TimeSpan #=z9rE$wtQ=, #=zJ7oqOrY1d0ZrP3NLdn05fl4=[] #=zUGby_kM=)
   at #=z3PsvlRiYapctkNp92cFS3WzldJ3E.Render(#=zH12PhrVn4m36UDN0NCrQPaS_1tTN #=zTRWQ_3A=, #=zbigzUFM= #=z89pO$sg=, TimeSpan #=z9rE$wtQ=)
   at #=z3PsvlRiYapctkNp92cFS3WzldJ3E.Render(#=zH12PhrVn4m36UDN0NCrQPaS_1tTN #=zTRWQ_3A=, #=zbigzUFM= #=z89pO$sg=)
   at #=zHkWRtkKogt4PyvBdPnvejcp_RIYb.#=zp63kqfsd38PB(Stream #=z7K91_tBnqjg3, Document #=zuLtr8sxWrQP_, HtmlLoadOptions #=zrgMXJiGnAK6q, String #=z6P7Y8k4mXPNT)
   at #=zHkWRtkKogt4PyvBdPnvejcp_RIYb.#=zarMHeZM=(Stream #=z7K91_tBnqjg3, Document #=zuLtr8sxWrQP_, HtmlLoadOptions #=zrgMXJiGnAK6q, String #=z6P7Y8k4mXPNT)
   at #=zHkWRtkKogt4PyvBdPnvejcp_RIYb.#=zarMHeZM=(Stream #=z7K91_tBnqjg3, Document #=zuLtr8sxWrQP_, HtmlLoadOptions #=zrgMXJiGnAK6q)
   at Aspose.Pdf.Document.#=zkyuxy3o=(Stream #=zwWjVf8Y=, LoadOptions #=zrMbwrLM=, Boolean #=z4EcuuUIH1rQe)
   at Aspose.Pdf.Document.#=zkyuxy3o=(Stream #=zwWjVf8Y=, LoadOptions #=zrMbwrLM=)
   at Aspose.Pdf.Document..ctor(Stream input, LoadOptions options)
   at CourseOps.Infrastructure.Services.PdfDownloader.ConvertHtmlToPdfAsync(Stream htmlFile) in /Users/testApp/Work/src/testApp/packages/serverless/shared/testApp.Infrastructure/Services/PdfDownloader.cs:line 60

@invokesec

We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): PDFNET-56490

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.