HTML2PDF

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}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales