I have an app that takes in html as a string and returns a pdf as a byte[] locally this works but when hosting in lambda I get a NullReferenceException from the ConvertHTML method.
public byte[] ConvertHtmlToPdf(string data)
{
using (var streamProvider = new MemoryStreamProvider())
{
// Initialize an HTML document
using (var document = new Aspose.Html.HTMLDocument(data, "."))
{
var pdfSaveOptions = new Aspose.Html.Saving.PdfSaveOptions();
// Convert HTML to PDF by using the MemoryStreamProvider
LambdaLogger.Log($"-----{data}");
Aspose.Html.Converters.Converter.ConvertHTML(document, pdfSaveOptions, streamProvider);
// Get access to the memory stream that contains the result data
var memory = streamProvider.Streams.First();
memory.Seek(0, SeekOrigin.Begin);
return memory.ToArray();
}
}
}
public class MemoryStreamProvider : Aspose.Html.IO.ICreateStreamProvider
{
// List of MemoryStream objects created during the document rendering
public List<MemoryStream> Streams { get; } = new List<MemoryStream>();
public Stream GetStream(string name, string extension)
{
// This method is called when the only one output stream is required, for instance for XPS, PDF or TIFF formats.
MemoryStream result = new MemoryStream();
Streams.Add(result);
return result;
}
public Stream GetStream(string name, string extension, int page)
{
// This method is called when the creation of multiple output streams are required. For instance during the rendering HTML to list of the image files (JPG, PNG, etc.)
MemoryStream result = new MemoryStream();
Streams.Add(result);
return result;
}
public void ReleaseStream(Stream stream)
{
// Here You can release the stream filled with data and, for instance, flush it to the hard-drive
}
public void Dispose()
{
// Releasing resources
foreach (var stream in Streams)
stream.Dispose();
}
}