How to Convert PDF to PNG in Python
1 min readMar 11, 2024
Using complementary Python code examples, we can easily convert PDF documents to PNG images with a free API.
First, let’s install the client SDK using Pip install:
pip install cloudmersive-convert-api-client
Next, let’s get a free Cloudmersive API key for authentication. This will allow a limit of 800 API calls per month with no commitments.
Lastly, let’s add the imports and call the function. We can make the conversion with our PDF file path:
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'
# create an instance of the API class
api_instance = cloudmersive_convert_api_client.ConvertDocumentApi(cloudmersive_convert_api_client.ApiClient(configuration))
input_file = '/path/to/inputfile' # file | Input file to perform the operation on.
try:
# Convert PDF to PNG Image Array
api_response = api_instance.convert_document_pdf_to_png_array(input_file)
pprint(api_response)
except ApiException as e:
print("Exception when calling ConvertDocumentApi->convert_document_pdf_to_png_array: %s\n" % e)
All done — no more code required!