How to convert a PDF to a single PNG image in Python

For today’s tutorial, we will be applying Python to the task of turning a PDF document into a single large PNG image. In terms of difficulty, this can range from tedious to surprisingly easy, depending on your approach. I hope you don’t mind if I assume you would like to see the easy method. Let’s take a look.

We are going to start with client installation for the API that we will be using, via pip install.

pip install cloudmersive-convert-api-client

Alright, now we can do our function call, which should be structured along these lines:

from __future__ import print_functionimport timeimport cloudmersive_convert_api_clientfrom 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.ConvertDocumentApi(cloudmersive_convert_api_client.ApiClient(configuration))input_file = '/path/to/file' # file | Input file to perform the operation on.try:# Convert PDF to Single PNG imageapi_response = api_instance.convert_document_pdf_to_png_single(input_file)pprint(api_response)except ApiException as e:print("Exception when calling ConvertDocumentApi->convert_document_pdf_to_png_single: %s\n" % e)

And would you believe that we are already done? Yes indeed, any PDF input file that we pass into this function will be converted into a PNG and returned — easy.

Image for post

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