Hello,
We have a base64String of HTML file, and we want to convert it to PDF and return it as Stream
can you please suggest the best way to do this conversion? or if you have any code snippets for this then please provide.
Thanks
Hello,
We have a base64String of HTML file, and we want to convert it to PDF and return it as Stream
can you please suggest the best way to do this conversion? or if you have any code snippets for this then please provide.
Thanks
To convert a Base64 string of an HTML file to a PDF and return it as a Stream in .NET, you can use the Aspose.PDF library. Below is a step-by-step guide along with a code snippet to help you achieve this.
MemoryStream
to hold the PDF output.HtmlLoadOptions
to load the HTML content.MemoryStream
.Here’s a sample code snippet demonstrating the conversion:
using Aspose.Pdf;
using Aspose.Pdf.IO;
using Aspose.Pdf.Text;
using System;
using System.IO;
public class HtmlToPdfConverter
{
public Stream ConvertBase64HtmlToPdf(string base64Html)
{
// Decode the Base64 string
byte[] htmlBytes = Convert.FromBase64String(base64Html);
string htmlContent = System.Text.Encoding.UTF8.GetString(htmlBytes);
// Create a MemoryStream to hold the PDF
using (MemoryStream pdfStream = new MemoryStream())
{
// Load the HTML content into a Document
using (var document = new Document(new MemoryStream(System.Text.Encoding.UTF8.GetBytes(htmlContent)), new HtmlLoadOptions()))
{
// Save the document as PDF to the MemoryStream
document.Save(pdfStream);
}
// Reset the position of the stream to the beginning
pdfStream.Position = 0;
return pdfStream; // Return the PDF stream
}
}
}
Convert.FromBase64String
method is used to decode the Base64 string into a byte array, which is then converted to a string representing the HTML content.MemoryStream
is created to store the PDF output.Document
class is used to load the HTML content. The HtmlLoadOptions
can be customized if needed.Save
method is called on the document to write the PDF data into the MemoryStream
.This approach allows you to convert HTML content encoded in Base64 format into a PDF document efficiently.
Using Aspose.PDF for .NET, you can easily convert HTML to PDF, and the above code snippet provides a straightforward way to handle Base64 encoded HTML. You can integrate this functionality into your application as needed.
If you have any further questions or need additional assistance, feel free to ask!