HTML2PDF

Sample Obj-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 iOS 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#import <OBJC/PDFNetOBJC.h>
7#import <Foundation/Foundation.h>
8
9//---------------------------------------------------------------------------------------
10// The following sample illustrates how to convert HTML pages to PDF format using
11// the PTHTML2PDF class.
12//
13// 'pdftron.PDF.PTHTML2PDF' is an optional PDFNet Add-On utility class that can be
14// used to convert HTML web pages into PDF documents by using an external module (html2pdf).
15//
16// html2pdf modules can be downloaded from http://www.pdftron.com/pdfnet/downloads.html.
17//
18// Users can convert HTML pages to PDF using the following operations:
19// - Simple one line static method to convert a single web page to PDF.
20// - Convert HTML pages from URL or string, plus optional table of contents, in user defined order.
21// - Optionally configure settings for proxy, images, java script, and more for each HTML page.
22// - Optionally configure the PDF output, including page size, margins, orientation, and more.
23// - Optionally add table of contents, including setting the depth and appearance.
24//---------------------------------------------------------------------------------------
25
26int main(int argc, char * argv[])
27{
28 @autoreleasepool {
29
30 int ret = 0;
31
32 // The first step in every application using PDFNet is to initialize the
33 // library and set the path to common PDF resources. The library is usually
34 // initialized only once, but calling Initialize() multiple times is also fine.
35 [PTPDFNet Initialize: @"YOUR_PDFTRON_LICENSE_KEY"];
36
37 // For PTHTML2PDF we need to locate the PTHTML2PDF 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 PTHTML2PDF.SetModulePath.
40 [PTHTML2PDF SetModulePath: @"../../../Lib"];
41 if( ![PTHTML2PDF IsModuleAvailable] )
42 {
43 NSLog(@"\n");
44 NSLog(@"Unable to run HTMLPDFTest: Apryse SDK HTML2PDF module not available.");
45 NSLog(@"---------------------------------------------------------------");
46 NSLog(@"The HTML2PDF module is an optional add-on, available for download");
47 NSLog(@"at https://www.pdftron.com/. If you have already downloaded this");
48 NSLog(@"module, ensure that the SDK is able to find the required files");
49 NSLog(@"using the HTML2PDF::SetModulePath() function.");
50 return 1;
51 }
52
53 //--------------------------------------------------------------------------------
54 // Example 1) Simple conversion of a web page to a PDF doc.
55
56 @try
57 {
58 PTPDFDoc *doc = [[PTPDFDoc alloc] init];
59 PTHTML2PDF *converter = [[PTHTML2PDF alloc] init];
60 [converter InsertFromURL: @"https://docs.apryse.com/"];
61
62 // now convert a web page, sending generated PDF pages to doc
63 [converter Convert: doc];
64 [doc SaveToFile: @"../../TestFiles/Output/html2pdf_example_01.pdf" flags: e_ptlinearized];
65 }
66 @catch (NSException *e)
67 {
68 NSLog(@"%@", e.reason);
69 ret = 1;
70 }
71
72 //--------------------------------------------------------------------------------
73 // Example 2) Modify the settings of the generated PDF pages and attach to an
74 // existing PDF document.
75
76 @try
77 {
78 // open the existing PDF, and initialize the security handler
79 PTPDFDoc *doc = [[PTPDFDoc alloc] initWithFilepath: @"../../TestFiles/numbered.pdf"];
80 [doc InitSecurityHandler];
81
82 // create the PTHTML2PDF converter object and modify the output of the PDF pages
83 PTHTML2PDF *converter = [[PTHTML2PDF alloc] init];
84 [converter SetPaperSize: e_11x17];
85
86 // insert the web page to convert
87 [converter InsertFromURL: @"https://docs.apryse.com/"];
88
89 // convert the web page, appending generated PDF pages to doc
90 [converter Convert: doc];
91 [doc SaveToFile: @"../../TestFiles/Output/html2pdf_example_02.pdf" flags: e_ptlinearized];
92 }
93 @catch (NSException *e)
94 {
95 NSLog(@"%@", e.reason);
96 ret = 1;
97 }
98
99 //--------------------------------------------------------------------------------
100 // Example 3) Convert multiple web pages
101 @try
102 {
103 // convert page 0 into pdf
104 PTPDFDoc *doc = [[PTPDFDoc alloc] init];
105 PTHTML2PDF *converter = [[PTHTML2PDF alloc] init];
106 NSString *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>";
107 NSString *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>";
108 [converter SetHeader:header];
109 [converter SetFooter:footer];
110 [converter SetMargins : @"1cm" bottom:@"2cm" left:@".5cm" right:@"1.5cm"];
111 PTWebPageSettings *settings = [[PTWebPageSettings alloc] init];
112 [settings SetZoom : 0.5];
113 [converter InsertFromURLWithSettings: @"https://docs.apryse.com/" settings: settings];
114 [converter Convert:doc];
115
116 // convert page 1 with the same settings, appending generated PDF pages to doc
117 [converter InsertFromURLWithSettings: @"https://docs.apryse.com/all-products/" settings: settings];
118 [converter Convert:doc];
119
120 // convert page 2 with different settings, appending generated PDF pages to doc
121 PTHTML2PDF *another_converter = [[PTHTML2PDF alloc] init];
122 [another_converter SetLandscape : YES];
123
124 PTWebPageSettings *another_settings = [[PTWebPageSettings alloc] init];
125 [another_settings SetPrintBackground : NO];
126
127 [another_converter InsertFromURLWithSettings: @"https://docs.apryse.com/web/faq" settings: another_settings];
128 [another_converter Convert:doc];
129
130 [doc SaveToFile: @"../../TestFiles/Output/html2pdf_example_03.pdf" flags: e_ptlinearized];
131 }
132 @catch (NSException *e)
133 {
134 NSLog(@"%@", e.reason);
135 ret = 1;
136 }
137
138 //--------------------------------------------------------------------------------
139 // Example 4) Convert HTML string to PDF.
140
141 @try
142 {
143 PTPDFDoc *doc = [[PTPDFDoc alloc] init];
144 PTHTML2PDF *converter = [[PTHTML2PDF alloc] init];
145
146 // Our HTML data
147 NSString *html = @"<html><body><h1>Heading</h1><p>Paragraph.</p></body></html>";
148
149 // Add html data
150 [converter InsertFromHtmlString: html];
151 // Note, InsertFromHtmlString can be mixed with the other Insert methods.
152
153 [converter Convert: doc];
154 [doc SaveToFile: @"../../TestFiles/Output/html2pdf_example_04.pdf" flags: e_ptlinearized];
155 }
156 @catch (NSException *e)
157 {
158 NSLog(@"%@", e.reason);
159 ret = 1;
160 }
161
162 //--------------------------------------------------------------------------------
163 // Example 5) Set the location of the log file to be used during conversion.
164
165 @try
166 {
167 PTPDFDoc *doc = [[PTPDFDoc alloc] init];
168 PTHTML2PDF *converter = [[PTHTML2PDF alloc] init];
169 [converter SetLogFilePath: @"../../TestFiles/Output/html2pdf.log"];
170 [converter InsertFromURL: @"https://docs.apryse.com/"];
171 [converter Convert: doc];
172 [doc SaveToFile: @"../../TestFiles/Output/html2pdf_example_05.pdf" flags: e_ptlinearized];
173 }
174 @catch (NSException *e)
175 {
176 NSLog(@"%@", e.reason);
177 ret = 1;
178 }
179
180 [PTPDFNet Terminate: 0];
181 return ret;
182 }
183}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales