ExpertPdf Html To Pdf Converter – API

The converter API is fully documented in the Doc/HtmlToPdf.chm that can be found in the product archive that can be downloaded from www.html-to-pdf.net. In order to use the converter library you have include the ExpertPdf.HtmlToPdf namespace in your application. The main classes in this namespace is the PdfConverter class and the ImgConverter class which expose the methods you can use to render a PDF document or an image from a URL or a HTML string. Below is a brief description of the main classes and properties of the converter.

PdfConverter Class

This class defines a set of methods to render a PDF document from a URL or from a HTML string. The conversion result can be a stream of bytes as byte[] object or a file on the disk. The PDF bytes can be further saved in a disk file or can be send a HTTP response to the client browser.

PdfConverter Save/Render Methods

The method below converts the URL specified as parameter to a PDF document returned as bytes array. The bytes array can be sent to a web browser from an ASP.NET application, saved in a file on disk or into a database or sent by email as an attachment.

public byte[] GetPdfFromUrlBytes ( string url )

To convert a HTML string to PDF you can use one of the following methods below. The first method simply renders the HTML string as a PDF document. The second one accepts an additional parameter urlBase which is the full URL of the page from where you have retrieved the HTML string. The urlBase parameter is a hint for the converter which is used to determine the full URL of the images and other external files like CSS and JavaScript referenced in the HTML string by a relative URL. If you don’t set this parameter the images referenced by relative URLS won’t appear in the document and the styles from external CSS files won’t be applied to the rendered document.

public byte[] GetPdfBytesFromHtmlString (string htmlString)
public byte[] GetPdfBytesFromHtmlString (string htmlString,string urlBase)

The correspondent methods you can use to render the PDF document in disk file are listed below. These methods internally use the methods above to get the bytes array and then they simply save the bytes in the specified file on disk.

public void SavePdfFromUrlToFile (string url, string outFile)

public void SavePdfFromHtmlStringToFile (string htmlString, string outFile)
public void SavePdfFromHtmlStringToFile (string htmlString, string outFile, string urlBase)

The methods above first obtain the PDF document as a byte array and then simply return that array of bytes or save it in a file. There is a set of methods which can save the PDF document directly into a stream without storing it first in the memory. The name of these methods have the ToStream suffix instead of the ToFile suffix from the set of Save methods prototyped above:

public void SavePdfFromUrlToStream(string url, Stream outPdfStream)
public void SavePdfFromUrlToStream(string url, string internalLinksDocUrl, Stream outPdfStream)
public void SavePdfFromHtmlFileToStream(string htmlFilePath, Stream outPdfStream)
public void SavePdfFromHtmlFileToStream(string htmlFilePath, string internalLinksDocUrl, Stream outPdfStream)
public void SavePdfFromHtmlStringToStream(string htmlString, Stream outPdfStream)
public void SavePdfFromHtmlStringToStream(string htmlString, string urlBase, Stream outPdfStream)
public void SavePdfFromHtmlStringToStream(string htmlString, string urlBase, string internalLinksDocUrl, Stream outPdfStream)
public void SavePdfFromHtmlStreamToStream(System.IO.Stream htmlStream, Encoding streamEncoding, Stream outPdfStream)
public void SavePdfFromHtmlStreamToStream(System.IO.Stream htmlStream, Encoding streamEncoding, string urlBase, Stream outPdfStream)
public void SavePdfFromHtmlStreamToStream(System.IO.Stream htmlStream, Encoding streamEncoding, string urlBase, string internalLinksDocUrl, Stream outPdfStream)

There is also a set of methods returning a Document object that can be used to further modify the generated PDF document as described in the Post Convert Customization of the Generated PDF Document section by adding new PDF pages and new PDF elements to the document. The Document class and the elements that can be added to a Document object are defined in the ExpertPdf.HtmlToPdf.PdfDocument namespace.

public Document GetPdfDocumentObjectFromUrl(string url)
public Document GetPdfDocumentObjectFromUrl(string url, string internalLinksDocUrl)
public Document GetPdfDocumentObjectFromHtmlFile(string htmlFilePath)
public Document GetPdfDocumentObjectFromHtmlFile(string htmlFilePath, string internalLinksDocUrl)
public Document GetPdfDocumentObjectFromHtmlString(string htmlString)
public Document GetPdfDocumentObjectFromHtmlString(string htmlString, string urlBase)
public Document GetPdfDocumentObjectFromHtmlString(string htmlString, string urlBase,
string internalLinksDocUrl)
public Document GetPdfDocumentObjectFromHtmlStream(System.IO.Stream htmlStream, Encoding streamEncoding)
public Document GetPdfDocumentObjectFromHtmlStream(System.IO.Stream htmlStream, Encoding streamEncoding,
string urlBase)
public Document GetPdfDocumentObjectFromHtmlStream(System.IO.Stream htmlStream, Encoding streamEncoding,
string urlBase, string internalLinksDocUrl)

The generated Document object can be modified and then saved into memory, file or stream using one of the Save methods of the Document class.

PdfConverter Configuration Properties

The conversion process and the aspect of the generated PDF document can be configured in many ways. You can set the PDF document page size (A4, A3, etc), orientation (Portrait or Landscape), compression level, encryption and passwords, document info (author, title, subject, etc), add headers and footers with page numbering, etc. The main properties of the converter are listed below.

To set the license key you received after purchase and unlock the product you can use the LicenseKey property. If this property is not set with any value the converter will enter in demo mode.

public string LicenseKey { get; set; }

The PageWidth and PageHeight properties allows you to set the width and height of the virtual browser windows. The web page content is rendered based on the virtual browser width specified as a integer value in pixels. Setting these properties has the same effect as the effect produced when resizing a web page in a browser window to the specified dimensions.

The default value of the PageWidth property is 1024 pixels. The default value of the PageHeight property is 0 pixels which means the height will be automatically determined. These values are producing good results in most of the cases but there are also some situations when you’ll have to change these properties. You can also choose to let the converter autodetermine both the width and height of the virtual browser by setting both PageWidth and Page Height properties to 0.

public int PageWidth { get; set; }
public int PageHeight { get; set; }

The PdfDocumentOptions property allows you to change the aspect and properties of the rendered PDF document like setting the margins, add header and footer, embed true type fonts, generate a document with selectable texts and images or a document with an embedded image, enable or disable live links, pdf page size and page orientation, compression level, show or hide the headers and footers.

This property exposes an object of PdfDocumentOptions type which is automatically created in the PdfConverter constructor. Therefore you don’t have to set this property directly with a value from your code but you’ll have to set the properties of the exposed PdfDocumentOptions object.

The main properties of the PdfDocumentOptions class are described in a later section.

public PdfDocumentOptions PdfDocumentOptions { get; }

The PdfSecurityOptions class property allows you to change the permissions of the rendered PDF document like allow or disallow printing, etiding, etc and also to set user and owner passwords.

This property exposes an object of PdfSecurityOptions type which is automatically created in the PdfConverter constructor. Therefore you don’t have to set this property directly with a value from your code but you’ll have to set the properties of the exposed PdfDocumentOptions object.

The main properties of the PdfDocumentOptions class are described in a later section.

public PdfSecurityOptions PdfSecurityOptions { get; }

The PdfDocumentInfo property allows you to set the rendered PDF description like title, author, subject, keywords, etc.

This property exposes an object of PdfSecurityOptions type which is automatically created in the PdfConverter constructor. Therefore you don’t have to set this property directly with a value from your code but you’ll have to set the properties of the exposed PdfDocumentOptions object.

The main properties of the PdfDocumentOptions class are described in a later section.

public PdfDocumentInfo PdfDocumentInfo { get; }

The PdfHeaderOptions and PdfFooterOptions properties allows you to customize the aspect of the headers and footers added to the rendered PDF document. Note that the header and footer are visible in the resulted PDF document only if the corresponding ShowHeader and ShowFooter properties from the PdfDocumentOptions property are true.

These properties expose objects of PdfHeaderOptions type and PdfFooterOptions type which are automatically created in the PdfConverter constructor. Therefore you don’t have to set this property directly with a value from your code but you’ll have to set the properties of the exposed PdfHeaderOptions and PdfFooterOptions object.

The main properties of the PdfHeadersOptions and PdfFooterOptions classes are described in a later section.

public PdfHeaderOptions PdfHeaderOptions { get; }
public PdfFooterOptions PdfFooterOptions { get; }

For more details about ExpertPdf visit www.html-to-pdf.net.