Convert HTML file to one-page PDF file in C# and VB.Net

Convert HTML file to one-page PDF file
in C# and VB.Net.


Complete code

using System;
using System.IO;

namespace Sample
{
    class Test
    {

        static void Main(string[] args)
        {
            // Console - Convert HTML file to one-page PDF file.
            SautinSoft.PdfVision v = new SautinSoft.PdfVision();

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

            v.PageStyle.PageOrientation.Portrait();

            string htmlPath = @"https://nationalzoo.si.edu";
            FileInfo pdfPath = new FileInfo("result.pdf");

            byte[] image = v.ConvertHtmlFileToImageStream(htmlPath, SautinSoft.PdfVision.eImageFormat.Png);
            int ret = -1;

            if (image != null)
            {
                v.PageStyle.PageSize.Auto();
                ret = v.ConvertImageStreamToPDFFile(image, pdfPath.FullName);
            }

            // 0 - converting successfully
            // 1 - can't open input file, check the input path
            // 2 - can't create output file, check the output path
            // 3 - converting failed
            if (ret == 0)
            {
                // Open produced PDF in default PDF Reader.
                System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(pdfPath.FullName) { UseShellExecute = true });
            }
        }
    }
}

Download.

        
            Imports System.IO

Namespace Sample
    Friend Class Test

        Shared Sub Main(ByVal args() As String)
            ' Console - Convert HTML file to one-page PDF file.
            Dim v As New SautinSoft.PdfVision()

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

            v.PageStyle.PageOrientation.Portrait()

            Dim htmlPath As String = "https://nationalzoo.si.edu"
            Dim pdfPath As New FileInfo("result.pdf")

            Dim image() As Byte = v.ConvertHtmlFileToImageStream(htmlPath, SautinSoft.PdfVision.eImageFormat.Png)
            Dim ret As Integer = -1

            If image IsNot Nothing Then
                v.PageStyle.PageSize.Auto()
                ret = v.ConvertImageStreamToPDFFile(image, pdfPath.FullName)
            End If

            ' 0 - converting successfully
            ' 1 - can't open input file, check the input path
            ' 2 - can't create output file, check the output path
            ' 3 - converting failed
            If ret = 0 Then
                ' Open produced PDF in default PDF Reader.
                System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo(pdfPath.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.