We are using Aspose.HTML to Convert HTML content to PDF in our Lambda Functions which are hosted in Linux.
Aspose.HTML are supposed to be cross-platform but we are receiving NullReferenceException when running in Linux, we don’t encounter any error if we run the functions in windows.
Here’s the code:
public byte[] GeneratePdf(string data)
{
var license = new License();
license.SetLicense("Aspose.Total.NET.lic");
using var document = new HTMLDocument(data, string.Empty);
var options = new PdfSaveOptions {
PageSetup =
{
AnyPage = new Page(new Size(1203,403),
new Margin(0, 0, 0, 0))
}
};
using var streamProvider = new MemoryStreamProvider();
Converter.ConvertHTML(document, options, streamProvider);
return streamProvider.Streams[0].ToArray();
}
Here’s the error we got:
System.NullReferenceException: Object reference not set to an instance of an object.
at . ( , Char )
at .( , , )
at ..ctor( , IBrowsingContext , )
at ..ctor(Node , , )
at .(Element , , , , , )
at .(Element , , , )
at .(Document , RenderingOptions , IDevice , , , )
at . (IDevice , )
at .()
at . (Renderer , [] , IDevice , CancellationToken )
at Aspose.Html.Rendering.HtmlRenderer.(IDevice , CancellationToken , Element[] , Document[] )
at Aspose.Html.Rendering.HtmlRenderer.Render(IDevice device, TimeSpan timeout, HTMLDocument[] sources)
at .( , IDevice )
at . ( , ImageSaveOptions , ICreateStreamProvider )
at Aspose.Html.Converters.Converter.ConvertHTML(HTMLDocument document, ImageSaveOptions options, ICreateStreamProvider provider)
at AIG.Travel.CloudPlayground.Services.AsposeHtmlService.GeneratePng(String data) in D:\BDF\F01A\6\s\src\AIG.Travel.CloudPlayground.Services\AsposeHtmlService.cs:line 56
at AIG.Travel.CloudPlayground.Lambdas.AsposeHtml.GeneratePng(IAsposeHtmlService asposeHtmlService) in D:\BDF\F01A\6\s\src\AIG.Travel.CloudPlayground\Lambdas\AsposeHtml.cs:line 25
at AIG.Travel.CloudPlayground.Lambdas.AsposeHtml_GeneratePng_Generated.GeneratePng(APIGatewayProxyRequest __request__, ILambdaContext __context__) in D:\BDF\F01A\6\s\src\AIG.Travel.CloudPlayground\obj\Release\net8.0\linux-x64\generated\Liberty.Logging.Lambda.Annotations.SourceGenerator\Liberty.Logging.Lambda.Annotations.SourceGenerator.Generator\AsposeHtml_GeneratePng_Generated.g.cs:line 63
at lambda_method1(Closure, Stream, ILambdaContext, Stream)
at Amazon.Lambda.RuntimeSupport.HandlerWrapper.<>c__DisplayClass8_0.<GetHandlerWrapper>b__0(InvocationRequest invocation) in /src/Repo/Libraries/src/Amazon.Lambda.RuntimeSupport/Bootstrap/HandlerWrapper.cs:line 54
at Amazon.Lambda.RuntimeSupport.LambdaBootstrap.InvokeOnceAsync(CancellationToken cancellationToken) in /src/Repo/Libraries/src/Amazon.Lambda.RuntimeSupport/Bootstrap/LambdaBootstrap.cs:line 185