Remote Printing – E-mail – Part 2 HTML
In this part, I’ve added the ability to print HTML. In order to print HTML, the HTML first needs to be converted so that it can be put into a PDF document and printed just like a PDF.
The following class has been extended to handle HTML attachments and print them. It’s a bit of a round about way of printing the HTML attachments – converting them with iText and then using PDFBox to print the – but it works.
package uk.co.vsf.printer.kyocera.component.impl;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.util.List;
import javax.activation.MimeType;
import javax.print.PrintException;
import org.apache.commons.io.IOUtils;
import org.apache.log4j.Logger;
import org.apache.pdfbox.pdmodel.PDDocument;
import uk.co.vsf.printer.kyocera.MimeTypeHandler;
import uk.co.vsf.printer.kyocera.bean.Attachment;
import uk.co.vsf.printer.kyocera.bean.Email;
import uk.co.vsf.printer.kyocera.component.PrinterComponent;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.html.simpleparser.HTMLWorker;
import com.lowagie.text.pdf.PdfWriter;
/**
* @author Victoria Scales
*/
public class PrinterComponentImpl implements PrinterComponent {
private Logger logger = Logger.getLogger(getClass());
private MimeTypeHandler mimeTypeHandler;
private void printAttachment(Attachment attachment) throws PrintException, IOException, PrinterException {
logger.info("Attachment: " + attachment);
MimeType attachmentMimeType = attachment.getMimeType();
if (mimeTypeHandler.isPdf(attachmentMimeType)) {
logger.info("Print pdf attachment: " + attachment);
printPdf(attachment.getData());
return;
} else if (mimeTypeHandler.isHtml(attachmentMimeType)) {
logger.info("Print html attachment: " + attachment);
printHtml(attachment);
return;
}
logger.info("Attachment not printed: " + attachment.getMimeType());
}
private void printHtml(Attachment attachment) throws IOException, PrinterException {
// convert the html to text to byte array representing pdf data
Document document = new Document();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
PdfWriter.getInstance(document, baos);
} catch (DocumentException e) {
throw new ITextPdfDocumentCreationException(e);
}
document.open();
// convert html to elements and add to the PDF
List<Element> elements = HTMLWorker.parseToList(new StringReader(IOUtils.toString(attachment.getData())), null);
for (Element element : elements) {
try {
document.add(element);
} catch (DocumentException e) {
e.printStackTrace();
}
}
document.close();
ByteArrayInputStream bias = new ByteArrayInputStream(baos.toByteArray());
printPdf(bias);
}
/**
* Handles printing PDF attachments.
*
* @param attachment
* to print
* @throws IOException
* if an error occurs during loading/closing of the attachment.
* @throws PrinterException
* if an error occurs during the print operation.
*/
private void printPdf(InputStream inputStream) throws IOException, PrinterException {
PDDocument document = null;
try {
document = PDDocument.load(inputStream);
PrinterJob printJob = PrinterJob.getPrinterJob();
document.silentPrint(printJob);
} finally {
if (document != null) {
document.close();
}
}
}
/**
* {@inheritDoc}
*/
public void printEmail(Email email) {
for (Attachment attachment : email.getAttachments()) {
try {
printAttachment(attachment);
} catch (PrintException e) {
new PrintingException(e);
} catch (IOException e) {
new PrintingException(e);
} catch (PrinterException e) {
new PrintingException(e);
}
}
}
public void setMimeTypeHandler(MimeTypeHandler mimeTypeHandler) {
this.mimeTypeHandler = mimeTypeHandler;
}
}
Please enable the Disqus feature in order to add comments