Convert HTML string to PDF bytes in C# and .NET


Modern applications often require data conversion from one format to another to improve visualization, storage, or exchange of information. One common scenario is converting HTML into a PDF document that is easy to view, store, and distribute. This is especially relevant for reporting systems, "on-the-fly" document generation, and web applications. In this article, we'll take a detailed look at how to convert an HTML string to a PDF byte array in C# and .NET applications using the powerful PDF Metamorphosis .NET from SautinSoft SDK.

Converting HTML to PDF is a very popular scenario in modern development, especially in reporting systems, electronic document management, and PDF generation. Many companies use these methods when creating online reports, automating document workflow, and integrating with third-party systems.

Working with byte arrays is a standard approach to creating, exchanging, and storing documents in systems where data exchange optimization is important.
Receiving a PDF as a byte array allows you to:

  • Transfer documents over the network: for example, via an API or storing them in a database.
  • Flexible document management in memory: eliminating the need to write to disk.
  • Integrate with other systems and modules: for example, sending via email or saving to storage.

The primary scenario involves using a conversion method that accepts an HTML string and returns a byte array of a PDF document. This is based on the library's internal mechanism, which parses the HTML, renders it, and creates a PDF that accurately replicates the appearance.
This solution is suitable for the following scenarios:

  • Generating reports and documents on the fly, from dynamically created HTML templates and database data.
  • Web applications, sending generated PDFs to clients without storing them on the server.
  • Archiving, converting HTML reports, emails, and pages to PDF for long-term storage.
  • Integration with other systems, transferring documents in byte format via an API.

Interesting aspects and recommendations:

  • Performance optimization. When processing large HTML documents, it is recommended to use asynchronous calls and manage memory.
  • Handling styles and external resources. If the HTML contains references to external styles or images, ensure that the library handles them correctly or embed the styles inline.
  • Customizing appearance. The library allows you to set parameters for document decoration, which is important for full control over the resulting files.
  • Error handling. It is recommended to surround conversion calls with exception handling and result validation.

Furthermore, having a library that operates quickly and reliably allows for expanding application capabilities and improving the user experience.

Input file:

convert html string to pdf bytes input

Output result:

convert html string to pdf bytes output

Complete code

using System;
using System.IO;
using System.Collections;
using System.Net;

namespace Sample
{
    class Test
    {

        static void Main(string[] args)
		{
			// Before starting, we recommend to get a free key:
            // https://sautinsoft.com/start-for-free/
            
            // Apply the key here:
			// SautinSoft.PdfMetamorphosis.SetLicense("...");

            SautinSoft.PdfMetamorphosis p = new SautinSoft.PdfMetamorphosis();

            if (p != null)
            {
                string inpFile = @"..\..\..\example.htm";
                string outFile = Path.ChangeExtension(inpFile, ".pdf");                

                // The easiest way is using the method 'HtmlToPdfConvertFile':
                // int ret = p.HtmlToPdfConvertFile(htmlPath,pdfPath);
                // or :
                // 1. Get HTML content.
                string htmlString = File.ReadAllText(inpFile);

                // 2. Converting HTML to PDF
                // Specify BaseUrl to help converter find a full path for relative images, CSS.
                p.HtmlSettings.BaseUrl = Path.GetDirectoryName(Path.GetFullPath(inpFile));
                byte[] pdfBytes = p.HtmlToPdfConvertStringToByte(htmlString);

                if (pdfBytes != null)
                {
                    // 3. Save the PDF document to a file for a viewing purpose.
                    File.WriteAllBytes(outFile, pdfBytes);
					System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(outFile) { UseShellExecute = true });
                }
                else
                {               
                    System.Console.WriteLine("An error occurred during converting HTML to PDF!");
                }
            }
        }        
    }
}

Download

Imports System.IO

Namespace Sample
    Friend Class Test

        Shared Sub Main(ByVal args() As String)
			' Before starting, we recommend to get a free key:
            ' https://sautinsoft.com/start-for-free/
            
            ' Apply the key here:
			' SautinSoft.PdfMetamorphosis.SetLicense("...");


            Dim p As New SautinSoft.PdfMetamorphosis()

            If p IsNot Nothing Then
                Dim inpFile As String = "..\..\..\example.htm"
                Dim outFile As String = Path.ChangeExtension(inpFile, ".pdf")

                ' The easiest way is using the method 'HtmlToPdfConvertFile':
                ' int ret = p.HtmlToPdfConvertFile(htmlPath,pdfPath)
                ' or :
                ' 1. Get HTML content.
                Dim htmlString As String = File.ReadAllText(inpFile)

                ' 2. Converting HTML to PDF
                ' Specify BaseUrl to help converter find a full path for relative images, CSS.
                p.HtmlSettings.BaseUrl = Path.GetDirectoryName(Path.GetFullPath(inpFile))
                Dim pdfBytes() As Byte = p.HtmlToPdfConvertStringToByte(htmlString)

                If pdfBytes IsNot Nothing Then

                    ' 3. Save the PDF document to a file for a viewing purpose.
                    File.WriteAllBytes(outFile, pdfBytes)
                    System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo(outFile) With {.UseShellExecute = True})
                Else
                    System.Console.WriteLine("An error occurred during converting HTML to PDF!")
                End If
            End If
        End Sub
    End Class
End Namespace

Download


If you need a new code example or have a question: email us at support@sautinsoft.com or ask at Online Chat (right-bottom corner of this page) or use the Form below:


Captcha

Questions and suggestions from you are always welcome!

We are developing .Net components since 2002. We know PDF, DOCX, RTF, HTML, XLSX and Images formats. If you need any assistance with creating, modifying or converting documents in various formats, we can help you. We will write any code example for you absolutely free.