How to Rasterize a PDF File in Python

Cloudmersive
2 min readJan 3, 2023

--

Rather than entirely change your PDF’s format to an image file like PNG or JPG, you can choose to rasterize your PDF instead, creating a 2-dimensional image-based version of the file (which will still be stored in PDF format).

The below Python code makes this transition easy, converting vector PDF files into raster PDF files via our PDF Rasterization API in one simple step. This operation offers practical file efficiency and content security benefits, reducing the size of your PDF files while also ensuring they cannot be easily converted into editable document formats (the latter will require the help of an OCR operation, which necessarily creates an entirely new file).

To install the Python SDK, run the below command:

pip install cloudmersive-convert-api-client

After that, call the function, adding your imports at the top of your 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'



# create an instance of the API class
api_instance = cloudmersive_convert_api_client.EditPdfApi(cloudmersive_convert_api_client.ApiClient(configuration))
input_file = '/path/to/inputfile' # file | Input file to perform the operation on.

try:
# Rasterize a PDF to an image-based PDF
api_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)

All that’s required to complete your API call is a valid Cloudmersive API key, which can be obtained easily & for free by registering a free account on our website (free-tier API keys will provide a limit of 800 API calls per month with no financial commitments; once your monthly limit is reached, your total will reset the following month).

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

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

No responses yet