How to render and convert a Website URL into a PDF file in Python

Today our goal is to set up a solution that will take in a website’s URL, then create a PDF copy of that site’s contents. Normally this would be quite a process. I would know because I’ve already put in the hours required to do it. This puts me in the position to offer you use of this effort through use of an API. Let’s look at how that can be set up.

Installing our API client is how we shall begin, so run this command here to do that.

pip install cloudmersive-convert-api-client

Next we are going to set up our function call using a newly instantiate API. We will then pass our function an input object containing our URL.

from __future__ import print_functionimport timeimport cloudmersive_convert_api_clientfrom cloudmersive_convert_api_client.rest import ApiExceptionfrom pprint import pprint# Configure API key authorization: Apikeyconfiguration = 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 classapi_instance = cloudmersive_convert_api_client.ConvertWebApi(cloudmersive_convert_api_client.ApiClient(configuration))input = cloudmersive_convert_api_client.UrlToPdfRequest() # UrlToPdfRequest | URL to PDF request parameterstry:# Convert a URL to PDFapi_response = api_instance.convert_web_url_to_pdf(input)pprint(api_response)except ApiException as e:print("Exception when calling ConvertWebApi->convert_web_url_to_pdf: %s\n" % e)

Once that’s done, it’s just a matter of waiting a short time for the API to complete the task and return the website’s PDF twin.

Written by

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

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store