How to Convert Email to PDF with Python

Convert Email (.eml) files to PDF with Python using a multipart pdfRest API request.
Share this page

This Python walkthrough uses requests and a multipart encoder. It converts an Email (.eml) file into a PDF through the pdfRest Convert to PDF API Tool. The complete sample is included below so Python developers can see the file handling, request construction, and response path together.

Why Email to PDF with Python?

Python ingestion pipelines commonly receive message files alongside other case, research, or operational inputs. Converting an .eml item to PDF creates a stable artifact that can join reports, review packets, and document-processing pipelines.

A data-governance workflow could convert a notification email associated with a model run, keep the PDF with the run’s evidence package, and retain the source message in its normal data store. The resulting document is easy for a reviewer to open without mail-client context.

The sample keeps upload and response handling explicit, so a Python application can add its own retry policy, output download, or resource cleanup after it receives the managed PDF ID.

Python Code Example for Email to PDF

import json
import os
import requests

# By default, we use the US-based API service. This is the primary endpoint for global use.
api_url = "https://api.pdfrest.com"

# For GDPR compliance and enhanced performance for European users, you can switch to the EU-based service by uncommenting the URL below.
# For more information visit https://pdfrest.com/pricing#how-do-eu-gdpr-api-calls-work
#api_url = "https://eu-api.pdfrest.com"

# This sample converts an Email (.eml) file to PDF by sending it directly in a
# multipart /pdf request.
input_path = "/path/to/sample.eml"

from requests_toolbelt import MultipartEncoder
with open(input_path, "rb") as input_file:
    fields = {
        "file": (os.path.basename(input_path), input_file, "message/rfc822"),
        "output": "pdf_from_email",
    }
    form = MultipartEncoder(fields=fields)
    response = requests.post(api_url + "/pdf", data=form, headers={
        "Accept": "application/json",
        "Content-Type": form.content_type,
        "Api-Key": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
    })

print("Response status code: " + str(response.status_code))
if response.ok:
    print(json.dumps(response.json(), indent=2))
else:
    print(response.text)
    raise SystemExit(1)

Source: View the sample on GitHub

Breaking Down the Code

Configure the service and input

# By default, we use the US-based API service. This is the primary endpoint for global use.
api_url = "https://api.pdfrest.com"

# For GDPR compliance and enhanced performance for European users, you can switch to the EU-based service by uncommenting the URL below.
# For more information visit https://pdfrest.com/pricing#how-do-eu-gdpr-api-calls-work
#api_url = "https://eu-api.pdfrest.com"

For this Email to PDF example, Python sends the multipart request to /pdf. The Python samples use requests and MultipartEncoder from requests-toolbelt. Install both packages when using the file outside the samples project and keep the API key in environment configuration. The sample’s placeholder path and API key make the moving pieces visible; replace them with a real local path and protected runtime credential before using the request.

Upload the Email File

from requests_toolbelt import MultipartEncoder
with open(input_path, "rb") as input_file:
    fields = {
        "file": (os.path.basename(input_path), input_file, "message/rfc822"),
        "output": "pdf_from_email",
    }
    form = MultipartEncoder(fields=fields)
    response = requests.post(api_url + "/pdf", data=form, headers={
        "Accept": "application/json",
        "Content-Type": form.content_type,
        "Api-Key": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",

The Python request places the Email source in the multipart part named file. Retain its .eml filename extension because Convert to PDF uses the uploaded name to identify the source type, then use the optional output field to choose the generated resource name without a PDF extension.

Build the multipart request

input_path = "/path/to/sample.eml"

from requests_toolbelt import MultipartEncoder
with open(input_path, "rb") as input_file:
    fields = {
        "file": (os.path.basename(input_path), input_file, "message/rfc822"),
        "output": "pdf_from_email",
    }
    form = MultipartEncoder(fields=fields)
    response = requests.post(api_url + "/pdf", data=form, headers={
        "Accept": "application/json",

For the Email to PDF form, MultipartEncoder combines the file tuple and text fields, then exposes a content_type value that includes the generated boundary. That value must be used as the request header.

Read the output resource

"output": "pdf_from_email",
    }
    form = MultipartEncoder(fields=fields)
    response = requests.post(api_url + "/pdf", data=form, headers={
        "Accept": "application/json",
        "Content-Type": form.content_type,
        "Api-Key": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
    })

print("Response status code: " + str(response.status_code))
if response.ok:
    print(json.dumps(response.json(), indent=2))

After the Email to PDF call, the sample prints formatted response JSON after a successful request and exits with an error after printing a failed response. Preserve the returned outputId when the next workflow step needs the generated resource.

Beyond the Tutorial

In this Python tutorial, you converted an Email file into a PDF resource with a direct multipart request. The pattern gives a message-based workflow a document output that can be reviewed, downloaded, or passed to another pdfRest operation.

Try representative email to pdf inputs in API Lab. For the authoritative request fields, accepted values, and response contract, consult the Convert to PDF API Tool documentation. The same request pattern can then be adapted to your Python application’s error handling, retention policy, and delivery workflow.

The repository also provides a JSON-payload Email-to-PDF version for Python. It uploads the message first and then posts its resource ID to /pdf, which is useful when an application stages inputs for several later operations. View the JSON-payload sample on GitHub.

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