Show / Hide Table of Contents

# Class HTML2PDF

'pdftron.PDF.HTML2PDF' is an optional PDFNet Add-On utility class that can be used to convert HTML web pages into PDF documents by using an external module (html2pdf).

The html2pdf modules can be downloaded from http: www.pdftron.com/pdfnet/downloads.html.

Users can convert HTML pages to PDF using the following operations: - Simple one line static method to convert a single web page to PDF. - Convert HTML pages from URL or string, plus optional table of contents, in user defined order. - Optionally configure settings for proxy, images, java script, and more for each HTML page. - Optionally configure the PDF output, including page size, margins, orientation, and more. - Optionally add table of contents, including setting the depth and appearance.

The following code converts a single webpage to pdf

    using System;
    using System.IO;
    using pdftron;
    using pdftron.Common;
    using pdftron.SDF;
    using pdftron.PDF;

    using (PDFDoc doc = new PDFDoc())
    {
    	if ( HTML2PDF.Convert(doc, "http://www.gutenberg.org/wiki/Main_Page") )
    		doc.Save(outputFile, SDFDoc.SaveOptions.e_linearized);
    }

The following code demonstrates how to convert multiple web pages into one pdf, including any images and the background, but with lowered image quality to save space.

    using System;
    using System.IO;
    using pdftron;
    using pdftron.Common;
    using pdftron.SDF;
    using pdftron.PDF;

    using (PDFDoc doc = new PDFDoc())
    {
    	HTML2PDF converter = new HTML2PDF();
    	converter.SetImageQuality(25);

    	HTML2PDF.WebPageSettings settings = new HTML2PDF.WebPageSettings();
    	settings.SetPrintBackground(false);

    	converter.InsertFromURL("http://www.gutenberg.org/wiki/Main_Page", settings);

    	if ( HTML2PDF.Convert(doc, "http://en.wikipedia.org/wiki/Canada") )
    		doc.Save(outputFile, SDFDoc.SaveOptions.e_linearized);
    }

##### Inheritance

object
HTML2PDF

##### Implements

IDisposable

##### Inherited Members

object.Equals(object)
object.Equals(object, object)
object.GetHashCode()
object.GetType()
object.MemberwiseClone()
object.ReferenceEquals(object, object)
object.ToString()

###### Namespace: pdftron.PDF

###### Assembly: PDFTronDotNet.dll

##### Syntax

    public class HTML2PDF : IDisposable

### Constructors

#### HTML2PDF()

Default constructor.

##### Declaration

    public HTML2PDF()

### Methods

#### AddCookie(string, string)

Add cookie to the HTTP Headers when converting via a URL.

##### Declaration

    public void AddCookie(string name, string value)

##### Parameters

Type | Name | Description
--- | --- | ---
string | name | the name of the cookie
string | value | the value of the cookie

#### Convert(PDFDoc)

Convert HTML documents and append the results to doc.

##### Declaration

    public bool Convert(PDFDoc doc)

##### Parameters

Type | Name | Description
--- | --- | ---
PDFDoc | doc | Target PDF to which converted HTML pages will be appended to.

##### Returns

Type | Description
--- | ---
bool | true if successful, otherwise false. Use GetHttpErrorCode for possible HTTP errors.

##### Remarks

html2pdf module must be located in the working directory, or with the PDFNet library.

#### Convert(PDFDoc, string)

Convert the HTML document at url and append the results to doc.

##### Declaration

    public static bool Convert(PDFDoc doc, string url)

##### Parameters

Type | Name | Description
--- | --- | ---
PDFDoc | doc | Target PDF to which converted HTML pages will be appended to.
string | url | HTML page, or relative path to local HTML page, that will be converted to PDF format.

##### Returns

Type | Description
--- | ---
bool | true if successful, otherwise false. Use GetHttpErrorCode for possible HTTP errors.

##### Remarks

html2pdf module must be located in the working directory, or with the PDFNet library.

#### Convert(PDFDoc, string, WebPageSettings)

Convert the HTML document at url and append the results to doc.

##### Declaration

    public static bool Convert(PDFDoc doc, string url, HTML2PDF.WebPageSettings settings)

##### Parameters

Type | Name | Description
--- | --- | ---
PDFDoc | doc | Target PDF to which converted HTML pages will be appended to.
string | url | HTML page, or relative path to local HTML page, that will be converted to PDF format.
HTML2PDF.WebPageSettings | settings | Modify how the web page is loaded and converted.

##### Returns

Type | Description
--- | ---
bool | true if successful, otherwise false. Use GetHttpErrorCode for possible HTTP errors.

##### Remarks

html2pdf module must be located in the working directory, or with the PDFNet library.

#### Destroy()

##### Declaration

    public void Destroy()

#### Dispose()

Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.

##### Declaration

    public void Dispose()

#### Dispose(bool)

##### Declaration

    protected virtual void Dispose(bool disposing)

##### Parameters

Type | Name | Description
--- | --- | ---
bool | disposing |

#### DumpOutline(string)

Save outline to a xml file.

##### Declaration

    public void DumpOutline(string xml_file)

##### Parameters

Type | Name | Description
--- | --- | ---
string | xml_file | Path of where xml data representing outline of produced PDF should be saved to.

##### Remarks

This option is deprecated in the latest HTML2PDF module and may have no effect.

#### ~HTML2PDF()

Releases all resources used by the HTML2PDF

##### Declaration

    protected ~HTML2PDF()

#### GetHTTPErrorCode()

Return the largest HTTP error code encountered during conversion

##### Declaration

    public int GetHTTPErrorCode()

##### Returns

Type | Description
--- | ---
int | the largest HTTP code greater then or equal to 300 encountered during loading of any of the supplied objects, if no such error code is found 0 is returned.

##### Remarks

This function will only return a useful result after Convert has been called.

#### GetLog()

Get results of conversion, including errors and warnings, in human readable form.

##### Declaration

    public string GetLog()

##### Returns

Type | Description
--- | ---
string | String containing results of conversion.

#### InsertFromHtmlString(string)

Convert HTML encoded in string.

##### Declaration

    public void InsertFromHtmlString(string html)

##### Parameters

Type | Name | Description
--- | --- | ---
string | html | String containing HTML code.

#### InsertFromHtmlString(string, WebPageSettings)

Convert HTML encoded in string.

##### Declaration

    public void InsertFromHtmlString(string html, HTML2PDF.WebPageSettings settings)

##### Parameters

Type | Name | Description
--- | --- | ---
string | html | String containing HTML code.
HTML2PDF.WebPageSettings | settings | How the HTML content described in html is loaded.

#### InsertFromURL(string)

Add a web page to be converted. A single URL typically results in many PDF pages.

HTML page, or relative path to local HTML page

##### Declaration

    public void InsertFromURL(string url)

##### Parameters

Type | Name | Description
--- | --- | ---
string | url |

#### InsertFromURL(string, WebPageSettings)

Add a web page to be converted. A single URL typically results in many PDF pages.

##### Declaration

    public void InsertFromURL(string url, HTML2PDF.WebPageSettings settings)

##### Parameters

Type | Name | Description
--- | --- | ---
string | url | HTML page, or relative path to local HTML page.
HTML2PDF.WebPageSettings | settings | How the web page should be loaded and converted

#### InsertTOC()

Add a table of contents to the produced PDF.

##### Declaration

    public void InsertTOC()

##### Remarks

This option is deprecated in the latest HTML2PDF module and may have no effect.

#### InsertTOC(TOCSettings)

Add a table of contents to the produced PDF.

##### Declaration

    public void InsertTOC(HTML2PDF.TOCSettings settings)

##### Parameters

Type | Name | Description
--- | --- | ---
HTML2PDF.TOCSettings | settings | Settings for the table of contents.

##### Remarks

This option is deprecated in the latest HTML2PDF module and may have no effect.

#### IsModuleAvailable()

Find out whether the HTML2PDF module is available (and licensed).

##### Declaration

    public static bool IsModuleAvailable()

##### Returns

Type | Description
--- | ---
bool | returns true if HTML2PDF operations can be performed

#### SetCompatibilityMode(bool)

Provides the ability to run HTML to PDF conversion to run in compatibility mode, which runs with altered graphics options and does not create a dedicated render process. This option may be somewhat slower than the default mode. However, it may be required on environments with limited platform dependencies, such as AWS Lambda.

##### Declaration

    public void SetCompatibilityMode(bool compatibility)

##### Parameters

Type | Name | Description
--- | --- | ---
bool | compatibility | If true, compatibility mode is enabled.

#### SetCookieJar(string)

Path of file used for loading and storing cookies.

##### Declaration

    public void SetCookieJar(string path)

##### Parameters

Type | Name | Description
--- | --- | ---
string | path | Path to file used for loading and storing cookies.

##### Remarks

This option is deprecated in the latest HTML2PDF module and may have no effect.

#### SetCustomHeader(string, string)

Add a custom HTTP header specified by name and value.

##### Declaration

    public void SetCustomHeader(string name, string value)

##### Parameters

Type | Name | Description
--- | --- | ---
string | name | the name of the custom header
string | value | the value of the custom header

#### SetDPI(int)

Change the DPI explicitly for the output PDF.

##### Declaration

    public void SetDPI(int dpi)

##### Parameters

Type | Name | Description
--- | --- | ---
int | dpi | Dots per inch, e.g. 80. This has no effect on X11 based systems. Results also depend on SetSmartShrinking.

#### SetFooter(string)

Set footer of generated PDF.

##### Declaration

    public void SetFooter(string footer)

##### Parameters

Type | Name | Description
--- | --- | ---
string | footer | HTML string to be used as the footer

#### SetHeader(string)

Set header of generated PDF.

##### Declaration

    public void SetHeader(string header)

##### Parameters

Type | Name | Description
--- | --- | ---
string | header | HTML string to be used as the header

#### SetImageDPI(int)

Maximum DPI to use for images in the generated PDF.

##### Declaration

    public void SetImageDPI(int dpi)

##### Parameters

Type | Name | Description
--- | --- | ---
int | dpi | Maximum dpi of images in produced PDF, e.g. 80.

##### Remarks

This option is deprecated in the latest HTML2PDF module and may have no effect.

#### SetImageQuality(int)

JPEG compression factor to use when generating PDF.

##### Declaration

    public void SetImageQuality(int quality)

##### Parameters

Type | Name | Description
--- | --- | ---
int | quality | Compression factor, e.g. 92.

##### Remarks

This option is deprecated in the latest HTML2PDF module and may have no effect.

#### SetLandscape(bool)

Set page orientation for output PDF.

##### Declaration

    public void SetLandscape(bool enable)

##### Parameters

Type | Name | Description
--- | --- | ---
bool | enable | If true generated PDF pages will be orientated to landscape, otherwise orientation will be portrait.

#### SetLogFilePath(string)

Sets the location of the log file to be used during conversion.

##### Declaration

    public void SetLogFilePath(string path)

##### Parameters

Type | Name | Description
--- | --- | ---
string | path | Full path and file name of file to log to.

#### SetMargins(string, string, string, string)

Set margins of generated PDF.

##### Declaration

    public void SetMargins(string top, string bottom, string left, string right)

##### Parameters

Type | Name | Description
--- | --- | ---
string | top | Size of the top margin, e.g. "2cm".
string | bottom | Size of the bottom margin, e.g. "2cm".
string | left | Size of the left margin, e.g. "2cm".
string | right | Size of the right margin, e.g. "2cm".

##### Remarks

Supported units are mm, cm, m, in, pica(pc), pixel(px) and point(pt).

#### SetModulePath(string)

Set the first location that PDFNet will look for the html2pdf module.

##### Declaration

    public static void SetModulePath(string path)

##### Parameters

Type | Name | Description
--- | --- | ---
string | path | A folder or file path. If non-empty, PDFNet will only look in path for the html2pdf module, otherwise it will search in the default locations for the module.

#### SetOutline(bool, int)

Add bookmarks to the PDF.

##### Declaration

    public void SetOutline(bool enable, int depth)

##### Parameters

Type | Name | Description
--- | --- | ---
bool | enable | If true bookmarks will be generated for the produced PDF.
int | depth | Maximum depth of the outline (e.g. 4).

##### Remarks

This option is deprecated in the latest HTML2PDF module and may have no effect.

#### SetPDFCompression(bool)

Use lossless compression to create PDF.

##### Declaration

    public void SetPDFCompression(bool enable)

##### Parameters

Type | Name | Description
--- | --- | ---
bool | enable | If true loss less compression will be used to create PDF.

##### Remarks

This option is deprecated in the latest HTML2PDF module and may have no effect.

#### SetPaperSize(string, string)

Manually set the paper dimensions of the produced PDF.

##### Declaration

    public void SetPaperSize(string width, string height)

##### Parameters

Type | Name | Description
--- | --- | ---
string | width | Width of the page, e.g. "4cm".
string | height | Height of the page, eg. "12in".

##### Remarks

Supported units are mm, cm, m, in, pica(pc), pixel(px) and point(pt).

#### SetPaperSize(PaperSize)

Set paper size of output PDF

##### Declaration

    public void SetPaperSize(PrinterMode.PaperSize size)

##### Parameters

Type | Name | Description
--- | --- | ---
PrinterMode.PaperSize | size | Paper size to use for produced PDF.

#### SetQuiet(bool)

Display HTML to PDF conversion progress, warnings, and errors, to stdout.

##### Declaration

    public void SetQuiet(bool quiet)

##### Parameters

Type | Name | Description
--- | --- | ---
bool | quiet | If false, progress information is sent to stdout during conversion.

##### Remarks

You can get the final results using GetLog.

#### SetSandbox(bool)

Provides the ability to run HTML to PDF conversion with sandbox disabled. Default is true. Note: On Linux this option has no effect, because sandbox is always disabled there.

##### Declaration

    public void SetSandbox(bool sandbox)

##### Parameters

Type | Name | Description
--- | --- | ---
bool | sandbox | If false, '--no_sandbox' will be applied and the sandbox will not be used.

### Implements

IDisposable