Convert Email to PDF in Python using PDF Extractor Web API

Learn how to convert email to PDF in Python using PDF Extractor API.

Here’s the sample email with attachments that we’re going to use for this tutorial.

Sample Email
Sample Email

Use Cases

Converting emails to PDF format can be useful for a variety of reasons, including:

Archiving important emails: If you need to keep a record of important emails for legal or compliance reasons, converting them to PDF format can help ensure that the contents of the emails are preserved and can be easily accessed in the future.

Sharing emails with others: If you need to share an email or a thread of emails with others, converting them to PDF format can make it easier to do so, as PDF files are widely supported across different devices and platforms.

Preserving formatting: Converting emails to PDF format can help preserve the formatting of the original email, including images, links, and text formatting.

Reducing file size: Some email clients, such as Gmail, allow you to download emails as PDF files, which can be smaller in size compared to other formats like HTML or text.

Creating an e-book or document: If you have a collection of emails that you want to turn into an e-book or a document, converting them to PDF format can be a good starting point. You can then edit and organize the PDF file as needed.

Overall, converting emails to PDF format can be a useful way to preserve and share important information contained in emails. In this tutorial, we’ve shown you how to convert email to PDF using PDF.co. It can help you create PDFs in Python based on emails, and use these data for your next workflows.

Source Code

Here is the source code for the operation, create a Python file with this content:

import requests

url = "https://api.pdf.co/v1/pdf/convert/from/email"

payload={'url': 'https://bytescout-com.s3-us-west-2.amazonaws.com/files/demo-files/cloud-api/email-to-pdf/sample.eml',
'embedAttachments': 'true',
'convertAttachments': 'true',
'paperSize': 'Letter',
'name': 'email-with-attachments',
'async': 'false'}
files=[

]
headers = {
		'Content-Type': 'application/json',
		'x-api-key': '{{x-api-key}}'
}

response = requests.request("POST", url, headers=headers, json=payload, files=files)

print(response.text)

Install Request Module

  • If you haven’t yet, kindly install the requests module. You can do it by running python -m pip install requests in your command line.

Add API Key

Add your API key to your sample Python code on line 17.

URL to Source File & Run Code

On line 6 add the URL to your source file and set the parameters for your output PDF.

Then run the code on the command line with Python!

The output will generate a URL where you can download the resulting PDF file.

You’ve learned how to convert email to PDF in Python using PDF Extractor API with this tutorial. This is just one of the examples of how PDF.co Web API can make things easier for Python developers.

We can also teach you how to generate PDFs with barcodes in Python using PDF.co Web API, among other things.