How to Convert EML to a PDF Document in Java

Cloudmersive
2 min readJul 21, 2021

--

Getting frustrated with that EML file you’re trying to work with? We hear you. Traditionally, EML files are not the easiest format to deal with, especially when attempting to collaborate with internal or external partners. Let’s eliminate that issue by transforming that EML file to the more user-friendly PDF format using an API in Java. Images are even supported if they are base 64 inline!

To kick things off, we will install the Maven SDK by adding a jitpack reference to the repository:

<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>

Next, we will add a reference to the dependency:

<dependencies>
<dependency>
<groupId>com.github.Cloudmersive</groupId>
<artifactId>Cloudmersive.APIClient.Java</artifactId>
<version>v3.90</version>
</dependency>
</dependencies>

At this point, we are ready to call the function with the below example code:

// Import classes:
//import com.cloudmersive.client.invoker.ApiClient;
//import com.cloudmersive.client.invoker.ApiException;
//import com.cloudmersive.client.invoker.Configuration;
//import com.cloudmersive.client.invoker.auth.*;
//import com.cloudmersive.client.ConvertDocumentApi;
ApiClient defaultClient = Configuration.getDefaultApiClient();// Configure API key authorization: Apikey
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");
Apikey.setApiKey("YOUR API KEY");
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
//Apikey.setApiKeyPrefix("Token");
ConvertDocumentApi apiInstance = new ConvertDocumentApi();
File inputFile = new File("/path/to/inputfile"); // File | Input file to perform the operation on.
Boolean bodyOnly = true; // Boolean | Optional; If true, the HTML string will only include the body of the email. Other information such as subject will still be given as properties in the response object. Default is false.
try {
byte[] result = apiInstance.convertDocumentEmlToPdf(inputFile, bodyOnly);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling ConvertDocumentApi#convertDocumentEmlToPdf");
e.printStackTrace();
}

And just like that, we’re done!

--

--

Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.