ConverHtml thtorw Object reference not set to an instance of an object exception

I’ve got issue when my code execute ConvertHtml from aspose where it return Object reference not set to an instance of an object exception. But I’m not sure what causing this.

This only occured on hosted environemnt (kubernetes/linux). In my local machine (windows) it’s working well.

This is the piece of code that I use

try
{
	_logger.LogDebug("Generate File. Convert HTML to PDF - 1");
	var path = System.IO.Path.Combine(_env.ContentRootPath, "Data", "output.pdf");

	_logger.LogDebug("Generate File. Convert HTML to PDF - path: " + path);
	Aspose.Html.Saving.PdfSaveOptions options = new Aspose.Html.Saving.PdfSaveOptions();

	_logger.LogDebug("Generate File. Convert HTML to PDF - 3");
	_logger.LogDebug("Template: " + html);
	_logger.LogDebug("Path: " + path);
	_logger.LogDebug("Endpoint: " + _config.podUrl);
	Aspose.Html.Converters.Converter.ConvertHTML(html,
		_config.podUrl,
		options,
		path); // -- this code throw exception

	_logger.LogDebug("Generate File. Convert HTML to PDF - 4");
	Document doc = new Document(path);

	_logger.LogDebug("Generate File. Convert HTML to PDF - 5");
	MemoryStream outStream = new MemoryStream();

	_logger.LogDebug("Generate File. Convert HTML to PDF - 6");
	doc.Save(outStream);

	_logger.LogDebug("Generate File. Convert HTML to PDF - 7");
	outStream.Position = 0;

	//File.Delete(path);
	return outStream;
} catch (Exception ex)
{
            _logger.LogError(exception: ex, "Error: {message} {stackTrace}", ex.Message, ex.StackTrace);
            throw new Exception("Error during stream reading", ex);
}

And this is the log

2021-01-26 06:23:18.4411 [DEBUG] [27842eda-7abe-4843-a8e0-6b95e868b338] MyApp.ReceiptAppService Final Template: --censored html--
2021-01-26 06:23:18.4448 [DEBUG] [27842eda-7abe-4843-a8e0-6b95e868b338] MyApp.ReceiptAppService Generate File. Convert HTML to PDF
2021-01-26 06:23:18.4448 [DEBUG] [27842eda-7abe-4843-a8e0-6b95e868b338] MyApp.ReceiptAppService Generate File. Convert HTML to PDF - 1
2021-01-26 06:23:18.4448 [DEBUG] [27842eda-7abe-4843-a8e0-6b95e868b338] MyApp.ReceiptAppService Generate File. Convert HTML to PDF - path: /app/Data/output.pdf
2021-01-26 06:23:18.6145 [DEBUG] [27842eda-7abe-4843-a8e0-6b95e868b338] MyApp.ReceiptAppService Generate File. Convert HTML to PDF - 3
2021-01-26 06:23:18.6150 [DEBUG] [27842eda-7abe-4843-a8e0-6b95e868b338] MyApp.ReceiptAppService Template: --censored html--
2021-01-26 06:23:18.6150 [DEBUG] [27842eda-7abe-4843-a8e0-6b95e868b338] MyApp.ReceiptAppService Path: /app/Data/output.pdf
2021-01-26 06:23:18.6150 [DEBUG] [27842eda-7abe-4843-a8e0-6b95e868b338] MyApp.ReceiptAppService Endpoint: http://localpod-url
2021-01-26 07:27:04.3825 [ERROR] [8cf90bf2-46d1-4868-bf17-54108a1f91f4] MyApp.ReceiptAppService Error: Object reference not set to an instance of an object.    at Aspose.Html.Converters.Converter.ConvertHTML(String content, String baseUri, PdfSaveOptions options, String outputPath)

and some how it return afterward

2021-01-26 08:02:36.3143 [ERROR] [9196ed15-1551-47d0-a11b-8ee5862f485d] MyApp.ReceiptAppService Error: Object reference not set to an instance of an object. at . ( , Char )

Thing(s) I’ve tried

  • Check all the assets to be Always Copy ( license and css etc )
  • Check _config.podUrl is properly set
  • I tried simple html as <html><body<h1>Test</h1><body></html> but still not working

It’s turned out that I missing some dependencies for the hosted linux server.

Adding these component fix this issue

RUN apt-get update -y
&& apt-get install -y
libc6-dev
libgdiplus
libx11-dev
&& rm -rf /var/lib/apt/lists/*

@cuppyzh

It is good to know that your issue has been resolved. Yes, it is necessary to install libgdiplus and MS Fonts in the non-Windows environment to let the API work correctly.