Free Support Forum - aspose.com

NullReferenceException when hosing in lambda

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();
        }
    }

@JamieMoff

Such issues may occur due to the absence of basic fonts in the system. You should try to upload some fonts to AWS Lambda Function along with our API. Please check the following article on how to configure custom font folders and preferable font (Arial, Times New Roman, etc.) to use.

In case the issue still persists, please share complete stack trace information along with your sample HTML file in a .zip format. Also, please share which version of .NET Core are you using. We will further proceed to assist you accordingly.