How to Convert BMP to PDF with Python

Learn how to use Python to turn BMP images into PDF documents by calling the Convert to PDF API Tool from pdfRest.
Share this page

Why Use Convert to PDF with Python?

The pdfRest Convert to PDF API Tool is a powerful resource for developers needing to convert various file formats into PDF. This API provides a simple way to programmatically convert files like images, text documents, and more into a universally accepted PDF format.

This tutorial will guide you through the process of sending an API call to Convert to PDF using Python, a popular programming language known for its simplicity and readability.

In the real world, converting files to PDF can be essential for ensuring compatibility across different systems and devices. For example, a business might need to convert scanned documents (in BMP format) into PDFs to archive them or to make them easier to share with clients who may not have specialized image viewers. Using Python to automate this process can save time and reduce the risk of human error, especially when dealing with large numbers of files.

Convert 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.bmp', open('/path/to/file', 'rb'), 'image/bmp'),
        'output' : 'example_pdf_out',
    }
)

headers = {
    'Accept': 'application/json',
    'Content-Type': mp_encoder_pdf.content_type,
    'Api-Key': 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' # place your api key here
}

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)

Code source: pdfRest API Python samples

Breaking Down the Code

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

from requests_toolbelt import MultipartEncoder
import requests
import json

These lines import the necessary Python modules. The MultipartEncoder is used for creating the multipart/form-data payload, requests is for making HTTP requests, and json is for handling JSON data.

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

This snippet creates a MultipartEncoder object with the file to convert and the desired output name. The 'file' field includes the filename, the file object, and its MIME type.

headers = {
    'Accept': 'application/json',
    'Content-Type': mp_encoder_pdf.content_type,
    'Api-Key': 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' # place your api key here
}

Here, the request headers are set, including the 'Content-Type' which is automatically generated by the MultipartEncoder, and the 'Api-Key' which should be replaced with your actual API key.

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

This line sends the POST request to the pdfRest API 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)

After the request, the code checks if the response is successful. If so, it prints the JSON response; otherwise, it prints the error text.

Beyond the Tutorial

In this tutorial, you've learned how to use Python to call the pdfRest Convert to PDF API, which converts a BMP image to a PDF. This is just one of many possible conversions supported by the API. By successfully making this API call, you've taken a step towards automating document conversion processes within your applications.

To explore further, you can demo all of the pdfRest API Tools in the API Lab and refer to the API Reference Guide for more detailed information about the API's capabilities.

Note: This is an example of a multipart API call. For code samples using JSON payloads, visit the pdfRest API Python samples with JSON payloads.

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