public class HTML2PDF
extends java.lang.Object
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);
Modifier and Type | Class and Description |
---|---|
static class |
HTML2PDF.Proxy
Proxy settings to be used when loading content from web pages.
|
static class |
HTML2PDF.TOCSettings
Settings for table of contents.
|
static class |
HTML2PDF.WebPageSettings
Settings that control how a web page is opened and converted to PDF.
|
Constructor and Description |
---|
HTML2PDF()
Default constructor.
|
Modifier and Type | Method and Description |
---|---|
boolean |
convert(Doc doc)
Convert HTML documents and append the results
to doc.
|
static boolean |
convert(Doc doc,
java.lang.String url)
Convert the HTML document at url and append the results
to doc.
|
static boolean |
convert(Doc doc,
java.lang.String url,
HTML2PDF.WebPageSettings settings)
Convert the HTML document at url and append the results
to doc.
|
void |
destroy()
Frees the native memory of the object.
|
void |
dumpOutline(java.lang.String xml_file)
Save outline to a xml file?
|
int |
getHTTPErrorCode()
Return the largest HTTP error code encountered during conversion
|
java.lang.String |
getLog()
Get results of conversion, including errors and warnings, in human readable form.
|
void |
insertFromHtmlString(java.lang.String html)
Convert HTML encoded in string.
|
void |
insertFromHtmlString(java.lang.String html,
HTML2PDF.WebPageSettings settings)
Convert HTML encoded in string.
|
void |
insertFromURL(java.lang.String url)
Add a web page to be converted.
|
void |
insertFromURL(java.lang.String url,
HTML2PDF.WebPageSettings settings)
Add a web page to be converted.
|
void |
insertTOC()
Add a table of contents to the produced PDF.
|
void |
insertTOC(HTML2PDF.TOCSettings settings)
Add a table of contents to the produced PDF.
|
void |
setCookieJar(java.lang.String path)
Path of file used for loading and storing cookies.
|
void |
setDPI(int dpi)
Change the DPI explicitly for the output 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 |
setMargins(java.lang.String top,
java.lang.String bottom,
java.lang.String left,
java.lang.String right)
Set margins of generated PDF.
|
static void |
setModulePath(java.lang.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 |
setPaperSize(int size_type)
Set paper size of output PDF
|
void |
setPaperSize(java.lang.String width,
java.lang.String height)
Manually set the paper dimensions of the produced PDF.
|
void |
setPDFCompression(boolean enable)
Use loss less compression to create PDF?
|
void |
setQuiet(boolean quiet)
Display HTML to PDF conversion progress, warnings, and errors, to stdout?
|
public HTML2PDF() throws PDFNetException
PDFNetException
public static boolean convert(Doc doc, java.lang.String url) throws PDFNetException
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
public static boolean convert(Doc doc, java.lang.String url, HTML2PDF.WebPageSettings settings) throws PDFNetException
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
public void insertFromURL(java.lang.String url) throws PDFNetException
url
- - HTML page, or relative path to local HTML pagePDFNetException
public void insertFromURL(java.lang.String url, HTML2PDF.WebPageSettings settings) throws PDFNetException
url
- - HTML page, or relative path to local HTML pagesettings
- - How the web page should be loaded and convertedPDFNetException
public void insertFromHtmlString(java.lang.String html) throws PDFNetException
html
- - String containing HTML code.PDFNetException
public void insertFromHtmlString(java.lang.String html, HTML2PDF.WebPageSettings settings) throws PDFNetException
html
- - String containing HTML code.settings
- - How the HTML content described in html is loaded.PDFNetException
public void insertTOC() throws PDFNetException
PDFNetException
public void insertTOC(HTML2PDF.TOCSettings settings) throws PDFNetException
settings
- - Settings for the table of contents.PDFNetException
public boolean convert(Doc doc) throws PDFNetException
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
public int getHTTPErrorCode() throws PDFNetException
Note: This function will only return a useful result after Convert has been called.
PDFNetException
public void setQuiet(boolean quiet) throws PDFNetException
Note: You can get the final results using getLog.
quiet
- - If false, progress information is sent to stdout during conversion.PDFNetException
public java.lang.String getLog() throws PDFNetException
PDFNetException
public static void setModulePath(java.lang.String path) throws PDFNetException
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
public void setPaperSize(int size_type) throws PDFNetException
size_type
- - Paper size to use for produced PDF. See pdftron.PDF.PrinterMode
for available types.PDFNetException
public void setPaperSize(java.lang.String width, java.lang.String height) throws PDFNetException
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
public void setLandscape(boolean enable) throws PDFNetException
enable
- - If true generated PDF pages will be orientated to
landscape, otherwise orientation will be portrait.PDFNetException
public void setDPI(int dpi) throws PDFNetException
Note: This has no effect on X11 based systems.
Note: Results also depend on SetSmartShrinking.
dpi
- - Dots per inch, e.g. 80.PDFNetException
public void setOutline(boolean enable, int depth) throws PDFNetException
enable
- - If true bookmarks will be generated for the produced PDF.depth
- - Maximum depth of the outline (e.g. 4).PDFNetException
public void dumpOutline(java.lang.String xml_file) throws PDFNetException
xml_file
- - Path of where xml data representing outline
of produced PDF should be saved to.PDFNetException
public void setPDFCompression(boolean enable) throws PDFNetException
enable
- - If true loss less compression will be used to create PDF.PDFNetException
public void setMargins(java.lang.String top, java.lang.String bottom, java.lang.String left, java.lang.String right) throws PDFNetException
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
public void setImageDPI(int dpi) throws PDFNetException
dpi
- - Maximum dpi of images in produced PDF, e.g. 80.PDFNetException
public void setImageQuality(int quality) throws PDFNetException
quality
- - Compression factor, e.g. 92.PDFNetException
public void setCookieJar(java.lang.String path) throws PDFNetException
path
- - Path to file used for loading and storing cookies.PDFNetException
public void destroy()