How to Convert PNG to PDF in Python

Cloudmersive
2 min readMar 11, 2024

--

Converting PNG to PDF is quick and easy using a free, low-code API with ready-to-run Python code examples.

Let’s start by installing the client SDK. We can run this command to install using pip install:

pip install cloudmersive-convert-api-client

Next, let’s turn our attention to authorization. We need a free Cloudmersive API key to authorize our calls (this will allow a limit of 800 API calls per month with zero commitments).

Finally, let’s add the imports and call the function. We can make our conversion with multiple PNG file paths to return a multi-page PDF document:

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_file1 = '/path/to/inputfile' # file | First input file to perform the operation on.
input_file2 = '/path/to/inputfile' # file | Second input file to perform the operation on.
input_file3 = '/path/to/inputfile' # file | Third input file to perform the operation on. (optional)
input_file4 = '/path/to/inputfile' # file | Fourth input file to perform the operation on. (optional)
input_file5 = '/path/to/inputfile' # file | Fifth input file to perform the operation on. (optional)
input_file6 = '/path/to/inputfile' # file | Sixth input file to perform the operation on. (optional)
input_file7 = '/path/to/inputfile' # file | Seventh input file to perform the operation on. (optional)
input_file8 = '/path/to/inputfile' # file | Eighth input file to perform the operation on. (optional)
input_file9 = '/path/to/inputfile' # file | Ninth input file to perform the operation on. (optional)
input_file10 = '/path/to/inputfile' # file | Tenth input file to perform the operation on. (optional)

try:
# Convert PNG Array to PDF
api_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)

No more code required — we’re all done!

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

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

No responses yet