How to Convert PDF to PNG with Python

Learn how to convert each page of a PDF to PNG using pdfRest PDF to Images API Tool with Python
Share this page

Why Convert PDF to PNG with Python?

The pdfRest PDF to Images API Tool is a powerful utility that allows users to convert PDF documents into image files. This tutorial will guide you on how to send an API call to the PDF to Images endpoint using Python. This can be particularly useful for developers looking to automate the conversion of PDF files into images for further processing or display in web applications.

Imagine a scenario where a user needs to display individual pages of a PDF document as images on a website. Using the PDF to Images API, the user can easily convert each page into an image format like PNG, which can then be embedded directly into a webpage. This is especially useful for applications such as online document viewers, digital libraries, or any service that requires high-quality image representations of PDF content.

PDF to PNG with Python Code Example

from requests_toolbelt import MultipartEncoder
import requests
import json

png_endpoint_url = 'https://api.pdfrest.com/png'

# The /png endpoint can take a single PDF file or id as input and turn them into PNG image files.
# This sample takes in a PDF and converts all pages into grayscale PNG files.
mp_encoder_png = MultipartEncoder(
    fields={
        'file': ('file_name.pdf', open('/path/to/file', 'rb'), 'application/pdf'),
        'pages': '1-last',
        'resolution': '600',
        'color_model': 'gray',
        'output' : 'example_png_out',
    }
)

# Let's set the headers that the png endpoint expects.
# Since MultipartEncoder is used, the 'Content-Type' header gets set to 'multipart/form-data' via the content_type attribute below.
headers = {
    'Accept': 'application/json',
    'Content-Type': mp_encoder_png.content_type,
    'Api-Key': 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' # place your api key here
}

print("Sending POST request to png endpoint...")
response = requests.post(png_endpoint_url, data=mp_encoder_png, 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)

# If you would like to download the file instead of getting the JSON response, please see the 'get-resource-id-endpoint.py' sample.

Source: GitHub

Breaking Down the Code

Let's break down the code to understand how it works:

from requests_toolbelt import MultipartEncoder
import requests
import json

This imports the necessary libraries. requests_toolbelt.MultipartEncoder is used to handle multipart form data, requests is used to make HTTP requests, and json is used to handle JSON data.

png_endpoint_url = 'https://api.pdfrest.com/png'

This sets the endpoint URL for the PDF to Images API.

mp_encoder_png = MultipartEncoder(
    fields={
        'file': ('file_name.pdf', open('/path/to/file', 'rb'), 'application/pdf'),
        'pages': '1-last',
        'resolution': '600',
        'color_model': 'gray',
        'output' : 'example_png_out',
    }
)

Here, we create a MultipartEncoder object to handle the multipart form data. The fields parameter contains:

  • 'file': The PDF file to be converted. Replace '/path/to/file' with the actual file path.
  • 'pages': Specifies the pages to convert. '1-last' means all pages.
  • 'resolution': The resolution of the output images, set to 600 DPI.
  • 'color_model': The color model for the output images, set to grayscale.
  • 'output': The output file name prefix.
headers = {
    'Accept': 'application/json',
    'Content-Type': mp_encoder_png.content_type,
    'Api-Key': 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' # place your api key here
}

The headers include:

  • 'Accept': Specifies that the response should be in JSON format.
  • 'Content-Type': Set automatically by MultipartEncoder to 'multipart/form-data'.
  • 'Api-Key': Your API key for authentication.
response = requests.post(png_endpoint_url, data=mp_encoder_png, headers=headers)

This sends a POST request to the API endpoint with the multipart form data and headers.

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

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

Beyond the Tutorial

In this tutorial, you learned how to use the pdfRest PDF to Images API to convert a PDF document into PNG images using Python. This is a powerful tool for automating the conversion of PDF files into image formats for various applications.

To explore more functionalities, you can demo all of the pdfRest API Tools in the API Lab. For detailed documentation, refer to the API Reference Guide.

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

Generate a self-service API Key now!
Create your FREE API Key to start processing PDFs in seconds, only possible with pdfRest.