How to print email to pdf programmatically

I want to create a PDF document from raw email. This email can contain html or just text. I don't need attachments.

The resulting pdf must contain correct formatting (from css and html) as well as inline images.

My first idea was to send an email using an email client like thunderbird and then print it in pdf format. Does thunderbird offer such an API or are there java libraries for printing pdf emails?

+3


source to share


6 answers


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

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



The code is available on github under the apache V2 license.

+2


source


I found the best solution for the one I wrote earlier. keeping the email in html then use jtidy to scrape it down to xhtml. and finally a flying saucer html renderer to save it to pdf.

Here's 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", "youremail@gmail.com", "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!

+3


source


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", "youremail@gmail.com", "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 used itext as PDF library

+1


source


You can read HTML content with your email client and then use iText to convert it to PDF

0


source


Have a look at fpdf and fpdi and free PHP libraries are used to create PDF documents.

Since SMTP has conventions, actually strict rules, you can always count on the first blank line before the message content. So you can definitely parse everything after the first part of the line to get the complete message.

For inline images, you will need a basic 64 decoder (usually) or some other decoder based on the encoding type of the email attachments to convert the data to a humanoid image.

0


source


You can try Apache PPDFbox library .

It seems to have a good API and it supports printing as well. PrintPDF

You will need to run the print command from the CLI with your file as a parameter.

Edit: This is Java and open-source.

Hope it helps!

0


source







All Articles