How to convert a PDF to PNG array in Python

Today we will be looking at a simple way to perform PDF-to-PNG array conversion using an API. This will be accomplished in two easy steps.

  1. Install the API client using pip installer:
pip install git+https://github.com/Cloudmersive/Cloudmersive.APIClient.Python.Convert.git

2. Call convert_document_pdf_to_png_array and input the desired PDF file.

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'
# 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 class
api_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:
# PDF to PNG 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)

And that’s it! The resulting output will consist of a JSON file that contains URLs for each page’s PNG, allowing for easy download or viewing.

Image for post
Image for post

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