How to Convert TIFF to PDF with Python

Use the Convert to PDF API Tool and learn how to convert TIFF images into PDF documents easily, all in the pdfRest API Toolkit.
Share this page

Why Convert TIFF to PDF with Python?

The pdfRest Convert to PDF API Tool is a powerful resource for developers looking to automate the conversion of various file formats into PDFs. This tutorial will demonstrate how to send an API call to Convert to PDF using Python. This is particularly useful in scenarios where you need to standardize the format of documents for archiving, sharing, or printing. For instance, a user might want to convert scanned TIFF images into PDF format to ensure compatibility across different platforms and devices.

Convert TIFF to PDF with Python Code Example

from requests_toolbelt import MultipartEncoder
import requests
import json

pdf_endpoint_url = 'https://api.pdfrest.com/pdf'

mp_encoder_pdf = MultipartEncoder(
    fields={
        'file': ('file_name.tif', open('/path/to/file', 'rb'), 'image/tiff'),
        'output' : 'example_pdf_out',
    }
)

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

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

Reference: GitHub - datalogics/pdf-rest-api-samples

Breaking Down the Code

The code snippet provided above demonstrates how to make a multipart/form-data POST request to the pdfRest Convert to PDF API endpoint. Let's break down the key parts of the code:

mp_encoder_pdf = MultipartEncoder(
    fields={
        'file': ('file_name.tif', open('/path/to/file', 'rb'), 'image/tiff'),
        'output' : 'example_pdf_out',
    }
)

This creates a MultipartEncoder object with the necessary fields for the API request. The 'file' field contains a tuple with the file name, a file object opened in binary read mode, and the MIME type of the file. The 'output' field specifies the desired name for the output PDF file.

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

The headers include the 'Accept' header to specify that the response should be JSON, the 'Content-Type' header set to 'multipart/form-data', and the 'Api-Key' header with your API key.

response = requests.post(pdf_endpoint_url, data=mp_encoder_pdf, headers=headers)

This line sends the POST request to the specified endpoint URL with the encoded data and headers. The response is then processed to check if the request was successful and to handle the JSON response or error message accordingly.

Beyond the Tutorial

By following the steps above, we've successfully called the Convert to PDF API using Python to convert a TIFF image to a PDF. This process can be applied to various input file types supported by the API. To explore and test all of the pdfRest API Tools, visit the API Lab at https://pdfrest.com/apilab/. For further details on the API, check out the API Reference documentation at https://pdfrest.com/documentation/.

Note: This is an example of a multipart API call. Code samples using JSON payloads can be found at GitHub - datalogics/json-payload-examples.

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