HTML2PDF - HTML to PDF Conversion - C++ Sample Code

Sample code for using Apryse SDK to directly convert HTML pages to PDF by using 'pdftron.PDF.HTML2PDF', provided in Python, C++, C#, Java, Node.js (JavaScript), PHP, Ruby, Go and VB. The HTML2PDF converter supports conversion from a string or URL and offers many options to control page size and formatting.

To use this code, you'll need to

  1. Download and get started with Server SDK
  2. Install the HTML2PDF Module

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
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 https://docs.apryse.com/core/guides/info/modules#html2pdf-module.
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://docs.apryse.com/core/guides/info/modules. 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}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales