How to Convert an HTML String to PDF using Python

2 min readJul 29, 2022


Hyperlinks aren’t the only way to share & present how a web page looks. By converting HTML to PDF format, you can generate fully rendered web pages in an easily shareable/transferrable way. Our HTML to PDF API makes it possible to include that operation as a service in your app — and it’s free to use. Below, we’ll walk through how you can take advantage of this API using Python code examples to structure your API call.

Start by running the below command to install the Python SDK:

pip install cloudmersive-convert-api-client

After that, add in the imports:

from __future__ import print_function
import time
import cloudmersive_convert_api_client
from import ApiException
from pprint import pprint

Lastly, authenticate your API key (you can get one by registering a free account on our website) and call the function:

# Configure API key authorization: Apikey
configuration = cloudmersive_convert_api_client.Configuration()
configuration.api_key['Apikey'] = 'YOUR_API_KEY'
# create an instance of the API class
api_instance = cloudmersive_convert_api_client.ConvertWebApi(cloudmersive_convert_api_client.ApiClient(configuration))
input = cloudmersive_convert_api_client.HtmlToPdfRequest() # HtmlToPdfRequest | HTML to PDF request parameters
# Convert HTML string to PDF
api_response = api_instance.convert_web_html_to_pdf(input)
except ApiException as e:
print("Exception when calling ConvertWebApi->convert_web_html_to_pdf: %s\n" % e)

That’s all you need — easy as pie. You can also take advantage of this API using different code examples on the Cloudmersive API Console, or as a Connector Action in MS Power Automate/Logic Apps.




There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.