java.lang.Object | |
↳ | com.pdftron.pdf.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
import pdftron.PDF.*;
import pdftron.SDF.*;
PDFDoc pdfdoc = new PDFDoc();
if ( HTML2PDF.convert(pdfdoc, "http://www.gutenberg.org/wiki/Main_Page") == 1 )
pdfdoc.save(outputFile, SDF.SDFDoc.e_remove_unused, null);
The following code demonstrates how to convert multiple web pages into one pdf,
excluding the background, and with lowered image quality to save space.
import pdftron.PDF.*;
import pdftron.SDF.*;
HTML2PDF converter = new HTML2PDF();
converter.setImageQuality(25);
HTML2PDF.WebPageSettings settings = HTML2PDF.WebPageSettings();
settings.setPrintBackground(false);
converter.insertFromURL("http://www.gutenberg.org/wiki/Main_Page", settings);
PDFDoc pdfdoc = new PDFDoc();
if ( converter.convert(pdfdoc) )
pdfdoc.save(outputFile, SDF.SDFDoc.e_remove_unused, null);
Nested Classes | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
class | HTML2PDF.Proxy | Proxy settings to be used when loading content from web pages. | |||||||||
class | HTML2PDF.TOCSettings | Settings for table of contents. | |||||||||
class | HTML2PDF.WebPageSettings | Settings that control how a web page is opened and converted to PDF. |
Public Constructors | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
HTML2PDF()
Default constructor.
|
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
void |
addCookie(String name, String value)
Add cookie to the HTTP Headers when converting via a URL.
| ||||||||||
void |
close()
Frees the native memory of the object.
| ||||||||||
static boolean |
convert(Doc doc, String url, HTML2PDF.WebPageSettings settings)
Convert the HTML document at url and append the results
to doc.
| ||||||||||
boolean |
convert(Doc doc)
Convert HTML documents and append the results
to doc.
| ||||||||||
static boolean |
convert(Doc doc, String url)
Convert the HTML document at url and append the results
to doc.
| ||||||||||
void |
destroy()
Frees the native memory of the object.
| ||||||||||
void |
dumpOutline(String xml_file)
Save outline to a xml file.
| ||||||||||
int |
getHTTPErrorCode()
Return the largest HTTP error code encountered during conversion
Note: This function will only return a useful result after Convert has been called. | ||||||||||
String |
getLog()
Get results of conversion, including errors and warnings, in human readable form.
| ||||||||||
void |
insertFromHtmlString(String html)
Convert HTML encoded in string.
| ||||||||||
void |
insertFromHtmlString(String html, HTML2PDF.WebPageSettings settings)
Convert HTML encoded in string.
| ||||||||||
void |
insertFromURL(String url)
Add a web page to be converted.
| ||||||||||
void |
insertFromURL(String url, HTML2PDF.WebPageSettings settings)
Add a web page to be converted.
| ||||||||||
void |
insertTOC(HTML2PDF.TOCSettings settings)
Add a table of contents to the produced PDF.
| ||||||||||
void |
insertTOC()
Add a table of contents to the produced PDF.
| ||||||||||
static boolean |
isModuleAvailable()
Find out whether the HTML2PDF module is available (and licensed).
| ||||||||||
void |
setCompatibilityMode(boolean compatibility)
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.
| ||||||||||
void |
setCookieJar(String path)
Path of file used for loading and storing cookies.
| ||||||||||
void |
setCustomHeader(String name, String value)
Add a custom HTTP header specified by name and value.
| ||||||||||
void |
setDPI(int dpi)
Change the DPI explicitly for the output PDF.
| ||||||||||
void |
setFooter(String footer)
Set footer of generated PDF.
| ||||||||||
void |
setHeader(String header)
Set header of generated PDF.
| ||||||||||
void |
setImageDPI(int dpi)
Maximum DPI to use for images in the generated PDF.
| ||||||||||
void |
setImageQuality(int quality)
JPEG compression factor to use when generating PDF.
| ||||||||||
void |
setLandscape(boolean enable)
Set page orientation for output PDF.
| ||||||||||
void |
setLogFilePath(String path)
Sets the location of the log file to be used during conversion.
| ||||||||||
void |
setMargins(String top, String bottom, String left, String right)
Set margins of generated PDF.
| ||||||||||
static void |
setModulePath(String path)
Set the first location that PDFNet will look for the html2pdf module.
| ||||||||||
void |
setOutline(boolean enable, int depth)
Add bookmarks to the PDF.
| ||||||||||
void |
setPDFCompression(boolean enable)
Use loss less compression to create PDF.
| ||||||||||
void |
setPaperSize(int size_type)
Set paper size of output PDF
| ||||||||||
void |
setPaperSize(String width, String height)
Manually set the paper dimensions of the produced PDF.
| ||||||||||
void |
setQuiet(boolean quiet)
Display HTML to PDF conversion progress, warnings, and errors, to stdout.
|
[Expand]
Inherited Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
From class
java.lang.Object
| |||||||||||
From interface
java.lang.AutoCloseable
|
Add cookie to the HTTP Headers when converting via a URL.
name | - the name of the cookie |
---|---|
value | - the value of the cookie |
PDFNetException |
---|
Frees the native memory of the object. This can be explicity called to control the deallocation of native memory and avoid situations where the garbage collector does not free the object in a timely manner.
PDFNetException |
---|
Convert the HTML document at url and append the results to doc. html2pdf module must be located in the working directory, or with the PDFNetC library.
Note: If you wish to convert more than one web page, or to setup callback handlers, you need to use an instance of HTML2PDF.
doc | - Target PDF to which converted HTML pages will be appended to. |
---|---|
url | - HTML page, or relative path to local HTML page, that will be converted to PDF format. |
settings | - Modify how the web page is loaded and converted. |
PDFNetException |
---|
Convert HTML documents and append the results to doc. html2pdf module must be located in the working directory, or with the PDFNetC library.
Note: Use insertFromURL and InsertFromHtmlString to add HTML documents to be converted.
doc | - Target PDF to which converted HTML pages will be appended to. |
---|
PDFNetException |
---|
Convert the HTML document at url and append the results to doc. html2pdf module must be located in the working directory, or with the PDFNetC library.
Note: If you wish to convert more than one web page, or to setup callback handlers, you need to use an instance of HTML2PDF.
doc | - Target PDF to which converted HTML pages will be appended to. |
---|---|
url | - HTML page, or relative path to local HTML page, that will be converted to PDF format. |
PDFNetException |
---|
Frees the native memory of the object. This can be explicity called to control the deallocation of native memory and avoid situations where the garbage collector does not free the object in a timely manner.
Save outline to a xml file.
xml_file | - Path of where xml data representing outline of produced PDF should be saved to. Note: This option is deprecated in the latest HTML2PDF module and may have no effect. |
---|
PDFNetException |
---|
Return the largest HTTP error code encountered during conversion
Note: This function will only return a useful result after Convert has been called.
PDFNetException |
---|
Get results of conversion, including errors and warnings, in human readable form.
PDFNetException |
---|
Convert HTML encoded in string.
html | - String containing HTML code. |
---|
PDFNetException |
---|
Convert HTML encoded in string.
html | - String containing HTML code. |
---|---|
settings | - How the HTML content described in html is loaded. |
PDFNetException |
---|
Add a web page to be converted. A single URL typically results in many PDF pages.
url | - HTML page, or relative path to local HTML page |
---|
PDFNetException |
---|
Add a web page to be converted. A single URL typically results in many PDF pages.
url | - HTML page, or relative path to local HTML page |
---|---|
settings | - How the web page should be loaded and converted |
PDFNetException |
---|
Add a table of contents to the produced PDF.
settings | Settings for the table of contents. Note: This option is deprecated in the latest HTML2PDF module and may have no effect. |
---|
PDFNetException |
---|
Add a table of contents to the produced PDF. Note: This option is deprecated in the latest HTML2PDF module and may have no effect.
PDFNetException |
---|
Find out whether the HTML2PDF module is available (and licensed).
PDFNetException |
---|
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.
compatibility | - If true, compatibility mode is enabled. |
---|
PDFNetException |
---|
Path of file used for loading and storing cookies.
path | - Path to file used for loading and storing cookies. Note: This option is deprecated in the latest HTML2PDF module and may have no effect. |
---|
PDFNetException |
---|
Add a custom HTTP header specified by name and value.
name | - the name of the custom header |
---|---|
value | - the value of the custom header |
PDFNetException |
---|
Change the DPI explicitly for the output PDF.
Note: This has no effect on X11 based systems.
Note: Results also depend on SetSmartShrinking.
dpi | - Dots per inch, e.g. 80. |
---|
PDFNetException |
---|
Set footer of generated PDF.
footer | HTML string to be used as the footer |
---|
PDFNetException |
---|
Set header of generated PDF.
header | HTML string to be used as the header |
---|
PDFNetException |
---|
Maximum DPI to use for images in the generated PDF.
dpi | - Maximum dpi of images in produced PDF, e.g. 80. Note: This option is deprecated in the latest HTML2PDF module and may have no effect. |
---|
PDFNetException |
---|
JPEG compression factor to use when generating PDF.
quality | - Compression factor, e.g. 92. Note: This option is deprecated in the latest HTML2PDF module and may have no effect. |
---|
PDFNetException |
---|
Set page orientation for output PDF.
enable | - If true generated PDF pages will be orientated to landscape, otherwise orientation will be portrait. |
---|
PDFNetException |
---|
Sets the location of the log file to be used during conversion.
path | Set the full path and file name of the file to log to. |
---|
PDFNetException |
---|
Set margins of generated PDF.
Note: Supported units are mm, cm, m, in, pica(pc), pixel(px) and point(pt).
top | - Size of the top margin, e.g. "2cm". |
---|---|
bottom | - Size of the bottom margin, e.g. "2cm". |
left | - Size of the left margin, e.g. "2cm". |
right | - Size of the right margin, e.g. "2cm". |
PDFNetException |
---|
Set the first location that PDFNet will look for the html2pdf module.
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. |
---|
PDFNetException |
---|
Add bookmarks to the PDF.
enable | - If true bookmarks will be generated for the produced PDF. |
---|---|
depth | - Maximum depth of the outline (e.g. 4). Note: This option is deprecated in the latest HTML2PDF module and may have no effect. |
PDFNetException |
---|
Use loss less compression to create PDF.
enable | - If true loss less compression will be used to create PDF. Note: This option is deprecated in the latest HTML2PDF module and may have no effect. |
---|
PDFNetException |
---|
Set paper size of output PDF
size_type | - Paper size to use for produced PDF. See pdftron.PDF.PrinterMode for available types. |
---|
PDFNetException |
---|
Manually set the paper dimensions of the produced PDF.
Note: Supported units are mm, cm, m, in, pica(pc), pixel(px) and point(pt).
width | - Width of the page, e.g. "4cm". |
---|---|
height | - Height of the page, eg. "12in". |
PDFNetException |
---|
Display HTML to PDF conversion progress, warnings, and errors, to stdout.
Note: You can get the final results using getLog.
quiet | - If false, progress information is sent to stdout during conversion. |
---|
PDFNetException |
---|