How to print an email to pdf programmatically

13,515

Solution 1

I put a piece of software together that converts eml files to pdf's by parsing (and cleaning) the mime/structure, converting it to html and then use wkhtmltopdf to convert it to a pdf file.

It also handles inline images, corrupt mime headers and can use a proxy.

The code is available at github under apache V2 license.

Solution 2

I've found a better solution to the one I posted before. saving the email to html, then use jtidy to clean it up to xhtml. and lastly use flying saucer html renderer to save it into pdf.

Here is an example I wrote:

import com.lowagie.text.DocumentException;
import org.w3c.tidy.Tidy;
import org.xhtmlrenderer.pdf.ITextRenderer;
import java.io.*;
import java.util.*;
import javax.mail.*;

public class Email2PDF {

public static void main(String[] args) {

    Properties props = new Properties();
    props.setProperty("mail.store.protocol", "imaps");
    try {
        Session session = Session.getInstance(props, null);
        Store store = session.getStore();
        //read your latest email
        store.connect("imap.gmail.com", "[email protected]", "password");
        Folder inbox = store.getFolder("INBOX");
        inbox.open(Folder.READ_ONLY);
        Message msg = inbox.getMessage(inbox.getMessageCount());
        Multipart mp = (Multipart) msg.getContent();
        BodyPart bp = mp.getBodyPart(0);
        String filename = msg.getSubject();
        FileOutputStream os = new FileOutputStream(filename + ".html");
        msg.writeTo(os);
        //use jtidy to clean up the html 
        cleanHtml(filename);
        //save it into pdf
        createPdf(filename);
    } catch (Exception mex) {
        mex.printStackTrace();
    }
}

public static void cleanHtml(String filename) {
    File file = new File(filename + ".html");
    InputStream in = null;
    try {
        in = new FileInputStream(file);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    OutputStream out = null;
    try {
        out = new FileOutputStream(filename + ".xhtml");
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    final Tidy tidy = new Tidy();
    tidy.setQuiet(false);
    tidy.setShowWarnings(true);
    tidy.setShowErrors(0);
    tidy.setMakeClean(true);
    tidy.setForceOutput(true);
    org.w3c.dom.Document document = tidy.parseDOM(in, out);
}
public static void createPdf(String filename)
        throws IOException, DocumentException {
    OutputStream os = new FileOutputStream(filename + ".pdf");
    ITextRenderer renderer = new ITextRenderer();
    renderer.setDocument(new File(filename + ".xhtml"));
    renderer.layout();
    renderer.createPDF(os) ;
    os.close();
    }
}

Enjoy!

Solution 3

import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfWriter;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.*;
import javax.mail.*;

public class Email2PDF {

    public static void main(String[] args) {

        Properties props = new Properties();
        props.setProperty("mail.store.protocol", "imaps");
        try {
            Session session = Session.getInstance(props, null);
            Store store = session.getStore();
            store.connect("imap.gmail.com", "[email protected]", "password");
            Folder inbox = store.getFolder("INBOX");
            inbox.open(Folder.READ_ONLY);
            Message msg = inbox.getMessage(inbox.getMessageCount());
            Multipart mp = (Multipart) msg.getContent();
            BodyPart bp = mp.getBodyPart(0);
            createPdf(msg.getSubject(), (String) bp.getContent());
        } catch (Exception mex) {
            mex.printStackTrace();
        }
    }

    public static void createPdf(String filename, String body)
            throws DocumentException, IOException {

        Document document = new Document();
        PdfWriter.getInstance(document, new FileOutputStream(filename + ".pdf"));
        document.open();
        document.add(new Paragraph(body));
        document.close();
    }

}

I've used itext as the pdf library

Share:
13,515
Nick Russler
Author by

Nick Russler

Updated on June 14, 2022

Comments

  • Nick Russler
    Nick Russler almost 2 years

    I want to generate a PDF document from a "raw" email. This email could containt html or just text. I don't care for attachments.

    The resulting pdf should contain the proper formatting (from css and html) and also embedded images.

    My first idea was to render the email using an email client like thunderbird and then print it to pdf. Does thunderbird offer such an API or are there java libraries available to print an email to pdf?