Html File(Base64string) to PDF conversion in .Net

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

@pthube

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.

Steps to Convert Base64 HTML to PDF

  1. Decode the Base64 String: First, you need to decode the Base64 string to get the HTML content.
  2. Create a Memory Stream: Use a MemoryStream to hold the PDF output.
  3. Load the HTML into a Document: Use the HtmlLoadOptions to load the HTML content.
  4. Save the Document as PDF: Finally, save the document to the MemoryStream.

Code Example

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
        }
    }
}

Explanation

  • Base64 Decoding: The 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: A MemoryStream is created to store the PDF output.
  • Document Creation: The Document class is used to load the HTML content. The HtmlLoadOptions can be customized if needed.
  • Saving the PDF: The 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.

Conclusion

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!