Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print an email to pdf programmatically [closed]

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?

like image 302
Nick Russler Avatar asked Mar 17 '23 17:03

Nick Russler


1 Answers

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!

like image 74
liorsolomon Avatar answered Apr 02 '23 08:04

liorsolomon