Convert Email to PDF in Python using PDF Extractor Web API

Dec 30, 2024·5 Minutes Read

In this detailed tutorial, we will guide you through the process of converting an email file (either .eml or .msg) to a PDF document using the PDF.co Web API in Python. We will use a sample .eml file for this demonstration, but you can apply the same steps to .msg files as well.

Sample .eml file

Step 1: Install the requests Library

Before starting, you need to install the requests library. This Python library is essential for making HTTP requests to the PDF.co Web API, which will allow you to interact with PDF.co’s services.

  1. Open your terminal or command line interface (CLI).
  2. Run the following command to install the requests module: python -m pip install requests

This will install the requests library in your Python environment, enabling you to communicate with the PDF.co Web API.

Step 2: Access the Source Code

Next, you need to prepare the Python script that will handle the conversion of the email file into a PDF.

  • Copy the sample Python code from the link provided (or use the code example below).
  • Paste the code into your preferred Python code editor, such as Visual Studio Code, PyCharm, or any other Python-compatible editor.

Step 3: Configure the Python Code

Now that you have the sample code, let’s configure it with your specific settings.

API Key:

  • Obtain your API Key from your PDF.co dashboard.
  • Add your API key to the designated spot in the Python script.

Source File URL:

  • Provide the URL for the email file you want to convert into a PDF. If the email file is stored locally on your machine, you need to upload it to PDF.co’s File Storage.
  • Once uploaded, you will receive a file URL (token URL) that you can use in the script to reference the file for conversion.

Output PDF Name:

  • Choose the desired name for the output PDF file where the converted content will be saved. Modify this in the code as needed.

Asynchronous Mode:

  • For efficiency, we recommend using Asynchronous Mode during the conversion process. This mode allows the conversion to run in the background, enabling the program to continue executing without waiting for the conversion to complete.

Paper Size:

  • Specify your desired paper size (e.g., Letter, A4) for the generated PDF output. Adjust the paperSize parameter in the code to match your requirements.

Embed Attachments:

  • If you want to include email attachments in the PDF conversion, set the Embed Attachments parameter to true. This will ensure that any attachments are embedded in the final PDF.

Convert Attachments:

  • Set this parameter to true if you want to include the email attachments in the conversion process. This ensures that any files attached to the email are converted along with the email body.
Configure the Python Code

Step 4: Save Python Program

Once you have updated the code with your API Key, email file URL, and parameter settings, save the Python program in your preferred directory.

Save Python Program

Step 5: Run the Program

Now, execute the program. If everything is configured correctly, the program will start the email-to-PDF conversion. Once the conversion is complete, you will receive a generated PDF URL.

  • You can copy and paste the URL into your browser to view the generated PDF.
  • Alternatively, navigate to the directory where the Python script is located, and you will find the converted PDF file saved with the specified name.
Run the Program

Step 6: View PDF Result

After the script finishes running and the PDF file has been generated, open the output PDF using your preferred PDF viewer. You should now see the content of your email file converted into a neatly formatted PDF document.

View PDF Result

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.

Related Tutorials

See Related Tutorials