PDF2Html - Convert PDF to HTML - Python Sample Code

Sample code for using Apryse SDK to programmatically convert generic PDF documents to HTML, provided in Python, C++, C#, Java, Node.js (JavaScript), PHP, Ruby, Go and VB. Learn more about our PDF to HTML

1#---------------------------------------------------------------------------------------
2# Copyright (c) 2001-2023 by Apryse Software Inc. All Rights Reserved.
3# Consult LICENSE.txt regarding license information.
4#---------------------------------------------------------------------------------------
5
6import site
7site.addsitedir("../../../PDFNetC/Lib")
8import sys
9from PDFNetPython import *
10
11import platform
12
13sys.path.append("../../LicenseKey/PYTHON")
14from LicenseKey import *
15
16#---------------------------------------------------------------------------------------
17# The following sample illustrates how to use the PDF.Convert utility class to convert
18# documents and files to HTML.
19#
20# There are two HTML modules and one of them is an optional PDFNet Add-on.
21# 1. The built-in HTML module is used to convert PDF documents to fixed-position HTML
22# documents.
23# 2. The optional add-on module is used to convert PDF documents to HTML documents with
24# text flowing across the browser window.
25#
26# The PDFTron SDK HTML add-on module can be downloaded from https://dev.apryse.com/
27#
28# Please contact us if you have any questions.
29#---------------------------------------------------------------------------------------
30
31# Relative path to the folder containing the test files.
32inputPath = "../../TestFiles/"
33outputPath = "../../TestFiles/Output/"
34
35def main():
36 # The first step in every application using PDFNet is to initialize the
37 # library. The library is usually initialized only once, but calling
38 # Initialize() multiple times is also fine.
39 PDFNet.Initialize(LicenseKey)
40
41 #-----------------------------------------------------------------------------------
42
43 try:
44 # Convert PDF document to HTML with fixed positioning option turned on (default)
45 print("Converting PDF to HTML with fixed positioning option turned on (default)")
46
47 outputFile = outputPath + "paragraphs_and_tables_fixed_positioning"
48
49 Convert.ToHtml(inputPath + "paragraphs_and_tables.pdf", outputFile)
50
51 print("Result saved in " + outputFile)
52 except Exception as e:
53 print("Unable to convert PDF document to HTML, error: " + str(e))
54
55 #-----------------------------------------------------------------------------------
56
57 PDFNet.AddResourceSearchPath("../../../PDFNetC/Lib/")
58
59 if not StructuredOutputModule.IsModuleAvailable():
60 print("")
61 print("Unable to run part of the sample: PDFTron SDK Structured Output module not available.")
62 print("-------------------------------------------------------------------------------------")
63 print("The Structured Output module is an optional add-on, available for download")
64 print("at https://docs.apryse.com/core/info/modules/. If you have already")
65 print("downloaded this module, ensure that the SDK is able to find the required files")
66 print("using the PDFNet::AddResourceSearchPath() function.")
67 print("")
68 return
69
70 #-----------------------------------------------------------------------------------
71
72 try:
73 # Convert PDF document to HTML with reflow full option turned on (1)
74 print("Converting PDF to HTML with reflow full option turned on (1)")
75
76 outputFile = outputPath + "paragraphs_and_tables_reflow_full.html"
77
78 htmlOutputOptions = HTMLOutputOptions()
79
80 # Set e_reflow_full content reflow setting
81 htmlOutputOptions.SetContentReflowSetting(HTMLOutputOptions.e_reflow_full)
82
83 Convert.ToHtml(inputPath + "paragraphs_and_tables.pdf", outputFile, htmlOutputOptions)
84
85 print("Result saved in " + outputFile)
86 except Exception as e:
87 print("Unable to convert PDF document to HTML, error: " + str(e))
88
89 #-----------------------------------------------------------------------------------
90
91 try:
92 # Convert PDF document to HTML with reflow full option turned on (only converting the first page) (2)
93 print("Converting PDF to HTML with reflow full option turned on (only converting the first page) (2)")
94
95 outputFile = outputPath + "paragraphs_and_tables_reflow_full_first_page.html"
96
97 htmlOutputOptions = HTMLOutputOptions()
98
99 # Set e_reflow_full content reflow setting
100 htmlOutputOptions.SetContentReflowSetting(HTMLOutputOptions.e_reflow_full)
101
102 # Convert only the first page
103 htmlOutputOptions.SetPages(1, 1)
104
105 Convert.ToHtml(inputPath + "paragraphs_and_tables.pdf", outputFile, htmlOutputOptions)
106
107 print("Result saved in " + outputFile)
108 except Exception as e:
109 print("Unable to convert PDF document to HTML, error: " + str(e))
110
111 #-----------------------------------------------------------------------------------
112
113 PDFNet.Terminate()
114 print("Done.")
115
116if __name__ == '__main__':
117 main()

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales