How to convert HTML to PDF in Python
1 min readAug 22, 2019
Let’s look at how simplify converting HTML to a PDF. First thing to do is use the pip installer to add our client:
pip install git+https://github.com/Cloudmersive/Cloudmersive.APIClient.Python.Convert.git
Then call convert_web_html_to_pdf:
from __future__ import print_function
import time
import cloudmersive_convert_api_client
from cloudmersive_convert_api_client.rest import ApiException
from pprint import pprint# Configure API key authorization: Apikey
configuration = cloudmersive_convert_api_client.Configuration()
configuration.api_key['Apikey'] = 'YOUR_API_KEY'
# Uncomment below to setup prefix (e.g. Bearer) for API key, if needed
# configuration.api_key_prefix['Apikey'] = 'Bearer'# 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 parameterstry:
# Convert HTML string to PDF
api_response = api_instance.convert_web_html_to_pdf(input)
pprint(api_response)
except ApiException as e:
print("Exception when calling ConvertWebApi->convert_web_html_to_pdf: %s\n" % e)
The input should be an HTML string. Images may be included as well as long as absolute URLs are used. How easy was that!