How to convert a PNG Array to PDF in Python

Creating a PDF out of an array of PNG images just got a whole lot easier for all the Python users out there. I’m talking about an API from Cloudmersive that will allow you to perform this function without the arduous coding process normally required to do conversions of this kind. Let’s get straight to the setup.

Begin with pip installation of our Convert API’s client files.

pip install cloudmersive-convert-api-client

Next we need to call convert_document_png_array_to_pdf, which can be fed our input PNGs. Stack these vertically to maintain their order, as shown in this example:

from __future__ import print_functionimport timeimport cloudmersive_convert_api_clientfrom cloudmersive_convert_api_client.rest 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_file1 = '/path/to/file' # file | First input file to perform the operation on.input_file2 = '/path/to/file' # file | Second input file to perform the operation on.input_file3 = '/path/to/file' # file | Third input file to perform the operation on. (optional)input_file4 = '/path/to/file' # file | Fourth input file to perform the operation on. (optional)input_file5 = '/path/to/file' # file | Fifth input file to perform the operation on. (optional)input_file6 = '/path/to/file' # file | Sixth input file to perform the operation on. (optional)input_file7 = '/path/to/file' # file | Seventh input file to perform the operation on. (optional)input_file8 = '/path/to/file' # file | Eighth input file to perform the operation on. (optional)input_file9 = '/path/to/file' # file | Ninth input file to perform the operation on. (optional)input_file10 = '/path/to/file' # file | Tenth input file to perform the operation on. (optional)try:# Convert PNG Array to PDFapi_response = api_instance.convert_document_png_array_to_pdf(input_file1, input_file2, input_file3=input_file3, input_file4=input_file4, input_file5=input_file5, input_file6=input_file6, input_file7=input_file7, input_file8=input_file8, input_file9=input_file9, input_file10=input_file10)pprint(api_response)except ApiException as e:print("Exception when calling ConvertDocumentApi->convert_document_png_array_to_pdf: %s\n" % e)

And that right there, is a wrap. If you need to use more files than this, you may perform this function multiple times, then merge the resulting PDFs with one of our other APIs.

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