Convert HTMLString to PDF using .net core 6 and Aspose pdf version 23.8.0 -Null Reference Exception

                string html = "<div><p>Aspose PDF</p><p>Test Content</p></div>";

                Document document = new Document();
                Page page = document.Pages.Add();

                HtmlFragment htmlFragment = new HtmlFragment(html);
                page.Paragraphs.Add(htmlFragment);

                MemoryStream docStream = new MemoryStream();
                document.Save(docStream);

The exception I am getting on document.Save(docStream);

I also use this code

            string html = "<div><p>Aspose PDF</p><p>Test Content</p></div>";
            byte[] byteArray = Encoding.UTF8.GetBytes(html);
            MemoryStream pdfStream = new MemoryStream(byteArray);
            HtmlLoadOptions option = new HtmlLoadOptions();
            Aspose.Pdf.Document pdfDocument = new Aspose.Pdf.Document(pdfStream,option);
            pdfDocument.Save(pdfStream);

in this code getting “Memory stream is not expandable” error on pdfDocument.Save(pdfStream)

@ravina

Instead of Aspose.PDF for .NET, can you please try using Aspose.Pdf.Drawing for .NET in .NET 6.0 and let us know in case issue still persists? We will further proceed to assist you accordingly.

I have installed Aspose.PDF.Drawing and un installed Aspose.PDF but not working.
same error
please share some example for generating pdf with html string

@ravina

We tested using the latest version of the API in our environment and we could not replicate the issue. Can you please share some more details like your operating system, etc.? If possible, can you please share a sample console application with us that can reproduce the issue? We will test the scenario in our environment and address it accordingly.

    Converter.ConvertHTML(@"<h1>Convert HTML to PDF!</h1>", ".", new Aspose.Html.Saving.PdfSaveOptions(), System.IO.Path.Combine(@"E:\", "convert-with-single-line.pdf"));

When i used this Aspose.Html 23.9.0, get error “System.NullReferenceException
HResult=0x80004003
Message=Object reference not set to an instance of an object.
Source=Aspose.HTML”

            string html = "<div><p>Aspose PDF</p><p>Test Content</p></div>";
            byte[] byteArray = Encoding.ASCII.GetBytes(html);
            MemoryStream mStream = new MemoryStream(byteArray);

            Aspose.Words.Document document = new Aspose.Words.Document();
            DocumentBuilder docBuilber = new DocumentBuilder(document);
            docBuilber.InsertHtml(html);

            MemoryStream docStream = new MemoryStream();
            document.Save(mStream, Aspose.Words.SaveFormat.Pdf);

when i used Aspose.Words(23.09.0) for convert HtmlString into pdf , Get error " ex|{System.NotSupportedException: Memory stream is not expandable. at System.IO.MemoryStream.set_Capacity(Int32 value) at System.IO.MemoryStream.EnsureCapacity(Int32 value) at System.IO.MemoryStream.WriteByte(Byte value) at bH.c(String a) at UH.WEFSL9a(nG a) at WE.a(oG a) at kH.c() at WGa.b(rbb a, YU b) at WGa.pkaFSL9a(rbb a) at Nua.NuaFSL9a(rbb a) at Aspose.Words.Document.a(rbb a) at Aspose.Words.Document.a(Stream a, String b, SaveOptions c) at demoTask1.Controllers.ValuesController.GeneratePdfUsingAPIV3Code() in D:\Other Folder\Demos\generic name task\demoTask1\demoTask1\Controllers\ValuesController.cs:line 439}|System.Exception {System.NotSupportedException}|" at document.Save

            string htmlString = "<div><p>Aspose PDF</p><p>Test Content</p></div>";
            Aspose.Words.Document doc = new Aspose.Words.Document();
            Aspose.Words.DocumentBuilder builder = new Aspose.Words.DocumentBuilder(doc);
            builder.InsertHtml(htmlString);
            doc.Save("HTMLToPDFExample.pdf", Aspose.Words.SaveFormat.Pdf);

if i used doc.Save(“HTMLToPDFExample.pdf”, Aspose.Words.SaveFormat.Pdf); its work for save in pdf format but i need to save html in MemoryStream insted of filename

i used Windows 11 Home Single Language

please give some example for save HtmlSting into MemoryStream and convert into pdf

@ravina

As per our understandings, you need to convert an HTML String into MemoryStream, convert it into PDF and then save the obtained PDF into MemoryStream. In order to achieve it, you can use below code snippet that uses Aspose.PDF for .NET:

string contentHtml = File.ReadAllText(dataDir + @"Sample.html"); // OR it can be any HTML string
HtmlLoadOptions htmloptions = new HtmlLoadOptions();
htmloptions.PageInfo.IsLandscape = true;
Document doc = new Document(new MemoryStream(System.Text.Encoding.UTF8.GetBytes(contentHtml)), htmloptions);
using (MemoryStream outStream = new MemoryStream())
{
    doc.Save(outStream);
}

2 posts were merged into an existing topic: Need to save html in MemoryStream insted of filename

            string contentHtml = "<div><p>Aspose PDF</p><p>Test Content</p></div>";
            HtmlLoadOptions htmloptions = new HtmlLoadOptions();
            htmloptions.PageInfo.IsLandscape = true;
            Document doc = new Document(new MemoryStream(System.Text.Encoding.UTF8.GetBytes(contentHtml)), htmloptions);
            using (MemoryStream outStream = new MemoryStream())
            {
                doc.Save(outStream);
            }

when i used this code i Get error At Document doc = new Document(new MemoryStream(System.Text.Encoding.UTF8.GetBytes(contentHtml)), htmloptions);
System.NullReferenceException
HResult=0x80004003
Message=Object reference not set to an instance of an object.
Source=Aspose.PDF
StackTrace:
at #=zxmF_JRRcJjo8uwpoSD4W$otttG_5qHMxgwjkJbM=.#=zb9hS7wjjTZxf(#=zUBHwzz8ND6QoQtfFdaeFfTLHgjkeXGkdqg== #=zka02UJI=, Char #=zU9B8iBQ=)

@ravina

Please make sure that you install msttcorefonts and libgdiplus packages. In case issue still persists, please let us know. Also, we suggest you use Aspose.Pdf.Drawing instead of Aspose.PDF as it performs better in .NET 6.0 like environments.