How to Convert a URL to PDF using Python
Using PDFs has become a common way to share a variety of office content, including PowerPoint presentations, image arrays, and much more. With our URL to PDF conversion API, you can piggyback on the PDF format once again, automatically rendering a full screenshot of a website URL in PDF format. This generates a useful document containing the “face” of any given website or web page.
You can easily take advantage of this API using the ready-to-run code examples provided below in Python or find additional code snippets available in other programming languages on the Cloudmersive API console page. In addition, you can leverage this API as a connector action in Microsoft Power Automate/Azure Logic Apps.
Let’s begin by running the below command to install the Python SDK:
pip install cloudmersive-convert-api-client
Next, let’s include the below imports:
from __future__ import print_function
import time
import cloudmersive_convert_api_client
from cloudmersive_convert_api_client.rest import ApiException
from pprint import pprint
Now let’s copy in the API key authorization snippet, followed by the remaining code to complete 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.UrlToPdfRequest() # UrlToPdfRequest | URL to PDF request parameterstry:
# Convert a URL to PDF
api_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)
If you don’t have an API key, you can easily get one by registering a free account on our website. Your free account will allow a limit of 800 API calls per month.