How to rasterize a Text PDF into an image-based PDF in Python

PDF rasterization is definitely not an exciting topic, as I’m sure you are aware. So why deal with coding out its implementation yourself? I’ve got a lovely API here that will have you up and running faster than you can finish your coffee.

It all begins with client installation, as you see here:

pip install cloudmersive-convert-api-client

Next we will instance our API and use it to call edit_pdf_rasterize.

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.EditPdfApi(cloudmersive_convert_api_client.ApiClient(configuration))input_file = '/path/to/file' # file | Input file to perform the operation on.try:# Rasterize a PDF to an image-based PDFapi_response = api_instance.edit_pdf_rasterize(input_file)pprint(api_response)except ApiException as e:print("Exception when calling EditPdfApi->edit_pdf_rasterize: %s\n" % e)

And lastly, provide your file path. That’s it. After sending the request, your rasterized PDF will be finished in a matter of moments.

Image for post
Image for post
Mmmm, coffee.

Written by

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