Converts HTML file or URL into array of PDF bytes.

Namespace:  SautinSoft
Assembly:  PdfMetamorphosis (in PdfMetamorphosis.dll) Version: 5.3.4.18

Syntax

      
 C#  Visual Basic 
public byte[] HtmlToPdfConvertFileToByte(
	string inputFileName
)
Public Function HtmlToPdfConvertFileToByte ( _
	inputFileName As String _
) As Byte()

Parameters

inputFileName
String
Path to local HTML file or URL

Return Value

PDF document as byte array, or null in case of converting failed

Examples

CopyHow to convert HTML string to PDF bytes using C# in memory
using System;
using System.IO;
using System.Collections;

namespace Sample
{
    class Test
    {

        static void Main(string[] args)
        {
            SautinSoft.PdfMetamorphosis p = new SautinSoft.PdfMetamorphosis();

            //this property is necessary only for registered version
            //p.Serial = "XXXXXXXXXXX";

            //specify some options
            p.PageStyle.PageOrientation.Landscape();
            //specify header in HTML format
            p.Header.Html("<b>Sample header in HTML format</b>");

            //specify footer in RTF format
            p.Footer.Rtf("{\b Bold footer}");

            //specify page numbers
            p.PageStyle.PageNumFormat = "Page {page} of {numpages}";

            if (p != null)
            {
                string htmlPath = @"http://www.sautinsoft.com/help/html-to-rtf/net/help/htmlsamples/pic.htm";
                string pdfPath = @"..\..\..\..\..\test.pdf";
                string htmlString = "";

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


                //2. Converting HTML to PDF
                //specify BaseUrl to help converter find a full path for relative images, CSS
                p.HtmlOptions.BaseUrl = @"http://www.sautinsoft.com/help/html-to-rtf/net/help/htmlsamples/";
                byte[] pdf = p.HtmlToPdfConvertStringToByte(htmlString);

                if (pdf != null)
                {
                    //3. Save to PDF file
                    FileStream fs = new FileStream(pdfPath, FileMode.Create, FileAccess.Write);
                    fs.Write(pdf, 0, pdf.Length);
                    fs.Close();
                    System.Diagnostics.Process.Start(pdfPath);
                }
                else
                {
                    System.Console.WriteLine("An error occured during converting HTML to PDF!");
                }
            }
        }
        static private string DownloadFile(string url)
        {
            string contents = "";
            System.IO.Stream StreamHttp = null;
            System.Net.WebResponse resp = null;
            System.Net.HttpWebRequest webrequest = null;


            try
            {
                webrequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
                webrequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; .NET CLR 1.1.4322)";
                resp = webrequest.GetResponse();
                //Get Total Size
                int dataLength = (int)resp.ContentLength;
                StreamHttp = resp.GetResponseStream();

                BinaryReader br = new BinaryReader(StreamHttp);
                System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();

                byte[] ch = new byte[dataLength];
                br.Read(ch, 0, dataLength);
                contents = encoding.GetString(ch);

                resp.Close();
                br.Close();
            }
            catch
            {

            }

            return contents;
        }
    }
}
CopyHow to convert HTML string PDF bytes using VB.Net in memory
Imports System
Imports System.IO
Imports System.Text

Module sample

    Sub Main()
        Dim p As New SautinSoft.PdfMetamorphosis

        'this property is necessary only for registered version
        'p.Serial = "XXXXXXXXXXX";


        'specify some options
        p.PageStyle.PageOrientation.Landscape()
        'specify header in HTML format
        p.Header.Html("<b>Sample header in HTML format</b>")

        'specify footer in RTF format
        p.Footer.Rtf("{\b Bold footer}")

        'specify page numbers
        p.PageStyle.PageNumFormat = "Page {page} of {numpages}"

        If Not p Is Nothing Then
            Dim htmlPath As String = "http://www.sautinsoft.com/help/html-to-rtf/net/help/htmlsamples/pic.htm"
            Dim pdfPath As String = "..\..\..\..\..\test.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 url
            htmlString = DownloadFile(htmlPath)


            '2. Converting HTML to PDF
            'specify BaseUrl to help converter find a full path for relative images, CSS
            p.HtmlOptions.BaseUrl = "http://www.sautinsoft.com/help/html-to-rtf/net/help/htmlsamples/"
            Dim pdf() As Byte = p.HtmlToPdfConvertStringToByte(htmlString)

            If Not pdf Is Nothing Then
                '3. Save to PDF file
                Dim fs As New FileStream(pdfPath, FileMode.Create, FileAccess.Write)
                fs.Write(pdf, 0, pdf.Length)
                fs.Close()
                System.Diagnostics.Process.Start(pdfPath)
            Else
                System.Console.WriteLine("An error occured during converting HTML to PDF!")
            End If
        End If
    End Sub
    Private Function DownloadFile(ByVal url As String) As String
        Dim contents As String = ""
        Dim StreamHttp As System.IO.Stream = Nothing
        Dim resp As System.Net.WebResponse = Nothing
        Dim webrequest As System.Net.HttpWebRequest = Nothing


        Try
            webrequest = CType(System.Net.WebRequest.Create(url), System.Net.HttpWebRequest)
            webrequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; .NET CLR 1.1.4322)"
            resp = webrequest.GetResponse()
            'Get Total Size
            Dim dataLength As Integer = CInt((resp.ContentLength))
            StreamHttp = resp.GetResponseStream()

            Dim br As New BinaryReader(StreamHttp)
            Dim encoding As New System.Text.ASCIIEncoding

            Dim ch(dataLength - 1) As Byte
            br.Read(ch, 0, dataLength)
            contents = encoding.GetString(ch)

            resp.Close()
            br.Close()
        Catch

        End Try

        Return contents
    End Function
End Module

See Also