Jun 10, 2020

2 min read

How to validate a PDF Document in Python

Validation of PDF documents, everybody’s favorite topic. But a little more seriously, what if I could show you how to get this boring yet important task completed incredibly quickly and with pretty much no effort on your part? It’s not a pipe dream, it’s an API.

Our API is going to need its client, which we can oblige using this command for pip install.

pip install cloudmersive-convert-api-client

Calling validate_document_pdf_validation will come next. As you can see from the following example, this isn’t too bad. The important steps are entering an API key and creating the API instance from which that function will run.

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.ValidateDocumentApi(cloudmersive_convert_api_client.ApiClient(configuration))input_file = '/path/to/file' # file | Input file to perform the operation on.try:# Validate a PDF document fileapi_response = api_instance.validate_document_pdf_validation(input_file)pprint(api_response)except ApiException as e:print("Exception when calling ValidateDocumentApi->validate_document_pdf_validation: %s\n" % e)

And when we run this with our input file, you will see that we are already complete in our process. Not bad for a 2-minute solution!