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);
Extends
Classes
Methods
-
<static> create()
-
Default constructor.
Returns:
A promise that resolves to an object of type: "PDFNet.HTML2PDF"- Type
- Promise.<PDFNet.HTML2PDF>
-
<static> isModuleAvailable()
-
Find out whether the HTLML2PDF module is available (and licensed).
Returns:
A promise that resolves to returns true if HTLML2PDF operations can be performed.- Type
- Promise.<boolean>
-
<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. Returns:
- Type
- Promise.<void>
-
<static> staticConvert(doc, url)
-
Parameters:
Name Type Description docPDFNet.PDFDoc | PDFNet.SDFDoc | PDFNet.FDFDoc urlstring Returns:
A promise that resolves to an object of type: "boolean"- Type
- Promise.<boolean>
-
<static> staticConvert2(doc, url, settings)
-
Parameters:
Name Type Description docPDFNet.PDFDoc | PDFNet.SDFDoc | PDFNet.FDFDoc urlstring settingsPDFNet.HTML2PDF.WebPageSettings Returns:
A promise that resolves to an object of type: "boolean"- Type
- Promise.<boolean>
-
addCookie(name, value)
-
Add cookie to the HTTP Headers when converting via a URL.
Parameters:
Name Type Description namestring the name of the cookie. valuestring the value of the cookie. Returns:
- Type
- Promise.<void>
-
convert(doc)
-
Convert HTML documents and append the results to doc. Note: html2pdf module must be located in the working directory, or with the PDFNetC library.
Parameters:
Name Type Description docPDFNet.PDFDoc | PDFNet.SDFDoc | PDFNet.FDFDoc Target PDF to which converted HTML pages will be appended to. Note: Use @see InsertFromURL and @see InsertFromHtmlString to add HTML documents to be converted. - See:
-
- GetHttpErrorCode for possible HTTP errors.
Returns:
A promise that resolves to true if successful, otherwise false. Use- Type
- Promise.<boolean>
-
destroy()
-
Destructor
- Inherited From:
Returns:
- Type
- Promise.<void>
-
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. Note: This option is deprecated in the latest HTML2PDF module and may have no effect. Returns:
- Type
- Promise.<void>
-
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. Note: This function will only return a useful result after @see Convert has been called.- Type
- Promise.<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
- Promise.<string>
-
insertFromHtmlString(html)
-
Convert HTML encoded in string.
Parameters:
Name Type Description htmlstring String containing HTML code. Returns:
- Type
- Promise.<void>
-
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. Returns:
- Type
- Promise.<void>
-
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 Returns:
- Type
- Promise.<void>
-
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 Returns:
- Type
- Promise.<void>
-
insertTOC()
-
Add a table of contents to the produced PDF. Note: This option is deprecated in the latest HTML2PDF module and may have no effect.
Returns:
- Type
- Promise.<void>
-
insertTOC2(settings)
-
Add a table of contents to the produced PDF.
Parameters:
Name Type Description settingsPDFNet.HTML2PDF.TOCSettings Settings for the table of contents. Note: This option is deprecated in the latest HTML2PDF module and may have no effect. Returns:
- Type
- Promise.<void>
-
setCompatibilityMode(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. 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.
Parameters:
Name Type Description compatibilityboolean If true, compatibility mode is enabled. Returns:
- Type
- Promise.<void>
-
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. Note: This option is deprecated in the latest HTML2PDF module and may have no effect. Returns:
- Type
- Promise.<void>
-
setCustomHeader(name, value)
-
Add a custom HTTP header specified by name and value.
Parameters:
Name Type Description namestring the name of the custom header. valuestring the value of the custom header. Returns:
- Type
- Promise.<void>
-
setDPI(dpi)
-
Change the DPI explicitly for the output PDF.
Parameters:
Name Type Description dpinumber Dots per inch, e.g. 80. Attention: This has no effect on X11 based systems. Note: Results also depend on @see SetSmartShrinking. Returns:
- Type
- Promise.<void>
-
setFooter(footer)
-
Set footer of generated PDF.
Parameters:
Name Type Description footerstring HTML string to be used as the footer Returns:
- Type
- Promise.<void>
-
setHeader(header)
-
Set header of generated PDF.
Parameters:
Name Type Description headerstring HTML string to be used as the header Returns:
- Type
- Promise.<void>
-
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. Note: This option is deprecated in the latest HTML2PDF module and may have no effect. Returns:
- Type
- Promise.<void>
-
setImageQuality(quality)
-
JPEG compression factor to use when generating PDF.
Parameters:
Name Type Description qualitynumber Compression factor, e.g. 92. Note: This option is deprecated in the latest HTML2PDF module and may have no effect. Returns:
- Type
- Promise.<void>
-
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. Returns:
- Type
- Promise.<void>
-
setLogFilePath(path)
-
Sets the location of the log file to be used during conversion.
Parameters:
Name Type Description pathstring Full path and filename of file to log to. Returns:
- Type
- Promise.<void>
-
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". Note: Supported units are mm, cm, m, in, pc(pica), px(pixel) and pt(point). Returns:
- Type
- Promise.<void>
-
setOutline(enable [, depth])
-
Add bookmarks to the PDF.
Parameters:
Name Type Argument Description enableboolean If true bookmarks will be generated for the produced PDF. depthnumber <optional>
Maximum depth of the outline (e.g. 4). Note: This option is deprecated in the latest HTML2PDF module and may have no effect. Returns:
- Type
- Promise.<void>
-
setPaperSize(size)
-
Set paper size of output PDF
Parameters:
Name Type Description sizenumber PDFNet.PrinterMode.PaperSize = { e_custom : 0 e_letter : 1 e_letter_small : 2 e_tabloid : 3 e_ledger : 4 e_legal : 5 e_statement : 6 e_executive : 7 e_a3 : 8 e_a4 : 9 e_a4_mall : 10 e_a5 : 11 e_b4_jis : 12 e_b5_jis : 13 e_folio : 14 e_quarto : 15 e_10x14 : 16 e_11x17 : 17 e_note : 18 e_envelope_9 : 19 e_envelope_10 : 20 e_envelope_11 : 21 e_envelope_12 : 22 e_envelope_14 : 23 e_c_size_sheet : 24 e_d_size_sheet : 25 e_e_size_sheet : 26 e_envelope_dl : 27 e_envelope_c5 : 28 e_envelope_c3 : 29 e_envelope_c4 : 30 e_envelope_c6 : 31 e_envelope_c65 : 32 e_envelope_b4 : 33 e_envelope_b5 : 34 e_envelope_b6 : 35 e_envelope_italy : 36 e_envelope_monarch : 37 e_6_3_quarters_envelope : 38 e_us_std_fanfold : 39 e_german_std_fanfold : 40 e_german_legal_fanfold : 41 e_b4_iso : 42 e_japanese_postcard : 43 e_9x11 : 44 e_10x11 : 45 e_15x11 : 46 e_envelope_invite : 47 e_reserved_48 : 48 e_reserved_49 : 49 e_letter_extra : 50 e_legal_extra : 51 e_tabloid_extra : 52 e_a4_extra : 53 e_letter_transverse : 54 e_a4_transverse : 55 e_letter_extra_transverse : 56 e_supera_supera_a4 : 57 e_Superb_Superb_a3 : 58 e_letter_plus : 59 e_a4_plus : 60 e_a5_transverse : 61 e_b5_jis_transverse : 62 e_a3_extra : 63 e_a5_extra : 64 e_b5_iso_extra : 65 e_a2 : 66 e_a3_transverse : 67 e_a3_extra_transverse : 68 e_japanese_double_postcard : 69 e_a6 : 70 e_japanese_envelope_kaku_2 : 71 e_japanese_envelope_kaku_3 : 72 e_japanese_envelope_chou_3 : 73 e_japanese_envelope_chou_4 : 74 e_letter_rotated : 75 e_a3_rotated : 76 e_a4_rotated : 77 e_a5_rotated : 78 e_b4_jis_rotated : 79 e_b5_jis_rotated : 80 e_japanese_postcard_rotated : 81 e_double_japanese_postcard_rotated : 82 e_a6_rotated : 83 e_japanese_envelope_kaku_2_rotated : 84 e_japanese_envelope_kaku_3_rotated : 85 e_japanese_envelope_chou_3_rotated : 86 e_japanese_envelope_chou_4_rotated : 87 e_b6_jis : 88 e_b6_jis_rotated : 89 e_12x11 : 90 e_japanese_envelope_you_4 : 91 e_japanese_envelope_you_4_rotated : 92 e_PrinterMode_prc_16k : 93 e_prc_32k : 94 e_prc_32k_big : 95 e_prc_envelop_1 : 96 e_prc_envelop_2 : 97 e_prc_envelop_3 : 98 e_prc_envelop_4 : 99 e_prc_envelop_5 : 100 e_prc_envelop_6 : 101 e_prc_envelop_7 : 102 e_prc_envelop_8 : 103 e_prc_envelop_9 : 104 e_prc_envelop_10 : 105 e_prc_16k_rotated : 106 e_prc_32k_rotated : 107 e_prc_32k_big__rotated : 108 e_prc_envelop_1_rotated : 109 e_prc_envelop_2_rotated : 110 e_prc_envelop_3_rotated : 111 e_prc_envelop_4_rotated : 112 e_prc_envelop_5_rotated : 113 e_prc_envelop_6_rotated : 114 e_prc_envelop_7_rotated : 115 e_prc_envelop_8_rotated : 116 e_prc_envelop_9_rotated : 117 e_prc_envelop_10_rotated : 118 }Paper size to use for produced PDF.Returns:
- Type
- Promise.<void>
-
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". Note: Supported units are mm, cm, m, in, pc(pica), px(pixel) and pt(point). Returns:
- Type
- Promise.<void>
-
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 Note: This option is deprecated in the latest HTML2PDF module and may have no effect. Returns:
- Type
- Promise.<void>
-
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. Note: You can get the final results using GetLog. Returns:
- Type
- Promise.<void>
-
takeOwnership()
-
Take the ownership of this object, so that PDFNet.runWithCleanup won't destroy this object.
- Inherited From:
Returns:
- Type
- void