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
System.Object
HTML2PDF
Implements
System.IDisposable
Inherited Members
System.Object.Equals(System.Object)
System.Object.Equals(System.Object, System.Object)
System.Object.GetHashCode()
System.Object.GetType()
System.Object.MemberwiseClone()
System.Object.ReferenceEquals(System.Object, System.Object)
System.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
System.String name

the name of the cookie

System.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
System.Boolean

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.

System.String url

HTML page, or relative path to local HTML page, that will be converted to PDF format.

Returns
Type Description
System.Boolean

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, HTML2PDF.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.

System.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
System.Boolean

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()

Declaration
public void Dispose()

Dispose(Boolean)

Declaration
protected virtual void Dispose(bool disposing)
Parameters
Type Name Description
System.Boolean disposing

DumpOutline(String)

Save outline to a xml file.

Declaration
public void DumpOutline(string xml_file)
Parameters
Type Name Description
System.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.

Finalize()

Releases all resources used by the HTML2PDF

Declaration
protected void Finalize()

GetHTTPErrorCode()

Return the largest HTTP error code encountered during conversion

Declaration
public int GetHTTPErrorCode()
Returns
Type Description
System.Int32

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
System.String

String containing results of conversion.

InsertFromHtmlString(String)

Convert HTML encoded in string.

Declaration
public void InsertFromHtmlString(string html)
Parameters
Type Name Description
System.String html

String containing HTML code.

InsertFromHtmlString(String, HTML2PDF.WebPageSettings)

Convert HTML encoded in string.

Declaration
public void InsertFromHtmlString(string html, HTML2PDF.WebPageSettings settings)
Parameters
Type Name Description
System.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
System.String url

InsertFromURL(String, HTML2PDF.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
System.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(HTML2PDF.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
System.Boolean

returns true if HTML2PDF operations can be performed

SetCookieJar(String)

Path of file used for loading and storing cookies.

Declaration
public void SetCookieJar(string path)
Parameters
Type Name Description
System.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.

SetDPI(Int32)

Change the DPI explicitly for the output PDF.

Declaration
public void SetDPI(int dpi)
Parameters
Type Name Description
System.Int32 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
System.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
System.String header

HTML string to be used as the header

SetImageDPI(Int32)

Maximum DPI to use for images in the generated PDF.

Declaration
public void SetImageDPI(int dpi)
Parameters
Type Name Description
System.Int32 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(Int32)

JPEG compression factor to use when generating PDF.

Declaration
public void SetImageQuality(int quality)
Parameters
Type Name Description
System.Int32 quality

Compression factor, e.g. 92.

Remarks

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

SetLandscape(Boolean)

Set page orientation for output PDF.

Declaration
public void SetLandscape(bool enable)
Parameters
Type Name Description
System.Boolean enable

If true generated PDF pages will be orientated to landscape, otherwise orientation will be portrait.

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
System.String top

Size of the top margin, e.g. "2cm".

System.String bottom

Size of the bottom margin, e.g. "2cm".

System.String left

Size of the left margin, e.g. "2cm".

System.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
System.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(Boolean, Int32)

Add bookmarks to the PDF.

Declaration
public void SetOutline(bool enable, int depth)
Parameters
Type Name Description
System.Boolean enable

If true bookmarks will be generated for the produced PDF.

System.Int32 depth

Maximum depth of the outline (e.g. 4).

Remarks

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

SetPaperSize(PrinterMode.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.

SetPaperSize(String, String)

Manually set the paper dimensions of the produced PDF.

Declaration
public void SetPaperSize(string width, string height)
Parameters
Type Name Description
System.String width

Width of the page, e.g. "4cm".

System.String height

Height of the page, eg. "12in".

Remarks

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

SetPDFCompression(Boolean)

Use lossless compression to create PDF.

Declaration
public void SetPDFCompression(bool enable)
Parameters
Type Name Description
System.Boolean 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.

SetQuiet(Boolean)

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

Declaration
public void SetQuiet(bool quiet)
Parameters
Type Name Description
System.Boolean quiet

If false, progress information is sent to stdout during conversion.

Remarks

You can get the final results using GetLog.

Implements

System.IDisposable
Back to top Generated by DocFX