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(#=zNLf95IFfifGIoaXKuRzcNLUgBD8vIjAgQQ== #=zKRrS19Q=, #=z7IO6LTDr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Op0n8P4Pu8= #=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