1. Các sản phẩm
  2.   E-mail
  3.   Java
  4.   Email-to-PDF Converter
 
  

Thư viện Java Email miễn phí để chuyển đổi EML & MSG sang PDF

Thư viện xử lý Email Java mã nguồn mở để chuyển đổi tệp Email (EML, MSG) sang PDF. Nó hỗ trợ xử lý tệp đính kèm và hình ảnh nội tuyến và nhiều định dạng Email trong các ứng dụng Java.

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

Bắt đầu với Email-to-PDF Converter

Trước khi sử dụng Email-to-PDF Converter, hãy đảm bảo bạn có môi trường chạy Java và wkhtmltopdf. Các bản phát hành mới nhất có sẵn trên kho GitHub, bao gồm trình cài đặt windows setup.exe và tệp JAR độc lập nền tảng.

Cài đặt thư viện Email-to-PDF Converter qua GitHub

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

Bạn có thể tải thư viện trực tiếp từ trang GitHub.

Chuyển đổi tệp Email thông minh sang PDF bằng Java

Thư viện Email to PDF Converter cung cấp giải pháp toàn diện để chuyển đổi tệp email thành tài liệu PDF có thể truy cập rộng rãi trong các ứng dụng Java. Cốt lõi của thư viện là phân tích cấu trúc MIME của email và chuyển chúng thành HTML sạch, định dạng tốt trước khi tạo PDF. Cách tiếp cận này đảm bảo các bố cục email phức tạp, văn bản có định dạng và các phần tử nhúng được hiển thị đúng trong tài liệu cuối cùng. Ví dụ cơ bản này lấy một tệp EML và chuyển nó sang PDF bằng các cài đặt mặc định. Thư viện xử lý toàn bộ sự phức tạp của việc phân tích cấu trúc email, trích xuất nội dung nội tuyến và tạo PDF cuối cùng.

Cách chuyển đổi tệp Email EML sang tài liệu PDF qua Thư viện Java?

 
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();
        }
    }
}
 

Xử lý tệp đính kèm trong quá trình chuyển đổi Email sang PDF bằng Java

Thư viện Email-to-PDF Converter cung cấp các tùy chọn để trích xuất tệp đính kèm email riêng biệt. Người dùng có thể cấu hình việc trích xuất tệp đính kèm vào một thư mục riêng và tùy chọn bao gồm danh sách tên tệp đính kèm trong tài liệu PDF. Ví dụ mã dưới đây minh họa cách phân tích một tin nhắn email riêng biệt khỏi quá trình chuyển đổi, cho phép kiểm tra hoặc chỉnh sửa nội dung email trước khi tạo PDF. Đối tượng ConversionOptions cung cấp quyền truy cập lập trình vào tất cả các tham số cấu hình có sẵn trong giao diện dòng lệnh.

Cách quản lý tệp đính kèm trong quá trình chuyển đổi Email sang PDF bằng 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();
        }
    }
}

Hỗ trợ đa định dạng Email

Email-to-PDF Converter hỗ trợ cả định dạng tệp EML và MSG, giúp tương thích với nhiều client email khác nhau. Tệp EML là định dạng email dựa trên văn bản tiêu chuẩn được nhiều ứng dụng email sử dụng, trong khi tệp MSG là định dạng email độc quyền của Microsoft Outlook. Sự hỗ trợ đa định dạng này đảm bảo tính linh hoạt trong các hệ sinh thái email khác nhau.

Xử lý hình ảnh nội tuyến khi xuất Email sang PDF

Một trong những tính năng nổi bật là xử lý đúng các hình ảnh nội tuyến được nhúng trong nội dung email. Thư viện Email-to-PDF Converter mã nguồn mở xác định và xử lý chính xác các hình ảnh này, đảm bảo chúng xuất hiện ở vị trí thích hợp trong PDF thay vì là các tệp đính kèm riêng.

 Tiếng Việt