1. Products
  2.   Email
  3.   Java
  4.   Email-to-PDF Converter
 
  

Free Java Email Library to Convert EML & MSG to PDF

Open Source Java Email Processing Library for Converting Email Files (EML, MSG) to PDF. It Supports Handling Attachments and Inline Images and Multiple Email Formats inside Java apps.

What is Email-to-PDF Converter?

In today's digital workspace, the ability to convert email communications into permanent, portable PDF documents has become increasingly valuable. The Email-to-PDF Converter (formerly EML to PDF Converter) is a versatile Java library that solves this exact problem. Available on GitHub, this open-source tool provides developers and organizations with a reliable way to transform email files (.eml and .msg formats) into professional PDF documents that preserve formatting, attachments, and metadata.

Email to PDF Converter is a versatile Java-based tool that converts email files into PDF format while preserving formatting, inline images, and attachments. Originally developed by Nick Russler, this Apache-licensed project offers three distinct usage modes: as a Java library for integration into applications, as a command-line utility for batch processing, and as a desktop application with a graphical interface. The library handles the complex process of parsing email structures, cleaning malformed MIME headers, converting content to HTML, and ultimately rendering it as a PDF using the powerful wkhtmltopdf rendering engine.

Previous Next

Getting Started with Email-to-PDF Converter

Before using the Email-to-PDF Converter, ensure you have Java runtime environment and wkhtmltopdf. Latest releases are available on the GitHub repository, including windows setup.exe installer and platform-independent JAR file.

Install Email-to-PDF Converter Library via GitHub

git clone https://github.com/nickrussler/email-to-pdf-converter.git 

You can download the library directly from GitHub page.

Intelligent Email File Conversion to PDF via Java

TThe Email to PDF Converter library provides a comprehensive solution for transforming email files into universally accessible PDF documents inside Java apps. At its core, the library parses email MIME structures and converts them into clean, well-formatted HTML before PDF generation. This approach ensures that complex email layouts, styled text, and embedded elements render correctly in the final document. This basic example takes an EML file and converts it to PDF using default settings. The library handles all the complexity of parsing the email structure, extracting inline content, and generating the final PDF.

How to Convert EML Email File to PDF Documents via Java Library?

 
import mimeparser.MimeMessageConverter;
import java.io.File;

public class EmailConverterExample {
    public static void main(String[] args) {
        try {
            File emailFile = new File("example.eml");
            File outputPdf = new File("output.pdf");
            
            // Perform the conversion
            MimeMessageConverter.convertToPdf(emailFile, outputPdf);
            
            System.out.println("Conversion completed successfully!");
        } catch (Exception e) {
            System.err.println("Error during conversion: " + e.getMessage());
            e.printStackTrace();
        }
    }
}
 

Handle Attachment during Email to PDF Conversion via Java

The Email-to-PDF Converter library has provided options for extracting email attachments separately. Users can configure whether to extract attachments to a dedicated directory and optionally include a list of attachment names within the PDF document itself. The following code example demonstrates how to parse an email message separately from the conversion process, allowing for inspection or modification of email content before PDF generation. The ConversionOptions object provides programmatic access to all configuration parameters available in the command-line interface.

How Manage Attachments during Email to PDF Conversion via Java?

 
import mimeparser.MimeMessageConverter;
import mimeparser.MimeMessageParser;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.io.FileInputStream;
import java.util.List;

public class AttachmentHandler {
    public static void main(String[] args) {
        try {
            File emailFile = new File("email-with-attachments.eml");
            
            // Parse the email
            MimeMessageParser parser = new MimeMessageParser();
            parser.parse(new FileInputStream(emailFile));
            
            // Access attachment information
            List attachments = parser.getAttachmentList();
            
            System.out.println("Found " + attachments.size() + " attachments:");
            for (File attachment : attachments) {
                System.out.println("- " + attachment.getName());
            }
            
            // Convert with attachment extraction
            ConversionOptions options = new ConversionOptions();
            options.setExtractAttachments(true);
            options.setAddAttachmentNames(true);
            
            MimeMessageConverter.convertToPdf(
                parser, 
                new File("output-with-attachments.pdf"), 
                options
            );
            
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Multiple Email Format Support

The Email-to-PDF Converter supports both EML and MSG file formats, making it compatible with various email clients. EML files are standard text-based email formats used by many email applications, while MSG files are Microsoft Outlook's proprietary email format. This dual-format support ensures flexibility across different email ecosystems.

Inline Image Handling While Email to PDF Export

One of the standout features is proper handling of inline images embedded within email bodies. The open source Email-to-PDF Converter library correctly identifies and processes these images, ensuring they appear in the appropriate locations within the PDF output rather than as separate attachments.