How to Convert PDF to TIFF with Python

Learn how to convert PDF pages to TIFF images using Python with pdfRest PDF to Images API tool
Share this page

Why Convert PDF to TIFF with Python?

The pdfRest PDF to Images API Tool is a powerful resource that allows users to convert PDF files into image formats such as TIFF. This tutorial will show you how to send an API call to the PDF to Images endpoint using Python, enabling you to automate the conversion process easily.

Imagine you are working in a document management system where you need to archive PDFs as images for better compatibility with various systems and devices. Converting PDFs to TIFF images can be particularly useful for creating high-quality, high-resolution images suitable for printing or archiving.

PDF to TIFF with Python Code Example

from requests_toolbelt import MultipartEncoder
import requests
import json

tif_endpoint_url = 'https://api.pdfrest.com/tif'

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

# Let's set the headers that the tif 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_tif.content_type,
    'Api-Key': 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' # place your api key here
}

print("Sending POST request to tif endpoint...")
response = requests.post(tif_endpoint_url, data=mp_encoder_tif, 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 Repository

Breaking Down the Code

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

from requests_toolbelt import MultipartEncoder
import requests
import json

We start by importing the necessary libraries. requests_toolbelt is used to handle multipart form data, requests is used to send HTTP requests, and json is used to handle JSON data.

tif_endpoint_url = 'https://api.pdfrest.com/tif'

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

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

Here, we create a MultipartEncoder instance to handle the multipart form data. The fields include:

  • 'file': The PDF file to be converted.
  • 'pages': Specifies the range of pages to convert, from the first to the last page.
  • 'resolution': Sets the resolution of the output images to 600 DPI.
  • 'color_model': Converts the images to grayscale.
  • 'output': The name of the output file.
headers = {
    'Accept': 'application/json',
    'Content-Type': mp_encoder_tif.content_type,
    'Api-Key': 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' # place your api key here
}

We set the headers for the request. The 'Content-Type' is set to 'multipart/form-data' using mp_encoder_tif.content_type, and the 'Api-Key' is your unique API key.

response = requests.post(tif_endpoint_url, data=mp_encoder_tif, headers=headers)

This line sends a POST request to the PDF to Images endpoint with the multipart data and headers.

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

Finally, we check if the response is successful. If it is, we print the JSON response. Otherwise, we print the error message.

Beyond the Tutorial

In this tutorial, we demonstrated how to convert a PDF to TIFF images using the pdfRest API with Python. You can explore more features and tools offered by pdfRest by visiting the API Lab.

For more detailed information, refer to the API Reference Guide. This example uses a multipart API call, but you can also find code samples using JSON payloads at GitHub Repository.

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