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


In today's world, document creation is a key task for developers, especially when it comes to automating the generation of reports, tickets, contracts, and other documents in popular formats such as PDF. One popular solution is converting HTML strings to PDF files—a process that allows you to easily format documents using any HTML technology and CSS for both aesthetics and functionality.

In this article, we'll take a detailed look at how to implement HTML string-to-PDF conversion in C# and .NET using the powerful PDF Metamorphosis .NET from SautinSoft library. We'll cover the advantages, potential, and practical applications of this approach.

Converting HTML to PDF is a standard task in reporting systems, electronic document management systems, SaaS solutions, and enterprise applications. Many large companies and developers value SautinSoft for its reliability and professionalism: it's used in government and commercial systems, financial and medical applications.

What is the value and purpose of this approach?

  • Automatic report generation: For example, converting HTML reports created on the server to PDF for further distribution.
  • Ticket or receipt generation: Using HTML templates for users.
  • Exporting web pages or presentations: For offline processing and storage.
  • Generating business documents: contracts, letters, and other documents in PDF format with precise formatting.

Why use this approach?

  • Flexibility: HTML allows for easy document formatting using custom tools and templates.
  • Automation: Code can be integrated into the system's backend, enabling mass PDF creation.
  • Scalability: Suitable for processing both small and very large data volumes.
  • Standardization: PDF is the preferred format for document exchange and long-term storage.

Interesting aspects and usage tips:

  • Error handling: The library provides the community with detailed error messages and diagnostics.
  • Appearance customization: Use CSS to style the HTML for a professional-looking PDF.
  • Font support: It's important that the library can embed fonts and respect styles.
  • Image processing: You can embed graphics and produce rich-looking documents.
  • Streaming method support: You can generate a PDF in memory instead of saving the file immediately.
  • Security options: Add passwords and encryption if needed.
  • Use caching and optimization to speed up bulk processing.

The basic idea is to take an HTML string, process it with the library, and save the result to a PDF file or stream. Below is a sample example with comments. This code is a basic example. In real-world scenarios, you can further customize the parameters, use streaming methods, set file metadata, and so on.

Input file:

convert html string to pdf file input

Output result:

convert html string to pdf file output

Complete code

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

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();

            // Specify some PDF options.
            p.PageSettings.Orientation = SautinSoft.PdfMetamorphosis.PageSetting.Orientations.Portrait;
            p.PageSettings.Size.Letter();

            // Specify header in HTML format.
            p.PageSettings.Header.FromString("<b>Sample header in HTML format</b>", SautinSoft.PdfMetamorphosis.HeadersFooters.InputFormat.Html);

            // Specify footer in RTF format.
            p.PageSettings.Footer.FromString(@"{\rtf1 \b Bold Footer}", SautinSoft.PdfMetamorphosis.HeadersFooters.InputFormat.Rtf);

            // Specify page numbers
            p.PageSettings.Numbering.Text = "Page {page} of {numpages}";
            p.PageSettings.Numbering.PosX.Mm = p.PageSettings.Size.Width.Mm / 2;
            p.PageSettings.Numbering.PosY.Mm = 10;

            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 from file				
                string htmlString = File.ReadAllText(inpFile);

                // 2. Converting HTML to PDF
                // Specify BaseUrl to help converter find a full path to relative images and external CSS.
                p.HtmlSettings.BaseUrl = Path.GetDirectoryName(inpFile);

                if (p.HtmlToPdfConvertStringToFile(htmlString, outFile) == 0)
                    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 Microsoft.VisualBasic
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()

            ' Specify some PDF options.
            p.PageSettings.Orientation = SautinSoft.PdfMetamorphosis.PageSetting.Orientations.Portrait
            p.PageSettings.Size.Letter()

            ' Specify header in HTML format.
            p.PageSettings.Header.FromString("<b>Sample header in HTML format</b>", SautinSoft.PdfMetamorphosis.HeadersFooters.InputFormat.Html)

            ' Specify footer in RTF format.
            p.PageSettings.Footer.FromString("{\rtf1 \b Bold footer}", SautinSoft.PdfMetamorphosis.HeadersFooters.InputFormat.Rtf)

            ' Specify page numbers
            p.PageSettings.Numbering.Text = "Page {page} of {numpages}"
            p.PageSettings.Numbering.PosX.Mm = p.PageSettings.Size.Width.Mm / 2
            p.PageSettings.Numbering.PosY.Mm = 10

            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 from file				
                Dim htmlString As String = File.ReadAllText(inpFile)

                ' 2. Converting HTML to PDF
                ' Specify BaseUrl to help converter find a full path to relative images and external CSS.
                p.HtmlSettings.BaseUrl = Path.GetDirectoryName(inpFile)

                If p.HtmlToPdfConvertStringToFile(htmlString, outFile) = 0 Then
                    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.