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

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales