Sample C# code for using Apryse SDK to directly convert HTML pages to PDF by using 'pdftron.PDF.HTML2PDF'. The HTML2PDF converter supports conversion from a string or URL and offers many options to control page size and formatting. Learn more about our Server SDK and PDF Conversion Library.
1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2024 by Apryse Software Inc. All Rights Reserved.
3// Consult legal.txt regarding legal and license information.
4//---------------------------------------------------------------------------------------
5
6using System;
7using System.IO;
8
9using pdftron;
10using pdftron.Common;
11using pdftron.SDF;
12using pdftron.PDF;
13
14namespace HTML2PDFTestCS
15{
16 //---------------------------------------------------------------------------------------
17 // The following sample illustrates how to convert HTML pages to PDF format using
18 // the HTML2PDF class.
19 //
20 // 'pdftron.PDF.HTML2PDF' is an optional PDFNet Add-On utility class that can be
21 // used to convert HTML web pages into PDF documents by using an external module (html2pdf).
22 //
23 // html2pdf modules can be downloaded from http://www.pdftron.com/pdfnet/downloads.html.
24 //
25 // Users can convert HTML pages to PDF using the following operations:
26 // - Simple one line static method to convert a single web page to PDF.
27 // - Convert HTML pages from URL or string, plus optional table of contents, in user defined order.
28 // - Optionally configure settings for proxy, images, java script, and more for each HTML page.
29 // - Optionally configure the PDF output, including page size, margins, orientation, and more.
30 // - Optionally add table of contents, including setting the depth and appearance.
31 //---------------------------------------------------------------------------------------
32 class HTML2PDFSample
33 {
34 private static pdftron.PDFNetLoader pdfNetLoader = pdftron.PDFNetLoader.Instance();
35 static HTML2PDFSample() {}
36
37 static void Main(string[] args)
38 {
39 string output_path = "../../../../TestFiles/Output/html2pdf_example";
40 string host = "https://docs.apryse.com";
41 string page0 = "/";
42 string page1 = "/all-products/";
43 string page2 = "/web/faq";
44
45 // The first step in every application using PDFNet is to initialize the
46 // library and set the path to common PDF resources. The library is usually
47 // initialized only once, but calling Initialize() multiple times is also fine.
48 PDFNet.Initialize(PDFTronLicense.Key);
49 // For HTML2PDF we need to locate the html2pdf module. If placed with the
50 // PDFNet library, or in the current working directory, it will be loaded
51 // automatically. Otherwise, it must be set manually using HTML2PDF.SetModulePath().
52 HTML2PDF.SetModulePath("../../../../../Lib");
53 if (!HTML2PDF.IsModuleAvailable())
54 {
55 Console.WriteLine();
56 Console.WriteLine("Unable to run HTML2PDFTest: Apryse SDK HTML2PDF module not available.");
57 Console.WriteLine("---------------------------------------------------------------");
58 Console.WriteLine("The HTML2PDF module is an optional add-on, available for download");
59 Console.WriteLine("at http://www.pdftron.com/. If you have already downloaded this");
60 Console.WriteLine("module, ensure that the SDK is able to find the required files");
61 Console.WriteLine("using the HTML2PDF.SetModulePath() function.");
62 Console.WriteLine();
63 return;
64 }
65
66 //--------------------------------------------------------------------------------
67 // Example 1) Simple conversion of a web page to a PDF doc.
68
69 try
70 {
71 using (PDFDoc doc = new PDFDoc())
72 {
73
74 // now convert a web page, sending generated PDF pages to doc
75 HTML2PDF.Convert(doc, host + page0);
76 doc.Save(output_path + "_01.pdf", SDFDoc.SaveOptions.e_linearized);
77 }
78 }
79 catch (PDFNetException e)
80 {
81 Console.WriteLine(e.Message);
82 }
83
84 //--------------------------------------------------------------------------------
85 // Example 2) Modify the settings of the generated PDF pages and attach to an
86 // existing PDF document.
87
88 try
89 {
90 // open the existing PDF, and initialize the security handler
91 using (PDFDoc doc = new PDFDoc("../../../../TestFiles/numbered.pdf"))
92 {
93 doc.InitSecurityHandler();
94
95 // create the HTML2PDF converter object and modify the output of the PDF pages
96 HTML2PDF converter = new HTML2PDF();
97 converter.SetPaperSize(PrinterMode.PaperSize.e_11x17);
98
99 // insert the web page to convert
100 converter.InsertFromURL(host + page0);
101
102 // convert the web page, appending generated PDF pages to doc
103 converter.Convert(doc);
104 doc.Save(output_path + "_02.pdf", SDFDoc.SaveOptions.e_linearized);
105 }
106 }
107 catch (PDFNetException e)
108 {
109 Console.WriteLine(e.Message);
110 }
111
112 //--------------------------------------------------------------------------------
113 // Example 3) Convert multiple web pages
114
115 try
116 {
117 using (PDFDoc doc = new PDFDoc())
118 {
119 // convert page 0 into pdf
120 HTML2PDF converter = new HTML2PDF();
121 string header = "<div style='width:15%;margin-left:0.5cm;text-align:left;font-size:10px;color:#0000FF'><span class='date'></span></div><div style='width:70%;direction:rtl;white-space:nowrap;overflow:hidden;text-overflow:clip;text-align:center;font-size:10px;color:#0000FF'><span>PDFTRON HEADER EXAMPLE</span></div><div style='width:15%;margin-right:0.5cm;text-align:right;font-size:10px;color:#0000FF'><span class='pageNumber'></span> of <span class='totalPages'></span></div>";
122 string footer = "<div style='width:15%;margin-left:0.5cm;text-align:left;font-size:7px;color:#FF00FF'><span class='date'></span></div><div style='width:70%;direction:rtl;white-space:nowrap;overflow:hidden;text-overflow:clip;text-align:center;font-size:7px;color:#FF00FF'><span>PDFTRON FOOTER EXAMPLE</span></div><div style='width:15%;margin-right:0.5cm;text-align:right;font-size:7px;color:#FF00FF'><span class='pageNumber'></span> of <span class='totalPages'></span></div>";
123 converter.SetHeader(header);
124 converter.SetFooter(footer);
125 converter.SetMargins("1cm", "2cm", ".5cm", "1.5cm");
126
127 HTML2PDF.WebPageSettings settings = new HTML2PDF.WebPageSettings();
128 settings.SetZoom(0.5);
129 converter.InsertFromURL(host + page0, settings);
130 converter.Convert(doc);
131
132 // convert page 1 with the same settings, appending generated PDF pages to doc
133 converter.InsertFromURL(host + page1, settings);
134 converter.Convert(doc);
135
136 // convert page 2 with different settings, appending generated PDF pages to doc
137 HTML2PDF another_converter = new HTML2PDF();
138 another_converter.SetLandscape(true);
139 HTML2PDF.WebPageSettings another_settings = new HTML2PDF.WebPageSettings();
140 another_settings.SetPrintBackground(false);
141 another_converter.InsertFromURL(host + page2, another_settings);
142 another_converter.Convert(doc);
143
144 doc.Save(output_path + "_03.pdf", SDFDoc.SaveOptions.e_linearized);
145 }
146 }
147 catch (PDFNetException e)
148 {
149 Console.WriteLine(e.Message);
150 }
151
152 //--------------------------------------------------------------------------------
153 // Example 4) Convert HTML string to PDF.
154
155 try
156 {
157 using (PDFDoc doc = new PDFDoc())
158 {
159
160 HTML2PDF converter = new HTML2PDF();
161
162 // Our HTML data
163 string html = "<html><body><h1>Heading</h1><p>Paragraph.</p></body></html>";
164
165 // Add html data
166 converter.InsertFromHtmlString(html);
167 // Note, InsertFromHtmlString can be mixed with the other Insert methods.
168
169 converter.Convert(doc);
170 doc.Save(output_path + "_04.pdf", SDFDoc.SaveOptions.e_linearized);
171 }
172 }
173 catch (PDFNetException e)
174 {
175 Console.WriteLine(e.Message);
176 }
177
178 //--------------------------------------------------------------------------------
179 // Example 5) Set the location of the log file to be used during conversion.
180
181 try
182 {
183 using (PDFDoc doc = new PDFDoc())
184 {
185 HTML2PDF converter = new HTML2PDF();
186 converter.SetLogFilePath("../../../../TestFiles/Output/html2pdf.log");
187 converter.InsertFromURL(host + page0);
188 converter.Convert(doc);
189 doc.Save(output_path + "_05.pdf", SDFDoc.SaveOptions.e_linearized);
190 }
191 }
192 catch (PDFNetException e)
193 {
194 Console.WriteLine(e.Message);
195 }
196
197 PDFNet.Terminate();
198 }
199 }
200}
1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2024 by Apryse Software Inc. All Rights Reserved.
3// Consult legal.txt regarding legal and license information.
4//---------------------------------------------------------------------------------------
5
6#include <iostream>
7#include <PDF/PDFNet.h>
8#include <PDF/PDFDoc.h>
9#include <PDF/HTML2PDF.h>
10#include "../../LicenseKey/CPP/LicenseKey.h"
11
12using namespace std;
13using namespace pdftron;
14using namespace Common;
15using namespace SDF;
16using namespace PDF;
17
18//---------------------------------------------------------------------------------------
19// The following sample illustrates how to convert HTML pages to PDF format using
20// the HTML2PDF class.
21//
22// 'pdftron.PDF.HTML2PDF' is an optional PDFNet Add-On utility class that can be
23// used to convert HTML web pages into PDF documents by using an external module (html2pdf).
24//
25// html2pdf modules can be downloaded from http://www.pdftron.com/pdfnet/downloads.html.
26//
27// Users can convert HTML pages to PDF using the following operations:
28// - Simple one line static method to convert a single web page to PDF.
29// - Convert HTML pages from URL or string, plus optional table of contents, in user defined order.
30// - Optionally configure settings for proxy, images, java script, and more for each HTML page.
31// - Optionally configure the PDF output, including page size, margins, orientation, and more.
32// - Optionally add table of contents, including setting the depth and appearance.
33//---------------------------------------------------------------------------------------
34
35int main(int argc, char * argv[])
36{
37 int ret = 0;
38
39 std::string output_path = "../../TestFiles/Output/html2pdf_example";
40 std::string host = "https://docs.apryse.com";
41 std::string page0 = "/";
42 std::string page1 = "/all-products/";
43 std::string page2 = "/web/faq";
44
45
46 // The first step in every application using PDFNet is to initialize the
47 // library and set the path to common PDF resources. The library is usually
48 // initialized only once, but calling Initialize() multiple times is also fine.
49 PDFNet::Initialize(LicenseKey);
50
51 // For HTML2PDF we need to locate the html2pdf module. If placed with the
52 // PDFNet library, or in the current working directory, it will be loaded
53 // automatically. Otherwise, it must be set manually using HTML2PDF.SetModulePath().
54 HTML2PDF::SetModulePath("../../../Lib");
55 if (!HTML2PDF::IsModuleAvailable())
56 {
57 cout << endl;
58 cout << "Unable to run HTMLPDFTest: Apryse SDK HTML2PDF module not available." << endl;
59 cout << "---------------------------------------------------------------" << endl;
60 cout << "The HTML2PDF module is an optional add-on, available for download" << endl;
61 cout << "at https://www.pdftron.com/. If you have already downloaded this" << endl;
62 cout << "module, ensure that the SDK is able to find the required files" << endl;
63 cout << "using the HTML2PDF::SetModulePath() function." << endl << endl;
64 return 1;
65 }
66
67 //--------------------------------------------------------------------------------
68 // Example 1) Simple conversion of a web page to a PDF doc.
69 try
70 {
71 PDFDoc doc;
72
73 // now convert a web page, sending generated PDF pages to doc
74 HTML2PDF::Convert(doc, host + page0);
75 doc.Save(output_path + "_01.pdf", SDFDoc::e_linearized, NULL);
76 }
77 catch (Common::Exception& e)
78 {
79 cout << e << endl;
80 ret = 1;
81 }
82 catch (...)
83 {
84 cout << "Unknown Exception" << endl;
85 ret = 1;
86 }
87
88 //--------------------------------------------------------------------------------
89 // Example 2) Modify the settings of the generated PDF pages and attach to an
90 // existing PDF document.
91
92 try
93 {
94 // open the existing PDF, and initialize the security handler
95 PDFDoc doc("../../TestFiles/numbered.pdf");
96 doc.InitSecurityHandler();
97
98 // create the HTML2PDF converter object and modify the output of the PDF pages
99 HTML2PDF converter;
100 converter.SetPaperSize(PrinterMode::e_11x17);
101
102 // insert the web page to convert
103 converter.InsertFromURL(host + page0);
104
105 // convert the web page, appending generated PDF pages to doc
106 converter.Convert(doc);
107 doc.Save(output_path + "_02.pdf", SDFDoc::e_linearized, NULL);
108 }
109 catch (Common::Exception& e)
110 {
111 cout << e << endl;
112 ret = 1;
113 }
114 catch (...)
115 {
116 cout << "Unknown Exception" << endl;
117 ret = 1;
118 }
119
120
121 //--------------------------------------------------------------------------------
122 // Example 3) Convert multiple web pages
123 try
124 {
125 // convert page 0 into pdf
126 PDFDoc doc;
127 HTML2PDF converter;
128 UString header("<div style='width:15%;margin-left:0.5cm;text-align:left;font-size:10px;color:#0000FF'><span class='date'></span></div><div style='width:70%;direction:rtl;white-space:nowrap;overflow:hidden;text-overflow:clip;text-align:center;font-size:10px;color:#0000FF'><span>PDFTRON HEADER EXAMPLE</span></div><div style='width:15%;margin-right:0.5cm;text-align:right;font-size:10px;color:#0000FF'><span class='pageNumber'></span> of <span class='totalPages'></span></div>");
129 UString footer("<div style='width:15%;margin-left:0.5cm;text-align:left;font-size:7px;color:#FF00FF'><span class='date'></span></div><div style='width:70%;direction:rtl;white-space:nowrap;overflow:hidden;text-overflow:clip;text-align:center;font-size:7px;color:#FF00FF'><span>PDFTRON FOOTER EXAMPLE</span></div><div style='width:15%;margin-right:0.5cm;text-align:right;font-size:7px;color:#FF00FF'><span class='pageNumber'></span> of <span class='totalPages'></span></div>");
130 converter.SetHeader(header);
131 converter.SetFooter(footer);
132 converter.SetMargins("1cm", "2cm", ".5cm", "1.5cm");
133 HTML2PDF::WebPageSettings settings;
134 settings.SetZoom(0.5);
135 converter.InsertFromURL(host + page0, settings);
136 converter.Convert(doc);
137
138 // convert page 1 with the same settings, appending generated PDF pages to doc
139 converter.InsertFromURL(host + page1, settings);
140 converter.Convert(doc);
141
142 // convert page 2 with different settings, appending generated PDF pages to doc
143 HTML2PDF another_converter;
144 another_converter.SetLandscape(true);
145 HTML2PDF::WebPageSettings another_settings;
146 another_settings.SetPrintBackground(false);
147 another_converter.InsertFromURL(host + page2, another_settings);
148 another_converter.Convert(doc);
149
150 doc.Save(output_path + "_03.pdf", SDFDoc::e_linearized, NULL);
151 }
152 catch (Common::Exception& e)
153 {
154 std::cout << e << endl;
155 ret = 1;
156 }
157 catch (...)
158 {
159 cout << "Unknown Exception" << endl;
160 ret = 1;
161 }
162
163 //--------------------------------------------------------------------------------
164 // Example 4) Convert HTML string to PDF.
165
166 try
167 {
168 PDFDoc doc;
169
170 HTML2PDF converter;
171
172 // Our HTML data
173 UString html("<html><body><h1>Heading</h1><p>Paragraph.</p></body></html>");
174
175 // Add html data
176 converter.InsertFromHtmlString(html);
177 // Note, InsertFromHtmlString can be mixed with the other Insert methods.
178
179 converter.Convert(doc);
180 doc.Save(output_path + "_04.pdf", SDFDoc::e_linearized, NULL);
181 }
182 catch (Common::Exception& e)
183 {
184 std::cout << e << endl;
185 ret = 1;
186 }
187 catch (...)
188 {
189 cout << "Unknown Exception" << endl;
190 ret = 1;
191 }
192
193 //--------------------------------------------------------------------------------
194 // Example 5) Set the location of the log file to be used during conversion.
195 try
196 {
197 PDFDoc doc;
198 HTML2PDF converter;
199 converter.SetLogFilePath("../../TestFiles/Output/html2pdf.log");
200 converter.InsertFromURL(host + page0);
201 converter.Convert(doc);
202 doc.Save(output_path + "_05.pdf", SDFDoc::e_linearized, NULL);
203 }
204 catch (Common::Exception& e)
205 {
206 cout << e << endl;
207 ret = 1;
208 }
209 catch (...)
210 {
211 cout << "Unknown Exception" << endl;
212 ret = 1;
213 }
214
215 PDFNet::Terminate();
216 return ret;
217
218}
1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2021 by PDFTron Systems Inc. All Rights Reserved.
3// Consult LICENSE.txt regarding license information.
4//---------------------------------------------------------------------------------------
5
6package main
7import (
8 "fmt"
9 "strconv"
10 . "pdftron"
11)
12
13import "pdftron/Samples/LicenseKey/GO"
14
15//---------------------------------------------------------------------------------------
16// The following sample illustrates how to convert HTML pages to PDF format using
17// the HTML2PDF class.
18//
19// 'pdftron.PDF.HTML2PDF' is an optional PDFNet Add-On utility class that can be
20// used to convert HTML web pages into PDF documents by using an external module (html2pdf).
21//
22// html2pdf modules can be downloaded from http://www.pdftron.com/pdfnet/downloads.html.
23//
24// Users can convert HTML pages to PDF using the following operations:
25// - Simple one line static method to convert a single web page to PDF.
26// - Convert HTML pages from URL or string, plus optional table of contents, in user defined order.
27// - Optionally configure settings for proxy, images, java script, and more for each HTML page.
28// - Optionally configure the PDF output, including page size, margins, orientation, and more.
29// - Optionally add table of contents, including setting the depth and appearance.
30//---------------------------------------------------------------------------------------
31
32func main(){
33 outputPath := "../../TestFiles/Output/html2pdf_example"
34 host := "https://www.pdftron.com"
35 page0 := "/"
36 page1 := "/support"
37 page2 := "/blog"
38
39 // The first step in every application using PDFNet is to initialize the
40 // library and set the path to common PDF resources. The library is usually
41 // initialized only once, but calling Initialize() multiple times is also fine.
42 PDFNetInitialize(PDFTronLicense.Key)
43
44 // For HTML2PDF we need to locate the html2pdf module. If placed with the
45 // PDFNet library, or in the current working directory, it will be loaded
46 // automatically. Otherwise, it must be set manually using HTML2PDF.SetModulePath.
47 HTML2PDFSetModulePath("../../../PDFNetC/Lib/")
48 if ! HTML2PDFIsModuleAvailable(){
49 fmt.Println("Unable to run HTML2PDFTest: PDFTron SDK HTML2PDF module not available.\n" +
50 "---------------------------------------------------------------\n" +
51 "The HTML2PDF module is an optional add-on, available for download\n" +
52 "at http://www.pdftron.com/. If you have already downloaded this\n" +
53 "module, ensure that the SDK is able to find the required files\n" +
54 "using the HTML2PDF::SetModulePath() function.")
55 return
56 }
57
58 //--------------------------------------------------------------------------------
59 // Example 1) Simple conversion of a web page to a PDF doc.
60
61 doc := NewPDFDoc()
62 // now convert a web page, sending generated PDF pages to doc
63 converter := NewHTML2PDF()
64 converter.InsertFromURL(host + page0)
65 if converter.Convert(doc){
66 doc.Save(outputPath + "_01.pdf", uint(SDFDocE_linearized))
67 }else{
68 fmt.Println("Conversion failed.")
69 }
70
71 //--------------------------------------------------------------------------------
72 // Example 2) Modify the settings of the generated PDF pages and attach to an
73 // existing PDF document.
74
75 // open the existing PDF, and initialize the security handler
76 doc = NewPDFDoc("../../TestFiles/numbered.pdf")
77 doc.InitSecurityHandler()
78
79 // create the HTML2PDF converter object and modify the output of the PDF pages
80 converter = NewHTML2PDF()
81 converter.SetPaperSize(PrinterModeE_11x17)
82
83 // insert the web page to convert
84 converter.InsertFromURL(host + page0)
85
86 // convert the web page, appending generated PDF pages to doc
87 if converter.Convert(doc){
88 doc.Save(outputPath + "_02.pdf", uint(SDFDocE_linearized))
89 }else{
90 fmt.Println("Conversion failed. HTTP Code: " + strconv.Itoa(converter.GetHTTPErrorCode()) + "\n" + converter.GetLog())
91 }
92 //--------------------------------------------------------------------------------
93 // Example 3) Convert multiple web pages, adding a table of contents, and setting
94 // the first page as a cover page, not to be included with the table of contents outline.
95
96 doc = NewPDFDoc()
97 converter = NewHTML2PDF()
98
99 header := "<div style='width:15%;margin-left:0.5cm;text-align:left;font-size:10px;color:#0000FF'><span class='date'></span></div><div style='width:70%;direction:rtl;white-space:nowrap;overflow:hidden;text-overflow:clip;text-align:center;font-size:10px;color:#0000FF'><span>PDFTRON HEADER EXAMPLE</span></div><div style='width:15%;margin-right:0.5cm;text-align:right;font-size:10px;color:#0000FF'><span class='pageNumber'></span> of <span class='totalPages'></span></div>"
100 footer := "<div style='width:15%;margin-left:0.5cm;text-align:left;font-size:7px;color:#FF00FF'><span class='date'></span></div><div style='width:70%;direction:rtl;white-space:nowrap;overflow:hidden;text-overflow:clip;text-align:center;font-size:7px;color:#FF00FF'><span>PDFTRON FOOTER EXAMPLE</span></div><div style='width:15%;margin-right:0.5cm;text-align:right;font-size:7px;color:#FF00FF'><span class='pageNumber'></span> of <span class='totalPages'></span></div>"
101 converter.SetHeader(header)
102 converter.SetFooter(footer)
103 converter.SetMargins("1cm", "2cm", ".5cm", "1.5cm")
104 settings := NewWebPageSettings()
105 settings.SetZoom(0.5)
106 converter.InsertFromURL(host + page0, settings)
107 is_conversion_0_successful := converter.Convert(doc)
108
109 // convert page 1 with the same settings, appending generated PDF pages to doc
110 converter.InsertFromURL(host + page1, settings)
111 is_conversion_1_successful := converter.Convert(doc)
112
113 // convert page 2 with different settings, appending generated PDF pages to doc
114 another_converter := NewHTML2PDF()
115 another_converter.SetLandscape(true)
116 another_settings := NewWebPageSettings()
117 another_settings.SetPrintBackground(false)
118 another_converter.InsertFromURL(host + page2, another_settings)
119 is_conversion_2_successful := another_converter.Convert(doc);
120
121 if(is_conversion_0_successful && is_conversion_1_successful && is_conversion_2_successful){
122 doc.Save(outputPath + "_03.pdf", uint(SDFDocE_linearized))
123 }else{
124 fmt.Println("Conversion failed. HTTP Code: " + strconv.Itoa(converter.GetHTTPErrorCode()) + "\n" + converter.GetLog())
125 }
126
127 //--------------------------------------------------------------------------------
128 // Example 4) Convert HTML string to PDF.
129
130 doc = NewPDFDoc()
131 converter = NewHTML2PDF()
132
133 // Our HTML data
134 html := "<html><body><h1>Heading</h1><p>Paragraph.</p></body></html>"
135
136 // Add html data
137 converter.InsertFromHtmlString(html)
138 // Note, InsertFromHtmlString can be mixed with the other Insert methods.
139
140 if converter.Convert(doc){
141 doc.Save(outputPath + "_04.pdf", uint(SDFDocE_linearized))
142 }else{
143 fmt.Println("Conversion failed. HTTP Code: " + strconv.Itoa(converter.GetHTTPErrorCode()) + "\n" + converter.GetLog())
144 }
145 PDFNetTerminate()
146}
1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2024 by Apryse Software Inc. All Rights Reserved.
3// Consult legal.txt regarding legal and license information.
4//---------------------------------------------------------------------------------------
5
6import com.pdftron.pdf.*;
7import com.pdftron.sdf.*;
8
9public class HTML2PDFTest {
10 //---------------------------------------------------------------------------------------
11 // The following sample illustrates how to convert HTML pages to PDF format using
12 // the HTML2PDF class.
13 //
14 // 'pdftron.PDF.HTML2PDF' is an optional PDFNet Add-On utility class that can be
15 // used to convert HTML web pages into PDF documents by using an external module (html2pdf).
16 //
17 // html2pdf modules can be downloaded from http://www.pdftron.com/pdfnet/downloads.html.
18 //
19 // Users can convert HTML pages to PDF using the following operations:
20 // - Simple one line static method to convert a single web page to PDF.
21 // - Convert HTML pages from URL or string, plus optional table of contents, in user defined order.
22 // - Optionally configure settings for proxy, images, java script, and more for each HTML page.
23 // - Optionally configure the PDF output, including page size, margins, orientation, and more.
24 // - Optionally add table of contents, including setting the depth and appearance.
25 //---------------------------------------------------------------------------------------
26
27 public static void main(String[] args) {
28 String output_path = "../../TestFiles/Output/html2pdf_example";
29 String host = "https://docs.apryse.com";
30 String page0 = "/";
31 String page1 = "/all-products/";
32 String page2 = "/web/faq";
33 // The first step in every application using PDFNet is to initialize the
34 // library and set the path to common PDF resources. The library is usually
35 // initialized only once, but calling initialize() multiple times is also fine.
36 PDFNet.initialize(PDFTronLicense.Key());
37 // For HTML2PDF we need to locate the html2pdf module. If placed with the
38 // PDFNet library, or in the current working directory, it will be loaded
39 // automatically. Otherwise, it must be set manually using HTML2PDF.SetModulePath().
40 try {
41 HTML2PDF.setModulePath("../../../Lib");
42 if(!HTML2PDF.isModuleAvailable())
43 {
44 System.out.println();
45 System.out.println("Unable to run HTML2PDFTest: Apryse SDK HTML2PDF module not available.");
46 System.out.println("---------------------------------------------------------------");
47 System.out.println("The HTML2PDF module is an optional add-on, available for download");
48 System.out.println("at https://www.pdftron.com/. If you have already downloaded this");
49 System.out.println("module, ensure that the SDK is able to find the required files");
50 System.out.println("using the HTML2PDF.setModulePath() function." );
51 System.out.println();
52 return;
53 }
54 } catch (Exception e) {
55 e.printStackTrace();
56 return;
57 }
58
59 //--------------------------------------------------------------------------------
60 // Example 1) Simple conversion of a web page to a PDF doc.
61
62 try (PDFDoc doc = new PDFDoc()) {
63 // now convert a web page, sending generated PDF pages to doc
64 HTML2PDF.convert(doc, host + page0);
65 doc.save(output_path + "_01.pdf", SDFDoc.SaveMode.LINEARIZED, null);
66 } catch (Exception e) {
67 e.printStackTrace();
68 return;
69 }
70
71 //--------------------------------------------------------------------------------
72 // Example 2) Modify the settings of the generated PDF pages and attach to an
73 // existing PDF document.
74
75 try (PDFDoc doc = new PDFDoc("../../TestFiles/numbered.pdf")) {
76 // open the existing PDF, and initialize the security handler
77 doc.initSecurityHandler();
78
79 // create the HTML2PDF converter object and modify the output of the PDF pages
80 HTML2PDF converter = new HTML2PDF();
81 converter.setPaperSize(PrinterMode.e_11x17);
82
83 // insert the web page to convert
84 converter.insertFromURL(host + page0);
85
86 // convert the web page, appending generated PDF pages to doc
87 converter.convert(doc);
88 doc.save(output_path + "_02.pdf", SDFDoc.SaveMode.LINEARIZED, null);
89 } catch (Exception e) {
90 e.printStackTrace();
91 return;
92 }
93
94 //--------------------------------------------------------------------------------
95 // Example 3) Convert multiple web pages
96
97 try (PDFDoc doc = new PDFDoc()) {
98 // convert page 0 into pdf
99
100 HTML2PDF converter = new HTML2PDF();
101
102 String header = "<div style='width:15%;margin-left:0.5cm;text-align:left;font-size:10px;color:#0000FF'><span class='date'></span></div><div style='width:70%;direction:rtl;white-space:nowrap;overflow:hidden;text-overflow:clip;text-align:center;font-size:10px;color:#0000FF'><span>PDFTRON HEADER EXAMPLE</span></div><div style='width:15%;margin-right:0.5cm;text-align:right;font-size:10px;color:#0000FF'><span class='pageNumber'></span> of <span class='totalPages'></span></div>";
103 String footer = "<div style='width:15%;margin-left:0.5cm;text-align:left;font-size:7px;color:#FF00FF'><span class='date'></span></div><div style='width:70%;direction:rtl;white-space:nowrap;overflow:hidden;text-overflow:clip;text-align:center;font-size:7px;color:#FF00FF'><span>PDFTRON FOOTER EXAMPLE</span></div><div style='width:15%;margin-right:0.5cm;text-align:right;font-size:7px;color:#FF00FF'><span class='pageNumber'></span> of <span class='totalPages'></span></div>";
104 converter.setHeader(header);
105 converter.setFooter(footer);
106 converter.setMargins("1cm", "2cm", ".5cm", "1.5cm");
107 HTML2PDF.WebPageSettings settings = new HTML2PDF.WebPageSettings();
108 settings.setZoom(0.5);
109 converter.insertFromURL(host + page0, settings);
110 converter.convert(doc);
111
112 // convert page 1 with the same settings, appending generated PDF pages to doc
113 converter.insertFromURL(host + page1, settings);
114 converter.convert(doc);
115
116 // convert page 2 with different settings, appending generated PDF pages to doc
117 HTML2PDF another_converter = new HTML2PDF();;
118 another_converter.setLandscape(true);
119 HTML2PDF.WebPageSettings another_settings = new HTML2PDF.WebPageSettings();
120 another_settings.setPrintBackground(false);
121 another_converter.insertFromURL(host + page2, another_settings);
122 another_converter.convert(doc);
123
124 doc.save(output_path + "_03.pdf", SDFDoc.SaveMode.LINEARIZED, null);
125 } catch (Exception e) {
126 e.printStackTrace();
127 return;
128 }
129
130 //--------------------------------------------------------------------------------
131 // Example 4) Convert HTML string to PDF.
132
133 try (PDFDoc doc = new PDFDoc()) {
134 HTML2PDF converter = new HTML2PDF();
135
136 // Our HTML data
137 String html = "<html><body><h1>Heading</h1><p>Paragraph.</p></body></html>";
138
139 // Add html data
140 converter.insertFromHtmlString(html);
141 // Note, InsertFromHtmlString can be mixed with the other Insert methods.
142
143 converter.convert(doc);
144 doc.save(output_path + "_04.pdf", SDFDoc.SaveMode.LINEARIZED, null);
145 } catch (Exception e) {
146 e.printStackTrace();
147 return;
148 }
149
150 //--------------------------------------------------------------------------------
151 // Example 5) Set the location of the log file to be used during conversion.
152
153 try (PDFDoc doc = new PDFDoc()) {
154 HTML2PDF converter = new HTML2PDF();
155 converter.setLogFilePath("../../TestFiles/Output/html2pdf.log");
156 converter.insertFromURL(host + page0);
157 converter.convert(doc);
158 doc.save(output_path + "_05.pdf", SDFDoc.SaveMode.LINEARIZED, null);
159 } catch (Exception e) {
160 e.printStackTrace();
161 return;
162 }
163
164 PDFNet.terminate();
165 }
166}
1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2024 by Apryse Software Inc. All Rights Reserved.
3// Consult legal.txt regarding legal and license information.
4//---------------------------------------------------------------------------------------
5
6//---------------------------------------------------------------------------------------
7// The following sample illustrates how to convert HTML pages to PDF format using
8// the HTML2PDF class.
9//
10// 'pdftron.PDF.HTML2PDF' is an optional PDFNet Add-On utility class that can be
11// used to convert HTML web pages into PDF documents by using an external module (html2pdf).
12//
13// html2pdf modules can be downloaded from http://www.pdftron.com/pdfnet/downloads.html.
14//
15// Users can convert HTML pages to PDF using the following operations:
16// - Simple one line static method to convert a single web page to PDF.
17// - Convert HTML pages from URL or string, plus optional table of contents, in user defined order.
18// - Optionally configure settings for proxy, images, java script, and more for each HTML page.
19// - Optionally configure the PDF output, including page size, margins, orientation, and more.
20// - Optionally add table of contents, including setting the depth and appearance.
21//---------------------------------------------------------------------------------------
22
23const { PDFNet } = require('@pdftron/pdfnet-node');
24const PDFTronLicense = require('../LicenseKey/LicenseKey');
25
26((exports) => {
27 'use strict';
28
29 exports.runHTML2PDFTest = () => {
30 const main = async () => {
31 const outputPath = '../TestFiles/Output/html2pdf_example';
32 const host = 'https://docs.apryse.com';
33 const page0 = '/';
34 const page1 = '/all-products/';
35 const page2 = '/web/faq';
36
37 // For HTML2PDF we need to locate the html2pdf module. If placed with the
38 // PDFNet library, or in the current working directory, it will be loaded
39 // automatically. Otherwise, it must be set manually using HTML2PDF.setModulePath.
40 await PDFNet.HTML2PDF.setModulePath('../../lib/');
41
42 if(!(await PDFNet.HTML2PDF.isModuleAvailable())) {
43 console.log('Unable to run HTML2PDFTest: Apryse SDK HTML2PDF module not available.');
44 console.log('---------------------------------------------------------------');
45 console.log('The HTML2PDF module is an optional add-on, available for download');
46 console.log('at https://www.pdftron.com/. If you have already downloaded this');
47 console.log('module, ensure that the SDK is able to find the required files');
48 console.log('using the HTML2PDF.setModulePath() function.');
49
50 return;
51 }
52
53 //--------------------------------------------------------------------------------
54 // Example 1) Simple conversion of a web page to a PDF doc.
55
56 try {
57 const html2pdf = await PDFNet.HTML2PDF.create();
58 const doc = await PDFNet.PDFDoc.create();
59
60 html2pdf.insertFromUrl(host.concat(page0));
61 // now convert a web page, sending generated PDF pages to doc
62 await html2pdf.convert(doc);
63 doc.save(outputPath.concat('_01.pdf'), PDFNet.SDFDoc.SaveOptions.e_linearized);
64 } catch (err) {
65 console.log(err);
66 }
67
68 //--------------------------------------------------------------------------------
69 // Example 2) Modify the settings of the generated PDF pages and attach to an
70 // existing PDF document.
71
72 try {
73 // open the existing PDF, and initialize the security handler
74 const doc = await PDFNet.PDFDoc.createFromFilePath('../TestFiles/numbered.pdf');
75 await doc.initSecurityHandler();
76
77 // create the HTML2PDF converter object and modify the output of the PDF pages
78 const html2pdf = await PDFNet.HTML2PDF.create();
79 html2pdf.setPaperSize(PDFNet.PrinterMode.PaperSize.e_11x17);
80
81 // insert the web page to convert
82 html2pdf.insertFromUrl(host.concat(page0));
83
84 // convert the web page, appending generated PDF pages to doc
85 await html2pdf.convert(doc);
86 doc.save(outputPath.concat('_02.pdf'), PDFNet.SDFDoc.SaveOptions.e_linearized);
87 } catch (err) {
88 console.log(err);
89 }
90
91 //--------------------------------------------------------------------------------
92 // Example 3) Convert multiple web pages
93
94 try {
95 // convert page 0 into pdf
96 const doc = await PDFNet.PDFDoc.create();
97
98 const converter = await PDFNet.HTML2PDF.create();
99
100 const header = '<div style=\'width:15%;margin-left:0.5cm;text-align:left;font-size:10px;color:#0000FF\'><span class=\'date\'></span></div><div style=\'width:70%;direction:rtl;white-space:nowrap;overflow:hidden;text-overflow:clip;text-align:center;font-size:10px;color:#0000FF\'><span>PDFTRON HEADER EXAMPLE</span></div><div style=\'width:15%;margin-right:0.5cm;text-align:right;font-size:10px;color:#0000FF\'><span class=\'pageNumber\'></span> of <span class=\'totalPages\'></span></div>';
101 const footer = '<div style=\'width:15%;margin-left:0.5cm;text-align:left;font-size:7px;color:#FF00FF\'><span class=\'date\'></span></div><div style=\'width:70%;direction:rtl;white-space:nowrap;overflow:hidden;text-overflow:clip;text-align:center;font-size:7px;color:#FF00FF\'><span>PDFTRON FOOTER EXAMPLE</span></div><div style=\'width:15%;margin-right:0.5cm;text-align:right;font-size:7px;color:#FF00FF\'><span class=\'pageNumber\'></span> of <span class=\'totalPages\'></span></div>';
102 converter.setHeader(header);
103 converter.setFooter(footer);
104 converter.setMargins('1cm', '2cm', '.5cm', '1.5cm');
105 const settings = await PDFNet.HTML2PDF.WebPageSettings.create();
106 await settings.setZoom(0.5);
107 converter.insertFromUrl2(host.concat(page0), settings);
108 await converter.convert(doc);
109
110 // convert page 1 with the same settings, appending generated PDF pages to doc
111 converter.insertFromUrl2(host.concat(page1), settings);
112 await converter.convert(doc);
113
114 // convert page 2 with different settings, appending generated PDF pages to doc
115 const another_converter = await PDFNet.HTML2PDF.create();
116 another_converter.setLandscape(true);
117 const another_settings = await PDFNet.HTML2PDF.WebPageSettings.create();
118 another_settings.setPrintBackground(false);
119 another_converter.insertFromUrl2(host.concat(page2), another_settings);
120 await another_converter.convert(doc);
121
122 doc.save(outputPath.concat('_03.pdf'), PDFNet.SDFDoc.SaveOptions.e_linearized);
123 } catch (err) {
124 console.log(err);
125 }
126
127 //--------------------------------------------------------------------------------
128 // Example 4) Convert HTML string to PDF.
129
130 try {
131 const html2pdf = await PDFNet.HTML2PDF.create();
132 const doc = await PDFNet.PDFDoc.create();
133 const html = '<html><body><h1>Heading</h1><p>Paragraph.</p></body></html>';
134
135 html2pdf.insertFromHtmlString(html);
136 await html2pdf.convert(doc);
137 doc.save(outputPath.concat('_04.pdf'), PDFNet.SDFDoc.SaveOptions.e_linearized);
138 } catch (err) {
139 console.log(err);
140 }
141
142 //--------------------------------------------------------------------------------
143 // Example 5) Set the location of the log file to be used during conversion.
144
145 try {
146 const html2pdf = await PDFNet.HTML2PDF.create();
147 const doc = await PDFNet.PDFDoc.create();
148 html2pdf.setLogFilePath('../TestFiles/Output/html2pdf.log');
149 html2pdf.insertFromUrl(host.concat(page0));
150 await html2pdf.convert(doc);
151 doc.save(outputPath.concat('_05.pdf'), PDFNet.SDFDoc.SaveOptions.e_linearized);
152 } catch (err) {
153 console.log(err);
154 }
155
156 console.log('Test Complete!');
157 }
158 PDFNet.runWithCleanup(main, PDFTronLicense.Key).catch(function(error) {
159 console.log('Error: ' + JSON.stringify(error));
160 }).then(function(){ return PDFNet.shutdown(); });
161 };
162 exports.runHTML2PDFTest();
163})(exports);
164// eslint-disable-next-line spaced-comment
165//# sourceURL=HTML2PDFTest.js
1<?php
2//---------------------------------------------------------------------------------------
3// Copyright (c) 2001-2023 by Apryse Software Inc. All Rights Reserved.
4// Consult LICENSE.txt regarding license information.
5//---------------------------------------------------------------------------------------
6if(file_exists("../../../PDFNetC/Lib/PDFNetPHP.php"))
7include("../../../PDFNetC/Lib/PDFNetPHP.php");
8include("../../LicenseKey/PHP/LicenseKey.php");
9
10// Relative path to the folder containing the test files.
11$input_path = getcwd()."/../../TestFiles/";
12$output_path = $input_path."Output/";
13
14//---------------------------------------------------------------------------------------
15// The following sample illustrates how to convert HTML pages to PDF format using
16// the HTML2PDF class.
17//
18// 'pdftron.PDF.HTML2PDF' is an optional PDFNet Add-On utility class that can be
19// used to convert HTML web pages into PDF documents by using an external module (html2pdf).
20//
21// html2pdf modules can be downloaded from https://dev.apryse.com/.
22//
23// Users can convert HTML pages to PDF using the following operations:
24// - Simple one line static method to convert a single web page to PDF.
25// - Convert HTML pages from URL or string, plus optional table of contents, in user defined order.
26// - Optionally configure settings for proxy, images, java script, and more for each HTML page.
27// - Optionally configure the PDF output, including page size, margins, orientation, and more.
28// - Optionally add table of contents, including setting the depth and appearance.
29//---------------------------------------------------------------------------------------
30
31 $output_path = "../../TestFiles/Output/html2pdf_example";
32 $host = "https://docs.apryse.com";
33 $page0 = "/";
34 $page1 = "/all-products/";
35 $page2 = "/web/faq";
36
37 // The first step in every application using PDFNet is to initialize the
38 // library and set the path to common PDF resources. The library is usually
39 // initialized only once, but calling Initialize() multiple times is also fine.
40 PDFNet::Initialize($LicenseKey);
41 PDFNet::GetSystemFontList(); // Wait for fonts to be loaded if they haven't already. This is done because PHP can run into errors when shutting down if font loading is still in progress.
42
43 // For HTML2PDF we need to locate the html2pdf module. If placed with the
44 // PDFNet library, or in the current working directory, it will be loaded
45 // automatically. Otherwise, it must be set manually using HTML2PDF.SetModulePath.
46 HTML2PDF::SetModulePath("./../../../PDFNetC/Lib");
47 if(!HTML2PDF::IsModuleAvailable()) {
48 echo "Unable to run HTML2PDFTest: PDFTron SDK HTML2PDF module not available.\n
49 ---------------------------------------------------------------\n
50 The HTML2PDF module is an optional add-on, available for download\n
51 at https://www.pdftron.com/. If you have already downloaded this\n
52 module, ensure that the SDK is able to find the required files\n
53 using the HTML2PDF::SetModulePath() function.\n";
54 return;
55 }
56
57 //--------------------------------------------------------------------------------
58 // Example 1) Simple conversion of a web page to a PDF doc.
59
60 $doc = new PDFDoc();
61
62 // now convert a web page, sending generated PDF pages to doc
63 $converter = new HTML2PDF();
64 $converter->InsertFromURL($host.$page0);
65 $converter->Convert($doc);
66 $doc->Save($output_path."_01.pdf", SDFDoc::e_linearized);
67 $doc->Close();
68
69 //--------------------------------------------------------------------------------
70 // Example 2) Modify the settings of the generated PDF pages and attach to an
71 // existing PDF document.
72
73 // open the existing PDF, and initialize the security handler
74 $doc = new PDFDoc("../../TestFiles/numbered.pdf");
75 $doc->InitSecurityHandler();
76
77 // create the HTML2PDF converter object and modify the output of the PDF pages
78 $converter = new HTML2PDF();
79 $converter->SetPaperSize(PrinterMode::e_11x17);
80
81 // insert the web page to convert
82 $converter->InsertFromURL($host.$page0);
83
84 // convert the web page, appending generated PDF pages to doc
85 $converter->Convert($doc);
86 $doc->Save($output_path."_02.pdf", SDFDoc::e_linearized);
87 $doc->Close();
88
89 //--------------------------------------------------------------------------------
90 // Example 3) Convert multiple web pages, adding a table of contents, and setting
91 // the first page as a cover page, not to be included with the table of contents outline.
92
93 $doc = new PDFDoc();
94
95 $converter = new HTML2PDF();
96 $header = "<div style='width:15%;margin-left:0.5cm;text-align:left;font-size:10px;color:#0000FF'><span class='date'></span></div><div style='width:70%;direction:rtl;white-space:nowrap;overflow:hidden;text-overflow:clip;text-align:center;font-size:10px;color:#0000FF'><span>PDFTRON HEADER EXAMPLE</span></div><div style='width:15%;margin-right:0.5cm;text-align:right;font-size:10px;color:#0000FF'><span class='pageNumber'></span> of <span class='totalPages'></span></div>";
97 $footer = "<div style='width:15%;margin-left:0.5cm;text-align:left;font-size:7px;color:#FF00FF'><span class='date'></span></div><div style='width:70%;direction:rtl;white-space:nowrap;overflow:hidden;text-overflow:clip;text-align:center;font-size:7px;color:#FF00FF'><span>PDFTRON FOOTER EXAMPLE</span></div><div style='width:15%;margin-right:0.5cm;text-align:right;font-size:7px;color:#FF00FF'><span class='pageNumber'></span> of <span class='totalPages'></span></div>";
98 $converter->SetHeader($header);
99 $converter->SetFooter($footer);
100 $converter->SetMargins("1cm", "2cm", ".5cm", "1.5cm");
101
102 $settings = new WebPageSettings();
103 $settings->SetZoom(0.5);
104 $converter->InsertFromURL($host.$page0, $settings);
105 $converter->Convert($doc);
106
107 //convert page 1 with the same settings, appending generated PDF pages to doc
108 $converter->InsertFromURL($host.$page1, $settings);
109 $converter->Convert($doc);
110
111 //convert page 2 with different settings, appending generated PDF pages to doc
112 $another_converter = new HTML2PDF();
113 $another_converter->SetLandscape(True);
114 $another_settings = new WebPageSettings();
115 $another_settings->SetPrintBackground(False);
116 $another_converter->InsertFromURL($host.$page2, $another_settings);
117 $another_converter->Convert($doc);
118
119 $doc->Save($output_path."_03.pdf", SDFDoc::e_linearized);
120 $doc->Close();
121
122 //--------------------------------------------------------------------------------
123 // Example 4) Convert HTML string to PDF.
124
125 $doc = new PDFDoc();
126
127 $converter = new HTML2PDF();
128
129 // Our HTML data
130 $html = "<html><body><h1>Heading</h1><p>Paragraph.</p></body></html>";
131
132 // Add html data
133 $converter->InsertFromHtmlString($html);
134 // Note, InsertFromHtmlString can be mixed with the other Insert methods.
135
136 $converter->Convert($doc);
137 $doc->Save($output_path."_04.pdf", SDFDoc::e_linearized);
138 $doc->Close();
139
140 //--------------------------------------------------------------------------------
141 // Example 5) Set the location of the log file to be used during conversion.
142
143 $doc = new PDFDoc();
144
145 // now convert a web page, sending generated PDF pages to doc
146 $converter = new HTML2PDF();
147 $converter->SetLogFilePath("../../TestFiles/Output/html2pdf.log");
148 $converter->InsertFromURL($host.$page0);
149 $converter->Convert($doc);
150 $doc->Save($output_path."_05.pdf", SDFDoc::e_linearized);
151 $doc->Close();
152
153 PDFNet::Terminate();
154?>
1#---------------------------------------------------------------------------------------
2# Copyright (c) 2001-2023 by Apryse Software Inc. All Rights Reserved.
3# Consult LICENSE.txt regarding license information.
4#---------------------------------------------------------------------------------------
5
6import site
7site.addsitedir("../../../PDFNetC/Lib")
8import sys
9from PDFNetPython import *
10
11sys.path.append("../../LicenseKey/PYTHON")
12from LicenseKey import *
13
14#---------------------------------------------------------------------------------------
15# The following sample illustrates how to convert HTML pages to PDF format using
16# the HTML2PDF class.
17#
18# 'pdftron.PDF.HTML2PDF' is an optional PDFNet Add-On utility class that can be
19# used to convert HTML web pages into PDF documents by using an external module (html2pdf).
20#
21# html2pdf modules can be downloaded from https://dev.apryse.com/.
22#
23# Users can convert HTML pages to PDF using the following operations:
24# - Simple one line static method to convert a single web page to PDF.
25# - Convert HTML pages from URL or string, plus optional table of contents, in user defined order.
26# - Optionally configure settings for proxy, images, java script, and more for each HTML page.
27# - Optionally configure the PDF output, including page size, margins, orientation, and more.
28# - Optionally add table of contents, including setting the depth and appearance.
29#---------------------------------------------------------------------------------------
30
31def main():
32 output_path = "../../TestFiles/Output/html2pdf_example"
33 host = "https://docs.apryse.com"
34 page0 = "/"
35 page1 = "/all-products/"
36 page2 = "/web/faq"
37
38 # The first step in every application using PDFNet is to initialize the
39 # library and set the path to common PDF resources. The library is usually
40 # initialized only once, but calling Initialize() multiple times is also fine.
41 PDFNet.Initialize(LicenseKey)
42
43 # For HTML2PDF we need to locate the html2pdf module. If placed with the
44 # PDFNet library, or in the current working directory, it will be loaded
45 # automatically. Otherwise, it must be set manually using HTML2PDF.SetModulePath.
46 HTML2PDF.SetModulePath("../../../PDFNetC/Lib/")
47 if not HTML2PDF.IsModuleAvailable():
48 print("""
49 Unable to run HTML2PDFTest: PDFTron SDK HTML2PDF module not available.
50 ---------------------------------------------------------------
51 The HTML2PDF module is an optional add-on, available for download
52 at https://www.pdftron.com/. If you have already downloaded this
53 module, ensure that the SDK is able to find the required files
54 using the HTML2PDF.SetModulePath() function.""")
55 return
56
57 #--------------------------------------------------------------------------------
58 # Example 1) Simple conversion of a web page to a PDF doc.
59
60 doc = PDFDoc()
61 # now convert a web page, sending generated PDF pages to doc
62 converter = HTML2PDF()
63 converter.InsertFromURL(host + page0)
64 converter.Convert(doc)
65 doc.Save(output_path + "_01.pdf", SDFDoc.e_linearized)
66
67 #--------------------------------------------------------------------------------
68 # Example 2) Modify the settings of the generated PDF pages and attach to an
69 # existing PDF document.
70
71 # open the existing PDF, and initialize the security handler
72 doc = PDFDoc("../../TestFiles/numbered.pdf")
73 doc.InitSecurityHandler()
74
75 # create the HTML2PDF converter object and modify the output of the PDF pages
76 converter = HTML2PDF()
77 converter.SetPaperSize(PrinterMode.e_11x17)
78
79 # insert the web page to convert
80 converter.InsertFromURL(host + page0)
81
82 # convert the web page, appending generated PDF pages to doc
83 converter.Convert(doc)
84 doc.Save(output_path + "_02.pdf", SDFDoc.e_linearized)
85 #--------------------------------------------------------------------------------
86 # Example 3) Convert multiple web pages
87
88 doc = PDFDoc()
89 converter = HTML2PDF()
90
91 header = "<div style='width:15%;margin-left:0.5cm;text-align:left;font-size:10px;color:#0000FF'><span class='date'></span></div><div style='width:70%;direction:rtl;white-space:nowrap;overflow:hidden;text-overflow:clip;text-align:center;font-size:10px;color:#0000FF'><span>PDFTRON HEADER EXAMPLE</span></div><div style='width:15%;margin-right:0.5cm;text-align:right;font-size:10px;color:#0000FF'><span class='pageNumber'></span> of <span class='totalPages'></span></div>"
92 footer = "<div style='width:15%;margin-left:0.5cm;text-align:left;font-size:7px;color:#FF00FF'><span class='date'></span></div><div style='width:70%;direction:rtl;white-space:nowrap;overflow:hidden;text-overflow:clip;text-align:center;font-size:7px;color:#FF00FF'><span>PDFTRON FOOTER EXAMPLE</span></div><div style='width:15%;margin-right:0.5cm;text-align:right;font-size:7px;color:#FF00FF'><span class='pageNumber'></span> of <span class='totalPages'></span></div>"
93 converter.SetHeader(header)
94 converter.SetFooter(footer)
95 converter.SetMargins("1cm", "2cm", ".5cm", "1.5cm")
96
97 settings = WebPageSettings()
98 settings.SetZoom(0.5)
99 converter.InsertFromURL(host + page0)
100 converter.Convert(doc)
101
102 # convert page 1 with the same settings, appending generated PDF pages to doc
103 converter.InsertFromURL(host + page1, settings)
104 converter.Convert(doc)
105
106 # convert page 2 with different settings, appending generated PDF pages to doc
107 another_converter = HTML2PDF()
108 another_converter.SetLandscape(True)
109 another_settings = WebPageSettings()
110 another_settings.SetPrintBackground(False)
111 another_converter.InsertFromURL(host + page2, another_settings)
112 another_converter.Convert(doc)
113
114 doc.Save(output_path + "_03.pdf", SDFDoc.e_linearized)
115
116 #--------------------------------------------------------------------------------
117 # Example 4) Convert HTML string to PDF.
118
119 doc = PDFDoc()
120 converter = HTML2PDF()
121
122 # Our HTML data
123 html = "<html><body><h1>Heading</h1><p>Paragraph.</p></body></html>"
124
125 # Add html data
126 converter.InsertFromHtmlString(html)
127 # Note, InsertFromHtmlString can be mixed with the other Insert methods.
128
129 converter.Convert(doc)
130 doc.Save(output_path + "_04.pdf", SDFDoc.e_linearized)
131
132 #--------------------------------------------------------------------------------
133 # Example 5) Set the location of the log file to be used during conversion.
134
135 doc = PDFDoc()
136 # now convert a web page, sending generated PDF pages to doc
137 converter = HTML2PDF()
138 converter.SetLogFilePath("../../TestFiles/Output/html2pdf.log")
139 converter.InsertFromURL(host + page0)
140 converter.Convert(doc)
141 doc.Save(output_path + "_05.pdf", SDFDoc.e_linearized)
142
143 PDFNet.Terminate()
144
145if __name__ == '__main__':
146 main()
1#---------------------------------------------------------------------------------------
2# Copyright (c) 2001-2023 by Apryse Software Inc. All Rights Reserved.
3# Consult LICENSE.txt regarding license information.
4#---------------------------------------------------------------------------------------
5
6require '../../../PDFNetC/Lib/PDFNetRuby'
7include PDFNetRuby
8require '../../LicenseKey/RUBY/LicenseKey'
9
10$stdout.sync = true
11
12#---------------------------------------------------------------------------------------
13# The following sample illustrates how to convert HTML pages to PDF format using
14# the HTML2PDF class.
15#
16# 'pdftron.PDF.HTML2PDF' is an optional PDFNet Add-On utility class that can be
17# used to convert HTML web pages into PDF documents by using an external module (html2pdf).
18#
19# html2pdf modules can be downloaded from http:#www.pdftron.com/pdfnet/downloads.html.
20#
21# Users can convert HTML pages to PDF using the following operations:
22# - Simple one line static method to convert a single web page to PDF.
23# - Convert HTML pages from URL or string, plus optional table of contents, in user defined order.
24# - Optionally configure settings for proxy, images, java script, and more for each HTML page.
25# - Optionally configure the PDF output, including page size, margins, orientation, and more.
26# - Optionally add table of contents, including setting the depth and appearance.
27#---------------------------------------------------------------------------------------
28
29 output_path = "../../TestFiles/Output/html2pdf_example"
30 host = "https://docs.apryse.com"
31 page0 = "/"
32 page1 = "/all-products/"
33 page2 = "/web/faq"
34
35 # The first step in every application using PDFNet is to initialize the
36 # library and set the path to common PDF resources. The library is usually
37 # initialized only once, but calling Initialize() multiple times is also fine.
38 PDFNet.Initialize(PDFTronLicense.Key)
39
40 # For HTML2PDF we need to locate the html2pdf module. If placed with the
41 # PDFNet library, or in the current working directory, it will be loaded
42 # automatically. Otherwise, it must be set manually using HTML2PDF.SetModulePath.
43 HTML2PDF.SetModulePath("../../../PDFNetC/Lib/");
44 if !HTML2PDF.IsModuleAvailable
45 puts 'Unable to run HTML2PDFTest: PDFTron SDK HTML2PDF module not available.'
46 puts '---------------------------------------------------------------'
47 puts 'The HTML2PDF module is an optional add-on, available for download'
48 puts 'at https://dev.apryse.com/. If you have already downloaded this'
49 puts 'module, ensure that the SDK is able to find the required files'
50 puts 'using the HTML2PDF.SetModulePath function.'
51 return
52 end
53
54 #--------------------------------------------------------------------------------
55 # Example 1) Simple conversion of a web page to a PDF doc.
56
57 doc = PDFDoc.new()
58 # now convert a web page, sending generated PDF pages to doc
59 converter = HTML2PDF.new()
60 converter.InsertFromURL(host + page0)
61 converter.Convert(doc)
62 doc.Save(output_path + "_01.pdf", SDFDoc::E_linearized)
63
64 #--------------------------------------------------------------------------------
65 # Example 2) Modify the settings of the generated PDF pages and attach to an
66 # existing PDF document.
67
68 # open the existing PDF, and initialize the security handler
69 doc = PDFDoc.new("../../TestFiles/numbered.pdf")
70 doc.InitSecurityHandler()
71
72 # create the HTML2PDF converter object and modify the output of the PDF pages
73 converter = HTML2PDF.new()
74 converter.SetPaperSize(PrinterMode::E_11x17)
75
76 # insert the web page to convert
77 converter.InsertFromURL(host + page0)
78
79 # convert the web page, appending generated PDF pages to doc
80 converter.Convert(doc)
81 doc.Save(output_path + "_02.pdf", SDFDoc::E_linearized)
82
83 #--------------------------------------------------------------------------------
84 # Example 3) Convert multiple web pages
85
86 doc = PDFDoc.new()
87 converter = HTML2PDF.new()
88
89 header = "<div style='width:15%;margin-left:0.5cm;text-align:left;font-size:10px;color:#0000FF'><span class='date'></span></div><div style='width:70%;direction:rtl;white-space:nowrap;overflow:hidden;text-overflow:clip;text-align:center;font-size:10px;color:#0000FF'><span>PDFTRON HEADER EXAMPLE</span></div><div style='width:15%;margin-right:0.5cm;text-align:right;font-size:10px;color:#0000FF'><span class='pageNumber'></span> of <span class='totalPages'></span></div>"
90 footer = "<div style='width:15%;margin-left:0.5cm;text-align:left;font-size:7px;color:#FF00FF'><span class='date'></span></div><div style='width:70%;direction:rtl;white-space:nowrap;overflow:hidden;text-overflow:clip;text-align:center;font-size:7px;color:#FF00FF'><span>PDFTRON FOOTER EXAMPLE</span></div><div style='width:15%;margin-right:0.5cm;text-align:right;font-size:7px;color:#FF00FF'><span class='pageNumber'></span> of <span class='totalPages'></span></div>"
91 converter.SetHeader(header)
92 converter.SetFooter(footer)
93 converter.SetMargins("1cm", "2cm", ".5cm", "1.5cm")
94
95 settings = WebPageSettings.new()
96 settings.SetZoom(0.5)
97 converter.InsertFromURL(host + page0, settings)
98 converter.Convert(doc)
99
100 # convert page 1 with the same settings, appending generated PDF pages to doc
101 converter.InsertFromURL(host + page1, settings)
102 converter.Convert(doc)
103
104 # convert page 2 with different settings, appending generated PDF pages to doc
105 another_converter = HTML2PDF.new()
106 another_converter.SetLandscape(true)
107 another_settings = WebPageSettings.new()
108 another_settings.SetPrintBackground(false)
109 another_converter.InsertFromURL(host + page2, another_settings)
110 another_converter.Convert(doc)
111
112 doc.Save(output_path + "_03.pdf", SDFDoc::E_linearized)
113
114 #--------------------------------------------------------------------------------
115 # Example 4) Convert HTML string to PDF.
116
117 doc = PDFDoc.new()
118 converter = HTML2PDF.new()
119
120 # Our HTML data
121 html = "<html><body><h1>Heading</h1><p>Paragraph.</p></body></html>"
122
123 # Add html data
124 converter.InsertFromHtmlString(html)
125 # Note, InsertFromHtmlString can be mixed with the other Insert methods.
126
127 converter.Convert(doc)
128 doc.Save(output_path + "_04.pdf", SDFDoc::E_linearized)
129
130 #--------------------------------------------------------------------------------
131 # Example 5) Set the location of the log file to be used during conversion.
132
133 doc = PDFDoc.new()
134 converter = HTML2PDF.new()
135
136 # specify the log file name
137 converter.SetLogFilePath('../../TestFiles/Output/html2pdf.log')
138
139 # insert the web page to convert
140 converter.InsertFromURL(host + page0)
141
142 # convert the web page
143 converter.Convert(doc)
144 doc.Save(output_path + "_05.pdf", SDFDoc::E_linearized)
145
146 PDFNet.Terminate
1'
2' Copyright (c) 2001-2024 by Apryse Software Inc. All Rights Reserved.
3'
4
5Imports System
6
7Imports pdftron
8Imports pdftron.Common
9Imports pdftron.Filters
10Imports pdftron.SDF
11Imports pdftron.PDF
12'---------------------------------------------------------------------------------------
13' The following sample illustrates how to convert HTML pages to PDF format using
14' the HTML2PDF class.
15'
16' 'pdftron.PDF.HTML2PDF' is an optional PDFNet Add-On utility class that can be
17' used to convert HTML web pages into PDF documents by using an external module (html2pdf).
18'
19' html2pdf modules can be downloaded from http://www.pdftron.com/pdfnet/downloads.html.
20'
21' Users can convert HTML pages to PDF using the following operations:
22' - Simple one line static method to convert a single web page to PDF.
23' - Convert HTML pages from URL or string, plus optional table of contents, in user defined order.
24' - Optionally configure settings for proxy, images, java script, and more for each HTML page.
25' - Optionally configure the PDF output, including page size, margins, orientation, and more.
26' - Optionally add table of contents, including setting the depth and appearance.
27'---------------------------------------------------------------------------------------
28Module HTML2PDFTestVB
29 Dim pdfNetLoader As PDFNetLoader
30 Sub New()
31 pdfNetLoader = pdftron.PDFNetLoader.Instance()
32 End Sub
33
34 Sub Main()
35 Dim output_path As String = "../../../../TestFiles/Output/html2pdf_example"
36 Dim host As String = "https://docs.apryse.com"
37 Dim page0 As String = "/"
38 Dim page1 As String = "/all-products/"
39 Dim page2 As String = "/web/faq"
40 ' The first step in every application using PDFNet is to initialize the
41 ' library and set the path to common PDF resources. The library is usually
42 ' initialized only once, but calling Initialize() multiple times is also fine.
43
44 PDFNet.Initialize(PDFTronLicense.Key)
45
46 ' For HTML2PDF we need to locate the html2pdf module. If placed with the
47 ' PDFNet library, or in the current working directory, it will be loaded
48 ' automatically. Otherwise, it must be set manually using HTML2PDF.SetModulePath().
49 HTML2PDF.SetModulePath("../../../../../Lib")
50
51 If Not HTML2PDF.IsModuleAvailable() Then
52 Console.WriteLine()
53 Console.WriteLine("Unable to run HTML2PDFTest: Apryse SDK CAD module not available.")
54 Console.WriteLine("---------------------------------------------------------------")
55 Console.WriteLine("The HTML2PDF module is an optional add-on, available for download")
56 Console.WriteLine("at http://www.pdftron.com/. If you have already downloaded this")
57 Console.WriteLine("module, ensure that the SDK is able to find the required files")
58 Console.WriteLine("using the HTML2PDF.SetModulePath() function.")
59 Console.WriteLine()
60 End If
61 '--------------------------------------------------------------------------------
62 ' Example 1) Simple conversion of a web page to a PDF doc.
63
64 Try
65 Dim doc As PDFDoc = New PDFDoc()
66 HTML2PDF.Convert(doc, host & page0)
67 doc.Save(output_path + "_01.pdf", SDFDoc.SaveOptions.e_linearized)
68 Catch ex As PDFNetException
69 Console.WriteLine(ex.Message)
70 Catch ex As Exception
71 MsgBox(ex.Message)
72 End Try
73
74 '--------------------------------------------------------------------------------
75 ' Example 2) Modify the settings of the generated PDF pages and attach to an
76 ' existing PDF document.
77
78 Try
79 ' open the existing PDF, and initialize the security handler
80 Dim doc As PDFDoc = New PDFDoc("../../../../TestFiles/numbered.pdf")
81 doc.InitSecurityHandler()
82
83 ' create the HTML2PDF converter object and modify the output of the PDF pages
84 Dim converter As HTML2PDF = New HTML2PDF()
85 converter.SetPaperSize(PrinterMode.PaperSize.e_11x17)
86
87 ' insert the web page to convert
88 converter.InsertFromURL(host & page0)
89
90 ' convert the web page, appending generated PDF pages to doc
91 converter.Convert(doc)
92 doc.Save(output_path + "_02.pdf", SDFDoc.SaveOptions.e_linearized)
93 Catch ex As PDFNetException
94 Console.WriteLine(ex.Message)
95 Catch ex As Exception
96 MsgBox(ex.Message)
97 End Try
98
99 '--------------------------------------------------------------------------------
100 ' Example 3) Convert multiple web pages
101
102 Try
103 Dim doc As PDFDoc = New PDFDoc()
104 ' convert page 0 into pdf
105 Dim converter As HTML2PDF = New HTML2PDF()
106 Dim header As String = "<div style='width:15%;margin-left:0.5cm;text-align:left;font-size:10px;color:#0000FF'><span class='date'></span></div><div style='width:70%;direction:rtl;white-space:nowrap;overflow:hidden;text-overflow:clip;text-align:center;font-size:10px;color:#0000FF'><span>PDFTRON HEADER EXAMPLE</span></div><div style='width:15%;margin-right:0.5cm;text-align:right;font-size:10px;color:#0000FF'><span class='pageNumber'></span> of <span class='totalPages'></span></div>"
107 Dim footer As String = "<div style='width:15%;margin-left:0.5cm;text-align:left;font-size:7px;color:#FF00FF'><span class='date'></span></div><div style='width:70%;direction:rtl;white-space:nowrap;overflow:hidden;text-overflow:clip;text-align:center;font-size:7px;color:#FF00FF'><span>PDFTRON FOOTER EXAMPLE</span></div><div style='width:15%;margin-right:0.5cm;text-align:right;font-size:7px;color:#FF00FF'><span class='pageNumber'></span> of <span class='totalPages'></span></div>"
108 converter.SetHeader(header)
109 converter.SetFooter(footer)
110 converter.SetMargins("1cm", "2cm", ".5cm", "1.5cm")
111 Dim settings As HTML2PDF.WebPageSettings = New HTML2PDF.WebPageSettings()
112 settings.SetZoom(0.5)
113 converter.InsertFromURL(host & page0, settings)
114 converter.Convert(doc)
115
116 ' convert page 1 with the same settings, appending generated PDF pages to doc
117 converter.InsertFromURL(host & page1, settings)
118 converter.Convert(doc)
119
120 ' convert page 2 with different settings, appending generated PDF pages to doc
121 Dim another_converter As HTML2PDF = New HTML2PDF()
122 another_converter.SetLandscape(True)
123 Dim another_settings As HTML2PDF.WebPageSettings = New HTML2PDF.WebPageSettings()
124 another_settings.SetPrintBackground(False)
125 another_converter.InsertFromURL(host & page2, another_settings)
126 another_converter.Convert(doc)
127
128 doc.Save(output_path + "_03.pdf", SDFDoc.SaveOptions.e_linearized)
129 Catch ex As PDFNetException
130 Console.WriteLine(ex.Message)
131 Catch ex As Exception
132 MsgBox(ex.Message)
133 End Try
134
135 '--------------------------------------------------------------------------------
136 ' Example 4) Convert HTML string to PDF.
137
138 Try
139 Dim doc As PDFDoc = New PDFDoc()
140
141 Dim converter As HTML2PDF = New HTML2PDF()
142
143 ' Our HTML data
144 Dim html As String = "<html><body><h1>Heading</h1><p>Paragraph.</p></body></html>"
145
146 ' Add html data
147 converter.InsertFromHtmlString(html)
148 ' Note, InsertFromHtmlString can be mixed with the other Insert methods.
149
150 converter.Convert(doc)
151 doc.Save(output_path + "_04.pdf", SDFDoc.SaveOptions.e_linearized)
152 Catch ex As PDFNetException
153 Console.WriteLine(ex.Message)
154 Catch ex As Exception
155 MsgBox(ex.Message)
156 End Try
157
158 '--------------------------------------------------------------------------------
159 ' Example 5) Set the location of the log file to be used during conversion.
160
161 Try
162 Dim doc As PDFDoc = New PDFDoc()
163 Dim converter As HTML2PDF = New HTML2PDF()
164 converter.SetLogFilePath("../../../../TestFiles/Output/html2pdf.log")
165 converter.InsertFromURL(host & page0)
166 converter.Convert(doc)
167 doc.Save(output_path + "_05.pdf", SDFDoc.SaveOptions.e_linearized)
168 Catch ex As PDFNetException
169 Console.WriteLine(ex.Message)
170 Catch ex As Exception
171 MsgBox(ex.Message)
172 End Try
173
174 PDFNet.Terminate()
175 End Sub
176
177End Module
Did you find this helpful?
Trial setup questions?
Ask experts on DiscordNeed other help?
Contact SupportPricing or product questions?
Contact Sales