HTML2PDF - HTML to PDF Conversion - Go 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-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 https://docs.apryse.com/core/guides/info/modules#html2pdf-module.
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 https://docs.apryse.com/core/guides/info/modules. 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}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales