Click or drag to resize
Pdf Library for .NET

Convert Html Code To Pdf

Using Select.Pdf Html to Pdf Converter for .NET any html code can be converted to pdf. Select.Pdf Library supports the latest HTML5 with CSS3 style sheets.

To convert a raw html string to pdf, HtmlToPdf class exposes the ConvertHtmlString(String) and ConvertHtmlString(String, String) methods.

The first method, that has only the url parameter, can be used to convert html strings that don't have external references to javascript, css files or images. In case the html string contains references to external resources specified using relative paths, the second method needs to be used. ConvertHtmlString(String, String) contains an additional parameter: baseUrl. The baseUrl parameter allows the converter to resolve relative urls. Basically, baseUrl + relative image/css url = full absolute url.

Sample Code

This sample code shows how to use Select.Pdf html to pdf converter to convert an html string to pdf, also setting a few properties.

// instantiate a html to pdf converter object
HtmlToPdf converter = new HtmlToPdf();

// set converter options
converter.Options.PdfPageSize = pageSize;
converter.Options.PdfPageOrientation = pdfOrientation;
converter.Options.WebPageWidth = webPageWidth;
converter.Options.WebPageHeight = webPageHeight;

// create a new pdf document converting an html string
PdfDocument doc = converter.ConvertHtmlString(htmlString, baseUrl);

// save pdf document
doc.Save(Response, false, "Sample.pdf");

// close pdf document
doc.Close();
See Also