new 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 namespace pdftron; using namespace PDF; PDFDoc pdfdoc; if ( HTML2PDF::Convert(pdfdoc, "http://www.gutenberg.org/wiki/Main_Page") ) 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.
using namespace pdftron;
using namespace PDF;
HTML2PDF converter;
converter.SetImageQuality(25);
HTML2PDF::WebPageSettings settings;
settings.SetPrintBackground(false);
converter.InsertFromURL("http://www.gutenberg.org/wiki/Main_Page", settings);
PDFDoc pdfdoc;
if ( converter.Convert(pdfdoc) )
pdfdoc.Save(outputFile, SDF::SDFDoc::e_remove_unused, NULL);
Methods
-
<static> create()
-
Default constructor.
Returns:
A promise that resolves to an object of type: "HTML2PDF"
- Type
- PDFNet.HTML2PDF
-
<static> setModulePath(path)
-
Set the only location that PDFNet will look for the html2pdf module.
Parameters:
Name Type Description pathstring 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.
-
convert(doc)
-
Convert HTML documents and append the results to doc.
Parameters:
Name Type Description docPDFNet.PDFDoc Target PDF to which converted HTML pages will be appended to.
Returns:
A promise that resolves to true if successful, otherwise false. Use
- Type
- boolean
-
destroy()
-
Frees the native memory of the object.
-
dumpOutline(xml_file)
-
Save outline to a xml file.
Parameters:
Name Type Description xml_filestring Path of where xml data representing outline of produced PDF should be saved to.
-
getHttpErrorCode()
-
Return the largest HTTP error code encountered during conversion
Returns:
A promise that resolves to 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.
- Type
- number
-
getLog()
-
Get results of conversion, including errors and warnings, in human readable form.
Returns:
A promise that resolves to string containing results of conversion.
- Type
- string
-
insertFromHtmlString(html)
-
Convert HTML encoded in string.
Parameters:
Name Type Description htmlstring String containing HTML code.
-
insertFromHtmlString2(html, settings)
-
Convert HTML encoded in string.
Parameters:
Name Type Description htmlstring String containing HTML code.
settingsPDFNet.HTML2PDF_WebPageSettings How the HTML content described in html is loaded.
-
insertFromUrl(url)
-
Add a web page to be converted. A single URL typically results in many PDF pages.
Parameters:
Name Type Description urlstring HTML page, or relative path to local HTML page
-
insertFromUrl2(url, settings)
-
Add a web page to be converted. A single URL typically results in many PDF pages.
Parameters:
Name Type Description urlstring HTML page, or relative path to local HTML page
settingsPDFNet.HTML2PDF_WebPageSettings How the web page should be loaded and converted
-
insertTOC()
-
Add a table of contents to the produced PDF.
-
insertTOC2(settings)
-
Add a table of contents to the produced PDF.
Parameters:
Name Type Description settingsPDFNet.HTML2PDF_TOCSettings Settings for the table of contents.
-
setCookieJar(path)
-
Path of file used for loading and storing cookies.
Parameters:
Name Type Description pathstring Path to file used for loading and storing cookies.
-
setDPI(dpi)
-
Change the DPI explicitly for the output PDF.
Parameters:
Name Type Description dpinumber Dots per inch, e.g. 80.
-
setImageDPI(dpi)
-
Maximum DPI to use for images in the generated PDF.
Parameters:
Name Type Description dpinumber Maximum dpi of images in produced PDF, e.g. 80.
-
setImageQuality(quality)
-
JpEG compression factor to use when generating PDF.
Parameters:
Name Type Description qualitynumber Compression factor, e.g. 92.
-
setLandscape(enable)
-
Set page orientation for output PDF.
Parameters:
Name Type Description enableboolean If true generated PDF pages will be orientated to landscape, otherwise orientation will be portrait.
-
setMargins(top, bottom, left, right)
-
Set margins of generated PDF.
Parameters:
Name Type Description topstring Size of the top margin, e.g. "2cm".
bottomstring Size of the bottom margin, e.g. "2cm".
leftstring Size of the left margin, e.g. "2cm".
rightstring Size of the right margin, e.g. "2cm".
-
setOutline(enable, depth)
-
Add bookmarks to the PDF.
Parameters:
Name Type Description enableboolean If true bookmarks will be generated for the produced PDF.
depthnumber Maximum depth of the outline (e.g. 4).
-
setPaperSize2(width, height)
-
Manually set the paper dimensions of the produced PDF.
Parameters:
Name Type Description widthstring Width of the page, e.g. "4cm".
heightstring Height of the page, eg. "12in".
-
setPDFCompression(enable)
-
Use loss less compression to create PDF.
Parameters:
Name Type Description enableboolean If true loss less compression will be used to create PDF.
-
setQuiet(quiet)
-
Display HTML to PDF conversion progress, warnings, and errors, to stdout.
Parameters:
Name Type Description quietboolean If false, progress information is sent to stdout during conversion.