Converts HTML string to PDF file. Don't forget to specify the property BaseUrl to see images in output PDF file.
Namespace:
SautinSoftAssembly: PdfMetamorphosis (in PdfMetamorphosis.dll) Version: 5.3.4.18
Syntax
| C# | Visual Basic |
Parameters
- inputString
- String
Any string in HTML format, even piece of HTML code
- outputFileName
- String
Local path to output PDF file
Return Value
0 - converting successfully2 - can't create output file, check the output path
3 - converting failed
Examples
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 = @"..\..\..\..\..\test.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 file ReadFromFile(htmlPath,ref htmlString); //2. Converting HTML to PDF //specify BaseUrl to help converter find a full path for relative images, CSS p.HtmlOptions.BaseUrl = Path.GetDirectoryName(htmlPath); 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!"); } } } public static int ReadFromFile(string fileName,ref string fileStr) { try { FileInfo fi = new FileInfo(fileName); StreamReader strmRead = fi.OpenText(); fileStr = strmRead.ReadToEnd(); strmRead.Close(); return 0; } catch { //error open file System.Console.WriteLine("Error in open file"); return 1; } } } }
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 = "..\..\..\..\..\test.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 file ReadFromFile(htmlPath, htmlString) '2. Converting HTML to PDF 'specify BaseUrl to help converter find a full path for relative images, CSS p.HtmlOptions.BaseUrl = Path.GetDirectoryName(htmlPath) 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 Public Function ReadFromFile(ByVal fileName As String, ByRef fileStr As String) As Integer Try Dim fi As New FileInfo(fileName) Dim strmRead As StreamReader = fi.OpenText() fileStr = strmRead.ReadToEnd() strmRead.Close() Return 0 Catch 'error open file System.Console.WriteLine("Error in open file") Return 1 End Try End Function End Module