Converts HTML string into array of PDF bytes. Don't forget to specify the property BaseUrl to see images in output PDF file.
Namespace:
SautinSoftAssembly: PdfMetamorphosis (in PdfMetamorphosis.dll) Version: 4.0.0.1
Syntax
| C# | Visual Basic |
Parameters
- inputString
- String
Any string in HTML format, even piece of HTML code
Return Value
PDF document as byte array, or null in case of converting failed
Examples
using System; using System.IO; using System.Text; namespace SampleConvert { class Class1 { 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; } } }
Imports Microsoft.VisualBasic Imports System Imports System.IO Imports System.Text Namespace SampleConvert Friend Class Class1 Shared Sub Main(ByVal args() As String) 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("{" & Constants.vbBack & " 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 Shared 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(Fix(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 Class End Namespace