How to Convert PDF to JPG with Python

Learn how to use the PDF to Image API Tool from pdfRest with Python to turn PDF documents into JPG image formats.
Share this page

Why Convert PDF to JPG with Python?

The pdfRest PDF to Images API Tool is a powerful resource that allows users to convert PDF documents into image files, such as JPEGs. This can be incredibly useful in scenarios where you need to display PDF content as images on a website, in a presentation, or for archival purposes where images are preferred.

In this tutorial, we will walk through how to send an API call to the PDF to Images API using Python.

Convert PDF to JPG Python Code Example

from requests_toolbelt import MultipartEncoder
import requests
import json

jpg_endpoint_url = 'https://api.pdfrest.com/jpg'

mp_encoder_jpg = MultipartEncoder(
    fields={
        'file': ('file_name.pdf', open('/path/to/file', 'rb'), 'application/pdf'),
        'pages': '1-last',
        'resolution': '600',
        'color_model': 'cmyk',
        'jpeg_quality': '90',
        'output' : 'example_jpg_out',
    }
)

headers = {
    'Accept': 'application/json',
    'Content-Type': mp_encoder_jpg.content_type,
    'Api-Key': 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
}

print("Sending POST request to jpg endpoint...")
response = requests.post(jpg_endpoint_url, data=mp_encoder_jpg, headers=headers)

print("Response status code: " + str(response.status_code))

if response.ok:
    response_json = response.json()
    print(json.dumps(response_json, indent = 2))
else:
    print(response.text)

Source of the provided code: pdf-rest-api-samples on GitHub

Breaking Down the Code

The code provided is a Python script that uses the pdfRest API to convert a PDF document into JPEG images. Let's break down each part of the code:

from requests_toolbelt import MultipartEncoder
import requests
import json

This imports the necessary libraries. MultipartEncoder from requests_toolbelt is used for encoding files as multipart/form-data.

jpg_endpoint_url = 'https://api.pdfrest.com/jpg'

This sets the API endpoint URL for the PDF to JPEG conversion service.

mp_encoder_jpg = MultipartEncoder(
    fields={
        'file': ('file_name.pdf', open('/path/to/file', 'rb'), 'application/pdf'),
        ...
    }
)

This section constructs the multipart payload. It includes the PDF file to convert and other parameters such as:

  • 'pages': Defines the range of pages to convert. '1-last' means all pages.
  • 'resolution': Sets the resolution of the output images in DPI.
  • 'color_model': Specifies the color model of the output images.
  • 'jpeg_quality': Determines the quality of the JPEG images.
  • 'output': The prefix for the output file names.
headers = {
    ...
    'Api-Key': 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
}

The headers include the content type and the API key for authentication.

response = requests.post(jpg_endpoint_url, data=mp_encoder_jpg, headers=headers)

This sends the POST request to the API endpoint with the payload and headers.

if response.ok:
    ...
else:
    print(response.text)

If the request is successful, the script prints the JSON response. Otherwise, it prints the error response.

Beyond the Tutorial

In this tutorial, we've learned how to use Python to send an API call to the pdfRest PDF to Images API to convert a PDF file into JPEG images. By following the steps outlined, you can transform PDFs into high-quality images for various applications.

Feel free to demo all of the pdfRest API Tools in the API Lab and refer to the API Reference documentation for more information.

Note: This is an example of a multipart API call. Code samples using JSON payloads can be found at pdf-rest-api-samples on GitHub.

Generate a self-service API Key now!

Create your FREE API Key to start processing PDFs in seconds, only possible with pdfRest.

Compare Plans
Contact Us