Convert HTML string to PDF bytes in C# and VB.Net

Convert HTML string to PDF bytes
in C# and VB.Net.


Complete code

using System;
using System.IO;

namespace Sample
{
    class Test
    {
        static void Main(string[] args)
        {
            // Convert HTML string to PDF bytes.
            SautinSoft.PdfVision v = new SautinSoft.PdfVision();

            //v.Serial = "XXXXXXXXXXXXXXX";

            // Set "Edge mode" to support all modern CSS.
            SautinSoft.PdfVision.TrySetBrowserModeEdgeInRegistry();

            // Specify some converting options.
            v.PageStyle.PageSize.Auto();
            //v.PageStyle.PageMarginLeft.Inch(1);
            //v.ImageStyle.Heightmm(150);
            //v.ImageStyle.WidthInch(10);

            // Specify top and bottom page margins
            v.PageStyle.PageMarginTop.Mm(5f);
            v.PageStyle.PageMarginBottom.Mm(5f);

            string inpHtml = "<html><body><p style=\"text-align: center; font-size: 45px;\">Hello my Friend!</p></body></html>";

            // Convert HTML string to PDF bytes.
            byte[] pdfBytes = v.ConvertHtmlStringToPDFStream(inpHtml);

            // Save PDF data to a file and open it for demonstration purposes.
            if (pdfBytes != null)
            {
                FileInfo outFile = new FileInfo(@"Result.pdf");

                using (FileStream fs = new FileStream(outFile.FullName, FileMode.Create, FileAccess.Write))
                {
                    fs.Write(pdfBytes, 0, pdfBytes.Length);
                }
				// Open the resulting PDF document in a default PDF Viewer.
                System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(outFile.FullName) { UseShellExecute = true });
            }
        }
    }
}

Download.

        
            Imports System
Imports System.IO

Namespace Sample
    Friend Class Test
        Shared Sub Main(ByVal args() As String)
            ' Convert HTML string to PDF bytes.
            Dim v As New SautinSoft.PdfVision()

            'v.Serial = "XXXXXXXXXXXXXXX"

            ' Set "Edge mode" to support all modern CSS.
            SautinSoft.PdfVision.TrySetBrowserModeEdgeInRegistry()

            ' Specify some converting options.
            v.PageStyle.PageSize.Auto()
            'v.PageStyle.PageMarginLeft.Inch(1);
            'v.ImageStyle.Heightmm(150);
            'v.ImageStyle.WidthInch(10);

            ' Specify top and bottom page margins
            v.PageStyle.PageMarginTop.Mm(5.0F)
            v.PageStyle.PageMarginBottom.Mm(5.0F)

            Dim inpHtml As String = "<html><body><p style=""text-align: center; font-size: 45px;"">Hello my Friend!</p></body></html>"

            ' Convert HTML string to PDF bytes.
            Dim pdfBytes() As Byte = v.ConvertHtmlStringToPDFStream(inpHtml)

            ' Save PDF data to a file and open it for demonstration purposes.
            If pdfBytes IsNot Nothing Then
                Dim outFile As New FileInfo("Result.pdf")

                Using fs As New FileStream(outFile.FullName, FileMode.Create, FileAccess.Write)
                    fs.Write(pdfBytes, 0, pdfBytes.Length)
                End Using
                ' Open the resulting PDF document in a default PDF Viewer.
                System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo(outFile.FullName) With {.UseShellExecute = True})
            End If
        End Sub
    End Class
End Namespace

Download.

If you are looking also for a .Net solution to Convert HTML to PDF documents, see our Document .Net.


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.