How to Convert PDF to JPG in Python

Cloudmersive
2 min readMar 13, 2024

--

Making PDF to JPG conversions with code should be easy — especially in Python.

By calling a free API with complementary Python code examples, we can make our conversion to JPG even easier. We’ll simply abstract the conversion logic away from our server and avoid pesky maintenance in our own code base.

We can start by installing the client SDK with pip install:

pip install cloudmersive-convert-api-client

And after that, we can add the following imports & use the remaining code to call the function. We can use our PDF file path to make our conversion:

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.
quality = 56 # int | Optional; Set the JPEG quality level; lowest quality is 1 (highest compression), highest quality (lowest compression) is 100; recommended value is 75. Default value is 75. (optional)

try:
# Convert PDF to JPG/JPEG image array
api_response = api_instance.convert_document_pdf_to_jpg(input_file, quality=quality)
pprint(api_response)
except ApiException as e:
print("Exception when calling ConvertDocumentApi->convert_document_pdf_to_jpg: %s\n" % e)

To take care of authorization, we can get a free Cloudmersive API key and enter that in the configuration.api_key snippet. We can make up to 800 API calls per month with no commitments using this key.

Just like that, we’re all done — no more code required.

--

--

Cloudmersive

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