In an ASP.NET MVC 4.8 C# app, I have an HTML string in memory that needs to be converted to a PDF byte array using Aspose’s library. All the processing occurs in-memory and nothing is saved to disk. The HTML is generated on the fly and PDF isn’t saved to disk but will be attached to an email and sent.
The Aspose docs explain how to load an HTML from disk and save a PDF to disk. I can’t find anything on how to do this all in-memory.
Thank you.
@asposeFriend9
Please try to use the below code snippet in order to achieve your requirements:
// Create a memory stream to store the PDF bytes.
using (MemoryStream outputStream = new MemoryStream())
{
// Initialize a new Document object.
Document pdfDocument = new Document();
// Create an HtmlFragment from the HTML string.
HtmlFragment htmlFragment = new HtmlFragment(htmlString);
// Add the HtmlFragment to the document's paragraph collection.
pdfDocument.Pages.Add().Paragraphs.Add(htmlFragment);
// Save the PDF to the memory stream.
pdfDocument.Save(outputStream);
// Close the document.
pdfDocument.Dispose();
// Return the PDF byte array.
return outputStream.ToArray();
}
1 Like