Sitemap
Alexander Stock

Alexander Stock

Experienced software development consultant and blogger with 10+ years in the industry, specializing in document processing tools and knowledge sharing.

Convert PDF to HTML with Python (Developer Guide)

3 min readApr 29, 2024

--

Press enter or click to view image in full size

By transforming PDF documents into HTML files, it becomes easier to integrate documents into web-based environment, incorporate interactive elements, and enable effortless content modification.

In this blog post, I will walk you through the process of converting PDF to HTML using a Python PDF library.

Install the Library

Spire.PDF for Python is my trusted tool for PDF document processing. This robust Python library allows me to effortlessly convert PDF files to HTML, without requiring any additional software installations.

The library can be install via PyPI by running the following pip command.

pip install Spire.PDF

PDF to HTML Conversion Options

Spire.PDF for Python offers a method called ConvertOptions.SetPdfToHtmlOptions() method that allows you to customize the options for converting PDF to HTML. This method takes the following four parameters:

  • useEmbeddedSvg(boolean) : Specifies whether to convert the PDF to an SVG image file and embed it in the HTML code.
  • useEmbededImg(boolean): Specifies whether to embed image data in the HTML file. This option is only effective when useEmbeddedSvg is set to false.
  • maxPageOneFile(boolean): Specifies the maximum number of pages in one HTML file. This option is only effective when useEmbeddedSvg is set to false.
  • useHighQualityEmbeddedSvg(boolean): Specifies whether to use high-quality SVG image embedding in the HTML. This option is only effective when useEmbeddedSvg is set to true.

Depending on the value of the “useEmbeddedSvg” parameter, you may have observed that the PDF file can undergo two types of conversions. If “useEmbeddedSvg” is set to true, the PDF is transformed into an SVG image enclosed within HTML code. On the other hand, if “useEmbeddedSvg” is set to false, the PDF is converted into a HTML file that retains all the details.

Get Alexander Stock’s stories in your inbox

Join Medium for free to get updates from this writer.

Converting PDF to HTML SVG graphics ensures that the original layout is preserved, which can be beneficial if maintaining the visual structure is important. Conversely, if further editing of the document is desired, choosing to convert the PDF to a complete HTML file will be more advantageous.

Convert a PDF to an HTML File with SVG Embedded in Python

The following code converts a PDF document into a single SVG graphic and embed it in a HTML file.

from spire.pdf.common import *
from spire.pdf import *

# Create a Document object
doc = PdfDocument()

# Load a PDF document
doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Privacy Policy.pdf")

# Set the conversion options to
convertOptions = doc.ConvertOptions

# Specify convert options
convertOptions.SetPdfToHtmlOptions(True, True, 1, True)

# Save the PDF document to HTML format
doc.SaveToFile("output/ToHTML.html", FileFormat.HTML)

# Dispose resources
doc.Dispose()

Convert a PDF to an HTML File with Details Included in Python

This example shows how to convert a PDF file into multiple HTML files. Each HTML file contains the details of one page from the PDF.

from spire.pdf.common import *
from spire.pdf import *

# Create a Document object
doc = PdfDocument()

# Load a PDF document
doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Privacy Policy.pdf")

# Set the conversion options to
convertOptions = doc.ConvertOptions

# Specify convert options
convertOptions.SetPdfToHtmlOptions(False, True, 1, False)

# Save the PDF document to HTML format
doc.SaveToFile("output/ToHTML.html", FileFormat.HTML)

# Dispose resources
doc.Dispose()

Convert a PDF to an HTML File Stream in Python

Instead of converting a PDF to an HTML file, you can convert it to an HTML file stream. Here is an example.

from spire.pdf.common import*
from spire.pdf import*

# Create a Document object
doc = PdfDocument()

# Load a PDF document
doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Privacy Policy.pdf")

# Set the conversion options to
convertOptions = doc.ConvertOptions

# Specify convert options
convertOptions.SetPdfToHtmlOptions(True, True, 1, True)

# Save the PDF document to an HTML stream
fileStream = Stream()
doc.SaveToStream(fileStream, FileFormat.HTML)

# Dispose resources
fileStream.Dispose()
doc.Dispose()

Conclusion

In this blog post, we’ve explored how to convert PDF to HTML in C#. By converting a PDF to an HTML file with embedded SVG, the original layout can be preserved. Alternatively, converting to an HTML file with all details included allows for further editing.

See Also

--

--

Alexander Stock
Alexander Stock

Written by Alexander Stock

Experienced software development consultant and blogger with 10+ years in the industry, specializing in document processing tools and knowledge sharing.