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


Complete code

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

namespace Sample
{
	class Test
	{
		
		static void Main(string[] args)
		{	
		// Activate your license here
		// SautinSoft.PdfMetamorphosis.SetLicense("1234567890");

			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 htmlPath = @"..\..\..\example.htm";
                string pdfPath = Path.ChangeExtension(htmlPath, ".pdf");
				string htmlString = "";

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

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

				if (p.HtmlToPdfConvertStringToFile(htmlString, pdfPath)==0)
					System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(pdfPath) { UseShellExecute = true });
				else
					System.Console.WriteLine("An error occurred during converting HTML to PDF!");
			}
		}

        public static string ReadFromFile(string fileName)
        {
            try
            {
                FileInfo fi = new FileInfo(fileName);
                FileStream strmRead = fi.Open(FileMode.Open);
                int len = (int)fi.Length;
                byte[] b = new byte[len];
                strmRead.Read(b, 0, len);
                strmRead.Close();
                char[] arCharRes = new char[len];
                for (int i = 0; i < len; i++)
                {
                    arCharRes[i] = (char)b[i];
                }
                return new string(arCharRes);
            }
            catch
            {
                return "";
            }
        }
	}
}

Download

Imports Microsoft.VisualBasic
Imports System.IO

Namespace Sample
    Friend Class Test

        Shared Sub Main(ByVal args() As String)
				' Activate your license here
				' SautinSoft.PdfMetamorphosis.SetLicense("1234567890")

            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 htmlPath As String = "..\..\..\example.htm"
                Dim pdfPath As String = Path.ChangeExtension(htmlPath, ".pdf")
                Dim htmlString As String = ""

                ' The easiest way is using the method 'HtmlToPdfConvertFile':
                ' int ret = p.HtmlToPdfConvertFile(htmlPath,pdfPath)
                ' or :
                ' 1. Get HTML content from file				
                htmlString = ReadFromFile(htmlPath)

                ' 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(htmlPath)

                If p.HtmlToPdfConvertStringToFile(htmlString, pdfPath) = 0 Then
                    System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo(pdfPath) With {.UseShellExecute = True})
                Else
                    System.Console.WriteLine("An error occurred during converting HTML to PDF!")
                End If
            End If
        End Sub
        Public Shared Function ReadFromFile(ByVal fileName As String) As String
            Try
                Dim fi As New FileInfo(fileName)
                Dim strmRead As FileStream = fi.Open(FileMode.Open)
                Dim len As Integer = CInt(fi.Length)
                Dim b(len - 1) As Byte
                strmRead.Read(b, 0, len)
                strmRead.Close()
                Dim arCharRes(len - 1) As Char
                For i As Integer = 0 To len - 1
                    arCharRes(i) = ChrW(b(i))
                Next i
                Return New String(arCharRes)
            Catch
                Return ""
            End Try
        End Function
    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:



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.